#!/bin/sh
# test for required software

tmpd="/tmp/tsclient"
touch ${tmpd}
TF="/tmp/tsclient"

msg_tsclient="Frenzy TSClient"
msg_error=""
msg_no_mkisofs="\n   mkisofs. \n,   sysutils/cdrtools."
msg_buildiso="  ..."
msg_ready="\n  ."

msg_tc_setup="   "
msg_tc_ip="IP-             "
msg_tc_mask="         "
msg_tc_gate="                 "
msg_tc_tserv="   "
msg_tc_tparam="     "
msg_tc_build=" ISO-"

msg_tci_ip=" IP- "
msg_tci_mask="  "
msg_tci_gate="   "
msg_tci_tserv="  "


msg_yes="YES"
msg_no="NO"

msg_rdconfig="  "
msg_back="<--"
msg_title=" "
msg_setusername="  "
msg_setpassword=" "
msg_setdomain=" "
msg_setkb="  "
msg_setcolor="  "

msg_susername="      "
msg_spassword="               "
msg_sdomain="                "
msg_skbd="  "
msg_scolor="         "
msg_sfs="   "
msg_sfsm="  "
msg_exit=""

msg_defscr=" "
msg_fullscreen="  "



tsc_build ()
{

FRENZY_ISO="."
FRENZY_ISOIMAGE="tsclient.iso"
TSCLIENT_DIR="frenzy-rdp"

# prepare config files
echo "ip=$tsclient_ip" > ${TSCLIENT_DIR}/frenzy/conf/ip.config
echo "mask=$tsclient_mask" >> ${TSCLIENT_DIR}/frenzy/conf/ip.config
echo "gate=$tsclient_gate" >> ${TSCLIENT_DIR}/frenzy/conf/ip.config

echo "server=$tsclient_tserv" > ${TSCLIENT_DIR}/frenzy/conf/ts.config
echo "keys=\"$tsclient_keys\"" >> ${TSCLIENT_DIR}/frenzy/conf/ts.config

dialog --title " $msg_tsclient " --infobox "$msg_buildiso" 8 42
mkisofs -quiet -b boot/cdboot -no-emul-boot -c boot/boot.catalog \
-boot-load-size 4 \
-r -J -D -V "FrenzyRDP" -o ${FRENZY_ISO}/${FRENZY_ISOIMAGE} ${TSCLIENT_DIR}
# > /dev/null 2>&1
dialog --title " $msg_tsclient " --msgbox "$msg_ready" 7 42
}



set_ip ()
{
dialog --title " $msg_tc_setup " --inputbox "$msg_tci_ip" 8 50 2>${tmpd}
tsclient_ip=`cat ${tmpd}`
if [ -z "${tsclient_ip}" ]; then
tsclient_ip="DHCP"
fi
}

set_mask ()
{
dialog --title " $msg_tc_setup " --inputbox "$msg_tci_mask" 8 50 2>${tmpd}
tsclient_mask=`cat ${tmpd}`
}

set_gate ()
{
dialog --title " $msg_tc_setup " --inputbox "$msg_tci_gate" 8 50 2>${tmpd}
tsclient_gate=`cat ${tmpd}`
}

set_tserv ()
{
dialog --title " $msg_tc_setup " --inputbox "$msg_tci_tserv" 8 50 2>${tmpd}
tsclient_tserv=`cat ${tmpd}`
if [ -z "${tsclient_tserv}" ]; then
tsclient_tserv="10.0.0.1"
fi
}


set_username ()
{
dialog --title "$msg_title" \
--inputbox "$msg_setusername" 8 30 2>${TF}
rusername=`cat ${TF}`
R_UN=""
if [ -n "${rusername}" ]; then
R_UN="-u ${rusername} "
fi
}

set_password ()
{
dialog --title "$msg_title" \
--inputbox "$msg_setpassword" 8 30 2>${TF}
rpassword=`cat ${TF}`
R_PW=""
rpx=""
if [ -n "${rpassword}" ]; then
R_PW="-p ${rpassword} "
rpx="*******"
fi
}


set_domain ()
{
dialog --title "$msg_title" \
--inputbox "$msg_setdomain" 8 30 2>${TF}
rdomain=`cat ${TF}`
R_DM=""
if [ -n "${rdomain}" ]; then
R_DM="-d ${rdomain} "
fi
}

