#!/bin/sh -e
#
# $Id: config,v 1.15.2.2 2003/11/20 19:58:03 grendel Exp $
#
EXTVER=

. /usr/share/debconf/confmodule

db_version 2.0
db_capb

get_cfg_port() {
    local yesno;
    local port="22202";
    
    db_title 'Config Interface Port'
        
    while test 1; do
     db_input medium caudium${EXTVER}/config_port || true
     db_go
     
     db_get caudium${EXTVER}/config_port || true
     port=$RET
     if test -z "$RET"; then
      db_reset caudium${EXTVER}/config_port || true
      continue
     fi
     
     db_subst caudium${EXTVER}/last_screen cfgport "$port"
     return 0
    done
}

get_iface() {
    local port="80";
    local yesno;
    
    db_title 'Port number'
    db_subst caudium${EXTVER}/listen_on portno $port

    while test 1; do
     db_input medium caudium${EXTVER}/listen_on || true
     db_go
     
     db_get caudium${EXTVER}/listen_on
     port=$RET
     if test -z "$port"; then
        port="`expr $port + 1`"	
     else
        return 0
     fi
     
     db_reset caudium${EXTVER}/listen_on
     db_set caudium${EXTVER}/listen_on $port || true
    done
}

get_cfg_login() {
    local login="${1:-admin}"

    db_title 'Config interface login'
    db_subst caudium${EXTVER}/config_login ciflogin $login

    while test 1; do
        db_input high caudium${EXTVER}/config_login || true
        db_go

        db_get caudium${EXTVER}/config_login
        login="$RET"
        if test -z "$login"; then
            login="admin"
        else
            return 0
        fi

        db_reset caudium${EXTVER}/config_login
        db_set caudium${EXTVER}/config_login $login || true
    done
}

get_cfg_pass() {
    local pass="${1:-password}"
    local pass2="${1:-password}"
    local pass_reset="no"
    local port="22202"
    
    db_title 'Config interface password'
    db_subst caudium${EXTVER}/config_password cifpass $pass

    while test 1; do
        db_input high caudium${EXTVER}/config_password || true
        db_go

        db_get caudium${EXTVER}/config_password
        pass="$RET"
        if test -z "$pass"; then
            pass="password"
	    pass_reset="yes"
        fi
	
        db_input high caudium${EXTVER}/config_password_confirm || true
	db_go
	
	db_get caudium${EXTVER}/config_password_confirm
	pass2="$RET"
	if test -z "$pass2"; then
	    pass2="password"
	    pass_reset="yes"
	fi
	
	if test "$pass" != "$pass2"; then
	  db_input high caudium${EXTVER}/config_password_mismatch || true
	  db_go
	  continue
	fi
	
	if test "$pass_reset" = "yes" -o "$pass" = "password" ; then
	  db_get caudium${EXTVER}/config_port || true
          port=$RET
	  db_subst caudium${EXTVER}/config_password_reset cfgport "$port"
	  db_input high caudium${EXTVER}/config_password_reset || true
	  db_go
	  return 0
	else
	  return 0
	fi
	
        db_reset caudium${EXTVER}/config_password
	db_reset caudium${EXTVER}/config_password_confirm
        db_set caudium${EXTVER}/config_password $pass || true
    done
}

if test ! -f /etc/caudium${EXTVER}/servers/Global_Variables; then
    get_cfg_port
    get_cfg_login
    get_cfg_pass
    get_iface
fi

db_title 'Startup options'
db_input medium caudium${EXTVER}/start_options || true

db_title 'Performance tweaking'
db_input medium caudium${EXTVER}/performance || true

#db_title 'Experimental code'
#db_input low caudium${EXTVER}/experimental_http || true

db_title 'Caudium configuration'
db_input medium caudium${EXTVER}/last_screen || true
    
db_go || true

exit 0
