#!/bin/ash

# 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: Michał Moskal


# given the variable name as $1, this function
# runs proper dialog (ui-<variable>) and then sets value for variable
# also handles Cancel (res != 1)
ask () {
    local tmp add_input val pass
    local cancel=$(nls "Cancel")
    eval "tmp=\"\$$1\""
    pass=
    add_input="<br><button caption=Ok res=2><button caption="$cancel" res=-1>"
    res=""
    while : ; do
	res_menu=""
	res_input=""
	eval "`ui-$1 $add_input`"
	if test "$res" -eq 3 ; then
	     break
	fi
	if test "$res" -ge 1  -a "x$res_menu" = "xui_custom"; then
	    # display input line
	    # echo $1 | grep -q 'pass$' && pass=pass=1
	    add_input="<br><meta active=res_input><input id=res_input caption=\"$tmp\" $pass><br><button caption=Ok res=3><button caption=\"$cancel\" res=-1>"
	else
	    break
	fi
    done
    val=
    if test $res = 3; then 
	# 'ok' for inputbox was hit, if input is filled, use it
	val="$res_input"
    elif test $res = 1 -o $res = 2; then
	# menu or 'ok' was hit
	val="$res_menu"
    else
	# if canceled, restore previous value
	eval "$1=\"$tmp\""
	return 0
    fi

    eval "$1=\"$val\""
}

separator () {
    echo "<item ghosted=1>${1}"
}

# show the complete item 
# usage: item varname descritpion
item () {
    eval "val=\$${1}"
    #if echo $1 | grep -q 'pass$' ; then
    #  val=XXXX
    #fi
    echo "<item id=${1} nohl=1>   ${1} = $val"
}


#####################################
# some common dialogs

ui-net_device () {
dml <<EOF 
    <br>`nls "Select network device"`
    <br>
    <meta title="$(nls "Network device")">
    <menu id=res_menu res=1>
    <item id="ui_custom">$custom_text
    <item id="eth0">eth_0: `nls "first ethernet card"`
    <item id="eth1">eth_1: `nls "second ethernet card"`
    </menu>
    $@
EOF
}

# create device items
net_device_items () {
local automods=`detect-net-devices -m || :`
local text=$(nls "_*_d_e_t_e_c_t_e_d_*")
grep -vs '^~' /etc/eth.list.in | while read line; do
    dev=`echo $line | sed 's/\.o|.*//'`
    for i in $automods; do 
	if test "x$i" = "x$dev"; then
	    auto="(${text})"
	    break
	else
	    auto=""
	fi
    done
    dev2=`echo $dev | sed 's/_/__/g'`
    desc=`echo $line | sed 's/[^|]*|//' | sed 's/|.*//'`
    echo "<item id=\"$dev\">$dev2: _$desc $auto"
done
}

ui-net_device_module () {
dml <<EOF 
    <br>`nls "Select one of network device modules below"`
    <br>`nls "If you have PCI card you may try"` ${auto_text}
    <meta title="$(nls "Network card")">
    <menu id=res_menu res=1>
    <item id=ui_custom>${custom_text}
    <item id=auto>${auto_text} (detect during installation, PCI only)
    `net_device_items`
    </menu>
    $@
EOF
}

ui-net_device_module_options () {
dml <<EOF
    <br>`nls "If your card requires any extra parameters,"`
    <br>`nls "put them here, e.g."` irq=10
    <meta title="$(nls "Network card special options")">
    <menu id=res_menu res=1>
    <item id=ui_custom>${custom_text}
    <item id=>${none_text}
    <item id="io=0x300">io=0x300
    </menu>
    <br>
    $@
EOF
}

ui-net_ipaddr () {
item1="192.168.0.1"
item2="192.168.0.2"
item3="192.168.0.3"
dml <<EOF 
    <br>`nls "Assign IP address"`
    <br>
    <meta title="$(nls "IP Address")">
    <menu id=res_menu res=1>
    <item id=ui_custom>${custom_text}
    <item id="$item2">`nls "Commonly used address for local network"` $item2
    <item id="$item3">`nls "Commonly used address for local network"` $item3
    <item id="$item1">`nls "Commonly used address for local masq router"` $item1
    <item id=dhcp>`nls "Get automatically from dhcp server"`
    </menu>
    $@
EOF
}

ui-net_prefix () {
local extra=""
local text=$(nls "resembles netmask")
if test "$net_ipaddr"; then
    extra="$(nls "for address") $net_ipaddr"
fi
dml <<EOF 
    <br>`nls "Input IP Prefix"` $extra
    <br>`nls "For local address, e.g 192.168.*, you may choose"` $auto_text
    <br>
    <meta title="$(nls "IP Prefix")">
    <menu id=res_menu res=1>
    <item id=ui_custom>${custom_text}
    <item id=auto>${auto_text}
    <item id=16>16 ($text 255.255.0.0)
    <item id=24>24 ($text 255.255.255.0)
    <item id=30>30 ($text 255.255.255.252)
    </menu>
    $@
EOF
}

ui-net_gateway () {
local text=$(nls "Typical masquerading gateway")
item1="192.168.0.1"
item2="192.168.1.1"
item3="192.168.2.1"
dml <<EOF 
    <br>`nls "Enter local gateway (default router)"`
    <br>
    <meta title="$(nls "Gateway")">
    <menu id=res_menu res=1>
    <item id=ui_custom>${custom_text}
    <item id="$item1">$text $item1
    <item id="$item2">$text $item2
    <item id="$item3">$text $item3
    </menu>
    $@
EOF
}

ui-net_dns () {
local extra text
text=$(nls "typical dns for masqueraded network")
item1="194.204.159.1 194.204.152.34"
item2="192.168.0.1"
item3="192.168.1.1"
extra=""
case "$LANG" in 
    pl*) 
	extra="$extra <item id=\"$item1\">Serwery tpsa.pl $item1"
	;; 
esac

dml <<EOF 
    <br>`nls "Enter DNS servers"`
    <br>
    <meta title="$(nls "DNS Servers")">
    <menu id=res_menu res=1>
    <item id=ui_custom>${custom_text}
    $extra
    <item id="$item2">$item2: $text
    <item id="$item3">$item3: $text
    </menu>
    $@
EOF
}



ui-net_v6 () {
dml <<EOF 
    <br>`nls "Use v6?"`
    <br>
    <meta title="$(nls "Use IPv6?")">
    <menu id=res_menu res=1 selected="$net_v6">
    <item id="auto">auto
    <item id="no">no
    <item id="yes">yes
    </menu>
    $@
EOF
}
