#!/bin/sh
# Regenerate the configuration for pe-exec.

[ -n "${PATH}" ] && . "/etc"/profile
. "/etc"/pe-format2.conf

firstworking() {
	while [ ${#} -gt 1 ]; do
		if "${1}" --version >/dev/null 2>&1; then
			echo "${1}"
			return
		fi
		shift
	done

	echo "\${${1}} is not set and none of the default applications were found!" >&2
}

: ${UNKNOWN:=$(firstworking wine mono ilrun dosbox dosemu UNKNOWN)}
: ${MSDOS:=$(firstworking dosbox dosemu MSDOS)}
: ${WIN32:=$(firstworking wine WIN32)}
: ${CLR:=$(firstworking mono ilrun CLR)}
: ${WIN64:=$(firstworking wine64 WIN64)}

if [ -z "${UNKNOWN}" -a -z "${MSDOS}" -a -z "${WIN32}" -a -z "${CLR}" -a -z "${WIN64}" ]; then
	echo 'None of the expected interpreters were set, aborting.' >&2
	exit 1
else
	printf '%s\0' "${UNKNOWN}" "${MSDOS}" "${WIN32}" "${CLR}" "${WIN64}" \
		> "/var/lib/pe-format2" && \
	chmod a+r "/var/lib/pe-format2"
	exit ${?}
fi
