#!/bin/ash
#
# $Id: ui-conf,v 1.14 2002/07/29 11:16:14 malekith Exp $
#
# This file is part of PLD batch-installer
# Copyright (C) 2001 CYBER Service
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# Author: Rafał Kleger-Rudomin
# Contributors:


make_form () {
local active
if test "$1"; then active="$1"; else active=lang; fi

nls "Initial configuration of installed system."
echo "<br>"
nls "Press Return (Enter) to change"
cat <<EOF
<meta title="$(nls "Configuration")">
<meta height=25 width=80 active=menures>
<br>
<br>
<menu height=18 width=76 id=menures res=1 selected=$active>
EOF

separator "$(nls "Language and timezone")"

item lang "$(nls "Default language")"
item timezone "$(nls "Timezone")"
item clock_utc "$(nls "Clock in UTC?")"

separator "$(nls "Basic network configuration")"
item net_config "$(nls "How to configure network?")"


case "$net_config" in
manual|none|installer_settings)
    if test "$net_config" = "installer_settings" && grep -q '^net_ipaddr=.*dhcp' $conffile; then
	:
    else
	item net_hostname "$(nls "Host name")"
	item net_domainname "$(nls "Net domain name")"
    fi
    ;;
esac

case "$net_config" in
manual|dhcp)
    item net_device "$(nls "Net interface name (in most cases single Eternet card eth0)")" 
    item net_device_module "$(nls "Network card type")" 
    item net_device_module_options "$(nls "Options for network card module")" 
    ;;
esac

case "$net_config" in
manual)
    item net_ipaddr "$(nls "IP address of machine")" 
    item net_prefix "$(nls "Prefix for IP address")" 
    item net_gateway "$(nls "IP address if default router")" 
    item net_dns "$(nls "List of dns servers (IP addresses) (optional)")" 
    ;;
esac


separator "$(nls "Administrator password")"
item root_pass "$(nls "Super-user (root) password")"

separator "$(nls "First user")"
item user_realname "$(nls "User real name")"
item user_name "$(nls "User login name")"
item user_pass "$(nls "User password")"


cat <<EOF
</menu>
<br>
<button res=2 caption='$(nls "_Finish")'>
<button res=3 caption='$(nls _Help)'>
EOF
}

ui-lang () {
dml <<EOF 
    <br>`nls "Choose the default language for the whole system"`
    <br>
    <meta title="$(nls "Default language")">
    <menu id=res_menu res=1>
    <item id=pl_PL>$(nls Polish) (pl__PL)
    <item id=de_DE>$(nls German) (de__DE)
    <item id="">$(nls "Do not set")
    </menu> 
    $@
EOF
}

ui-clock_utc () {
dml <<EOF 
    <br>`nls "Does your system clock runs in UTC?"`
    <br>
    <meta title="$(nls "UTC?")">
    <menu id=res_menu res=1>
    <item id=yes>$(nls "Yes.")
    <item id=no>$(nls "No.")
    </menu> 
    $@
EOF
}

ui-timezone () {
dml <<EOF
<br>`nls "Choose the timezone"`
<br>
<input id=res_menu caption="$timezone">
<br>
$@
EOF
}

ui-timezone () {
dml <<EOF 
    <br>`nls "Choose the timezone"`
    <br>
    <meta title="$(nls "Timezone")">
    <menu id=res_menu res=1>
    <item id=Warsaw>$(nls Warsaw)
    <item id="">$(nls "Do not set")
    </menu> 
    $@
EOF
}

ui-net_config () {
dml <<EOF 
    <br>`nls "How to configure the network?"`
    <br>
    <meta title="$(nls "Network config source")">
    <menu id=res_menu res=1>
    <item id=manual>$(nls "Manual")
    <item id=installer_settings>$(nls "Use installer settings")
    <item id=dhcp>$(nls "DHCP")
    <item id=none>$(nls "no network")
    </menu> 
    $@
EOF
}

ui-net_hostname () {
dml <<EOF 
    <meta title="$(nls "Host name")">
    <br><input id=res_menu caption="$net_hostname"><br>
    $@
EOF
}

ui-net_domainname () {
dml <<EOF 
    <meta title="$(nls "Net domain name")">
    <br><input id=res_menu caption="$net_domainname"><br>
    $@
EOF
}

# !! rest of ui-net_ is defined in ui-dialogs !!


ui-root_pass () {
dml <<EOF 
    <br>`nls "Enter password that will be set for user 'root'"`
    <br>
    <meta title="$(nls "Root password")">
    <menu id=res_menu res=1>
    <item id=ui_custom>${custom_text}
    <item id=generate>$(nls "Generate password for me")
    </menu>
    $@
EOF
}

ui-user_realname () {
dml <<EOF 
    <meta title="$(nls "User real name")">
    <br><input id=res_menu caption="$user_realname"><br>
    $@
EOF
}

ui-user_name () {
dml <<EOF 
    <meta title="$(nls "User login name")">
    <br><input id=res_menu caption="$user_name"><br>
    $@
EOF
}

ui-user_pass () {
dml <<EOF 
    <br>`nls "Enter password that will be set for user %s" "$user_name"`
    <br>
    <meta title="$(nls "User password")">
    <menu id=res_menu res=1>
    <item id=ui_custom>${custom_text}
    <item id=generate>$(nls "Generate password for me")
    </menu>
    $@
EOF
}


generate_pass () {
    local tmp
    while : ; do
	tmp=`mktemp /tmp/pass-XXXXXXXX`
	echo $tmp | grep -q "[0-9]" || continue
	break
    done
    echo $tmp | sed s/.*-//
    rm -f $tmp
}


##########################################################
######################## main ############################
##########################################################


export PATH=.:..:$PATH
. installer-functions
. ui-functions
. ui-dialogs

tmpdir=/tmp
if [ -d $HOME/tmp ] ; then
  tmpdir=$HOME/tmp
fi

custom_text=$(nls "_C_u_s_t_o_m")
auto_text=$(nls "_A_u_t_o")
none_text=$(nls "None")

find_config_paths $@
load_or_create_sys_config

while : ; do

    # create form, run it and get the result
    eval "`make_form $result | dml`"
  
    # all dialogs returns res, change to 'result' to not confuse program
    if test $res = 1 ; then
      result=$menures
    else
      result=$res
    fi
 
    do_validate=1
    case "$result" in
	root_pass)
	    ask root_pass
	    if test "x$root_pass" = "xgenerate"; then 
		root_pass=`generate_pass`; 
	    fi
	    ;;
	user_pass)
	    ask user_pass
	    if test "x$user_pass" = "xgenerate"; then user_pass=`generate_pass`; fi
	    ;;
	2)
	    break
	    ;;
	
	3)
	    : can you help me?
	    ;;
	
	*)
	    ask "$result"
	    ;;

    esac

    save_sys_config
done
