#! /bin/sh -e
#
# Configure script for Xbuffy.  Called with "-u" it will use the
# options in /etc/xbuffy/ledconfig (if it exists), otherwise it will
# query the invoker.
#
# Joel Rosdahl <joel@debian.org>

LED_PROGRAM="/usr/lib/xbuffy/led"

CONF_DIR="/etc/xbuffy"
CONF_FILE="$CONF_DIR/ledconfig"

if [ ! -d $CONF_DIR ]; then
    mkdir $CONF_DIR
fi

if [ "$1" = "-u" -a -r $CONF_FILE ]; then
    . $CONF_FILE
fi

if [ -z "$ENABLE_LED" ]; then
    echo ""
    echo "Xbuffy can notify you by blinking a keyboard LED when new mail has arrived."
    echo "However, to access the Linux console driver (to be able to change the LED"
    echo "states) the code needs to run as root.  In Xbuffy this is done by a small"
    echo "external setuid-root program.  This should make it safe from exploits."
    echo ""
    echo "Note: If you enable this, by default, this means that anyone with execution"
    echo "access to the LED blinking program can change the states of the keyboard LEDs."
    echo ""
    echo -n "Do you want to enable LED support via a small setuid-root program [y/N]? "
    
    read answer
    echo ""

    case "$answer" in
        [Yy]*)
            ENABLE_LED="yes"
            ;;
        *)
            ENABLE_LED="no"
            ;;
    esac

    cat <<EOF > $CONF_FILE
# Do you want to enable LED blinking via a small setuid program
# (/usr/lib/xbuffy/led)?

ENABLE_LED="$ENABLE_LED"
EOF
fi

if [ "$ENABLE_LED" = "yes" ]; then
    echo -n "Enabling LED blinking support..."
    if [ -x /usr/sbin/suidregister ]; then
        suidregister -s xbuffy $LED_PROGRAM root root 4755
    else
        chown root.root $LED_PROGRAM
        chmod 4755 $LED_PROGRAM
    fi
else
    echo -n "Disabling LED blinking support..."
    if [ -x /usr/sbin/suidregister ]; then
        suidregister -s xbuffy $LED_PROGRAM root root 755
    else
        chmod 755 $LED_PROGRAM
    fi
fi

echo done.
