#!/bin/sh
#
# binfmts trigger.
#
# Binaries can be specified like:
#  binfmts="<path> [<args> ...]"
#
# Arguments:	$ACTION = [run/targets]
#		$TARGET = [post-install/pre-remove]
#		$PKGNAME
#		$VERSION
#		$UPDATE = [yes/no]
#
ACTION="$1"
TARGET="$2"
PKGNAME="$3"
VERSION="$4"
UPDATE="$5"

export PATH="usr/bin:usr/sbin:/usr/sbin:/usr/bin:/sbin:/bin"

case "$ACTION" in
targets)
	echo "post-install pre-remove"
	;;
run)
	[ -x /usr/bin/update-binfmts ] || exit 0
	[ -z "${binfmts}" ] && exit 0

	case "$TARGET" in
	post-install)
		echo "$binfmts" | tr '\' '&' 2> /dev/null | while read line; do
			line=$(echo $line | tr '&' '\' 2> /dev/null)
			set -- ${line}
			_bin="$1"; shift; _args="$@"; _bname="$(basename ${_bin})"
			update-binfmts --package ${PKGNAME} --install ${_bname} ${_bin} ${_args}
		done
		;;
	pre-remove)
		echo "$binfmts" | tr '\' '&' 2> /dev/null | while read line; do
			line=$(echo $line | tr '&' '\' 2> /dev/null)
			set -- ${line}
			_bin="$1"; shift; _args="$@"; _bname="$(basename ${_bin})"
			if [ -f /var/lib/binfmts/${_bname} ]; then
				update-binfmts --package ${PKGNAME} --remove ${_bname} ${_bin}
			fi
		done
		;;
	*)
		exit 1
		;;
	esac
	;;
*)
	exit 1
	;;
esac

exit 0
# end
# end
