#!/bin/sh

### BEGIN INIT INFO
# Provides:          exagear
# Required-Start:    binfmt-support
# Required-Stop:     binfmt-support
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: registers ExaGear as a handler for x86 binaries
# Description:       registers ExaGear as a handler for x86 binaries
### END INIT INFO

#
# Copyright (c) 2013 "Elbrus Technologies" LLC. All rights reserved.
#
# This script comes with NO warranty, not even for MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.
#

DESC="ExaGear"
NAME="x86_guest"
PACKAGE="exagear"

MAGIC="\\x7f\\x45\\x4c\\x46\\x01\\x01\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x00\\x03\\x00"
MASK="\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfc\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfc\\xff\\xff\\xff"

UBT_WRAPPER="/opt/exagear/bin/ubt_wrapper_suid"
OPTIONS="--preserve yes --credentials yes"
case "$1" in
    start|restart|reload|force-reload)
        echo -n "Starting $DESC: "
        update-binfmts --package ${PACKAGE} --install ${NAME} ${UBT_WRAPPER} --magic ${MAGIC} --mask ${MASK} ${OPTIONS}
        ;;
    stop)
        echo -n "Stopping $DESC: "
        update-binfmts --package ${PACKAGE} --remove ${NAME} ${UBT_WRAPPER}
        ;;
    status)
        echo -n "Checking status $DESC: "
        update-binfmts --display ${NAME} | grep "enabled";
        exit $?
        ;;
    *)
        echo "Usage: $NAME {start|stop|restart|reload|force-reload|status}" >&2
        exit 1
        ;;
esac

exit 0