set_kb ()
{
dialog --title "$msg_title" \
--inputbox "$msg_setkb" 8 30 2>${TF}
rkb=`cat ${TF}`
R_KB=""
if [ -n "${rkb}" ]; then
R_KB="-k ${rkb} "
fi
}

set_color ()
{
dialog --title "$msg_title" \
--menu "$msg_setcolor" 12 30 5 \
"8"    "8 bit " \
"15"   "15 bit " \
"16"   "16 bit " \
"24"   "24 bit "  2>${TF}

rcolor=`cat ${TF}`
R_CD=""
if [ -n "${rcolor}" ]; then
R_CD="-a ${rcolor} "
fi
}

set_fullscreen ()
{
dialog --title "$msg_title" \
--menu "$msg_setcolor" 12 50 5 \
"NO"    "$msg_defscr" \
"640x480"   "640x480" \
"800x600"   "800x600" \
"1024x768"   "1024x768" \
"Fullscreen" "$msg_sfs" 2>${TF}

rscreen=`cat ${TF}`
R_FS=""
if [ -n "${rscreen}" ]; then
case $rscreen in
Fullscreen)
R_FS="-f "
;;
NO)
R_FS=""
;;
*)
R_FS="-g ${rscreen}"
;;
esac
fi
}


set_tparam ()
{

choice=""
while [ "$choice" != "$msg_back" ];
do

dialog --title "$msg_title" \
--menu "$msg_rdconfig" 15 65 8 \
"Username"   "$msg_susername : $rusername" \
"Password"   "$msg_spassword : $rpx" \
"Domain"     "$msg_sdomain : $rdomain" \
"Keyboard"   "$msg_skbd : $rkb" \
"Depth"      "$msg_scolor : $rcolor" \
"Screen"     "$msg_sfsm : $rscreen" \
"$msg_back"        "$msg_exit" 2>${TF}

choice=`cat ${TF}`
if [ -z "${choice}" ]; then
choice=${msg_back}
fi

case $choice in
Username)
set_username
;;
Password)
set_password
;;
Domain)
set_domain
;;
Keyboard)
set_kb
;;
Depth)
set_color
;;
Screen)
set_fullscreen
;;
*)
;;
esac

done

rm ${TF}

tsclient_keys="${R_UN} ${R_PW} ${R_DM} ${R_KB} ${R_CD} ${R_FS}"

}





if [ ! -e /usr/local/bin/mkisofs ]; then
dialog --title " $msg_error " --msgbox "$msg_no_mkisofs" 8 52
fi

tsclient_ip="DHCP"
tsclient_mask=""
tsclient_gate=""
tsclient_tserv="10.0.0.1"
tsclient_keys="-f -a 8"

#tmsg_tc_ip="$msg_tc_ip [ $tsclient_ip ]"
#tmsg_tc_mask="$msg_tc_mask [ $tsclient_mask ]"
#tmsg_tc_gate="$msg_tc_ip [ $tsclient_ip ]"
#tmsg_tc_ip="$msg_tc_ip $tsclient_ip ]"

while [ "$RES" != "BUILD" ];
do
dialog --menu "$msg_tc_setup" 15 60 8 \
 "ip" "$msg_tc_ip : $tsclient_ip" \
 "mask" "$msg_tc_mask : $tsclient_mask" \
 "gate" "$msg_tc_gate : $tsclient_gate" \
 "tserv" "$msg_tc_tserv : $tsclient_tserv" \
 "param" "$msg_tc_tparam >" \
 "-" "" \
 "BUILD" "$msg_tc_build" 2> $tmpd

if [ $? -ne 0 ]; then
rm $tmpd
exit
fi

RES=`cat $tmpd`
rm $tmpd

case $RES in
    ip)
        set_ip
    ;;
    mask)
        set_mask
    ;;
    gate)
        set_gate
    ;;
    tserv)
        set_tserv
    ;;
    param)
        set_tparam
    ;;
    BUILD) 
	echo "ZZZZZ"
	tsc_build
	exit
    ;;
    *)
    ;;
esac
done
rm ${tmpd}
exit








