#!/bin/sh -e
# If /etc/qadsl.conf exists, rename it alive.conf to preserve
# user settings.  OK, GNU Alive can read old qadsl.conf files,
# the alive.deb does otherwise install alive.conf which makes
# the system collect cruft, and alive.conf is the premiere
# choice for the program, so if it's unconfigured things might
# go bad.
#
# Here we potentially edit a config file from a previous
# install, but if the user has told us to use debconf the
# we should merely overwrite previously written debconf
# values.

# Source debconf library.
. /usr/share/debconf/confmodule

CONF_FILE=/etc/alive.conf
INIT_FILE=/etc/init.d/alive

db_get alive/use_debconf
if [ "$RET" = "true" ]; then
    db_get alive/login_server
    SERVER=$RET
    db_get alive/username
    USERNAME=$RET
    db_get alive/password
    PASSWORD=$RET

    # Setup alive.conf
    sed "
s/\(USERNAME[	 ]*=\).*/\1 $USERNAME/
s/\(PASSWORD[	 ]*=\).*/\1 $PASSWORD/
s/\(SERVER[	 ]*=\).*/\1 $SERVER/" <$CONF_FILE >$CONF_FILE.new && mv $CONF_FILE.new $CONF_FILE

    # Activate /etc/init.d/alive
    sed "s/\(^ACTIVE\).*$/\1=yes/" <$INIT_FILE >$INIT_FILE.new && mv $INIT_FILE.new $INIT_FILE
    chmod +x $INIT_FILE
fi

# Start it or, at least, display "edit me".
if [ -x $INIT_FILE ]; then
    update-rc.d alive defaults >/dev/null
    if [ -x /usr/sbin/invoke-rc.d ]; then
	invoke-rc.d alive start || exit 0
    else
	$INIT_FILE start || exit 0
    fi
fi
