#!/bin/bash

PATH=/bin:/sbin:/usr/bin:/usr/sbin
NETSTAT=/bin/netstat
SERVICES=/etc/services
CONFFILE=/etc/boa/boa.conf
DEFPORT=80

. /usr/share/debconf/confmodule

#set -e

resolve_port_problem ()
{
    OK=0

    db_text high boa/port_prob_dsc || true	
    db_go

    #
    # Loop until a valid port is selected.
    #

    while [ $OK = 0 ]; do
        db_input high boa/port_prob || true
        db_go
        db_get boa/port_prob

        if [ "$RET" = "Do nothing" ]; then
            OK=1
        elif [ "$RET" = "Specify an alternative port" ]; then
            db_input critical boa/select_port || true
            db_go

            db_get boa/select_port

            PORT=$RET

            check_port && OK=1
        fi
        
        if [ $OK = 0 ]; then
            db_text high boa/input_prob_dsc || true
            db_fset boa/port_prob seen false
            db_fset boa/select_port seen false
            db_go
        fi
        
    done
}

select_port () {
    OK=0

    db_text high boa/port_no_prob_dsc || true	
    db_go

    #
    # Loop until a valid port is selected.
    #

    while [ $OK = 0 ]; do
        db_input high boa/port_prob || true
        db_go
        db_get boa/port_prob
        
        if [ "$RET" = "Do nothing" ]; then
            OK=1
        elif [ "$RET" = "Specify an alternative port" ]; then
            db_input high boa/select_port || true
            db_go
            db_get boa/select_port
            PORT=$RET
            check_port && OK=1
        fi
        
        if [ $OK = 0 ]; then
            db_text high boa/input_prob_dsc || true
            db_fset boa/port_prob seen false
            db_fset boa/select_port seen false
            db_go
        fi
        
    done
}

port_to_name () {
RESULT=`grep -w $PORT_TMP $SERVICES | grep tcp | awk '{print $1}'`
if [ -n "$RESULT" ]; then
    PORT_TMP=$RESULT
fi

}

# Determine if boa is set to something other then port 80 
get_setting () {
    db_get boa/select_port
    PORT=$RET

    if [ -z $PORT ]; then
        PORT=$DEFPORT
    fi
}

# Determine whether the specified port is bound.
check_port () {
OUTPUT=`$NETSTAT -plunt | awk '{print $4}' | grep -w $PORT `
if [ "$OUTPUT" ]; then
    PORT_OUT=1
    return 1
else
    PORT_OUT=0
    return 0
fi
}


INSTALL_STAT=`dpkg -s boa | grep Status | awk '{print $4}'`
test -f $CONFFILE && PORT_CONF=`grep "Port 80" $CONFFILE`

if [ "$INSTALL_STAT" = "installed" ]; then
    if [ -x /etc/init.d/boa ]; then
        /etc/init.d/boa stop 1>&2> /dev/null || true
    fi
fi

if [ "$INSTALL_STAT" = "installed" ]; then
    # Ok, we're not installed, so someone called re-configure
    # So, if we're not using port 80, ask the question
    PORT=$DEFPORT
    check_port

    get_setting
    if [ "$PORT" != "$DEFPORT" ]; then 
        if [ "$PORT_OUT" = "1" ]; then
            resolve_port_problem
        else
            select_port
        fi
    fi
else
    # We're installing, see if 80 is in use.
    PORT=$DEFPORT
    check_port

    if [ "$PORT_OUT" = "1" ]; then
        resolve_port_problem
        PROBLEM=1
    fi
    if [ "$PORT_OUT" = "0" ]; then
	db_stop
    	update-rc.d boa defaults 20 > /dev/null
    	echo "Updating rc.d symbolic links to start boa upon booting."
    	echo "Starting boa ..."
    	/etc/init.d/boa start  > /dev/null || true
    fi
fi
