#!/bin/bash

# XDrdesktop is a Xdialog UI to rdesktop 1.4 or above under GNU GPL license.
#
# Created by Allan Brazute Alves because no other gui map printers and drivers. ;-)



##
## Only edit above this line if you know what is doing. ##
##

# Change Log :
#
## 1.2.1 Localization - 2006/03/16 - <Allan (at) GHSix.com.br>
#  Now it is localized with the help of gettext.
#  The first localization that is coming with is pt_BR (Braziliam Portuguese),
# it is coming in locale/pt_BR/LC_MESSAGES/ directory.
#  To know how to translate it to your language, see README file.
##
## 1.2 New functions - 2006/03/10 - <Allan (at) GHSix.com.br>
#  Now it can map all your printers and not only one like the olders. Now
# you have a new Printer Map menu where you can see the mapped printers,
# add and remove maps.
#  Changed the function of the check box on main menu, now it show or
# not the command line before connect, and the XDrdesktop always save
# options on exit now.
#  A new pop up come when you click connect that show to users that it
# is connecting and if you close that pop up it will close the rdesktop
# in background so it will not stay there waiting the timeout.
##
## 1.1 New functions - 2006/02/?? - <Allan (at) GHSix.com.br>
#
#  New options addeds on menu. Now it have all options from rdesktop.
#  A new command-line option (--c /PATH/rcfile) to a non interactive
# action with a pre-configured conf file.
#  38 new Windowze printers names added. Now it have 258 drivers names.
#
##
## 1.0 Initial release - 2005/11/23 - <Allan (at) GHSix.com.br>
#
#  Created becose there is no other GUI to rdesktop that
# have options to work with printers at the moment.
#  If someday it lack some option too, it have a way to
# add options by hand before to run rdesktop.
#  I belive XDrdesktop will never lack options becose it is bash script
# and easy to adapt.
# On web at http://sourceforge.net/projects/xdrdesktop
#
##
#
TEXTDOMAIN=xdrdesktop
TEXTDOMAINDIR=`echo $0|sed s/xdrdesktop/locale/g`
xrd_version="1.2.1"
function xrd_init()
{
if [ ! $RDESKTOP ]; then
#	RDESKTOP=`whereis -b rdesktop|cut -d":" -f2|cut -d" " -f2`
	RDESKTOP=`which rdesktop`
fi

if [ ! $DIALOG ]; then
#	DIALOG=`whereis -b Xdialog|cut -d":" -f2|cut -d" " -f2`
	DIALOG=`which Xdialog`
fi

if [ ! $DIALOG ]; then
	DEPEND="Xdialog"
	xrd_depend
fi

if [ ! $RDESKTOP ]; then
	DEPEND="rdesktop"
	xrd_depend
fi

}

function xrd_depend()
{
	echo $" * $DEPEND is necessary for GUI and was not found on your system! * "
	echo  
	echo $"Ctrl+C at any time to exit."
	echo  
	echo -n $"Install $DEPEND? (You will need root password.) [y/N] "
	read T
	if [ ."$T" != ."y" ] && [ ."$T" != ."Y" ];then
		echo $"Exiting... $T ! "
		exit 1
	else
		echo $"Install using??? "
		echo $"ie. smart install or urpmi or apt-get install or emerge or ... "

		echo  
		echo -n $"Install command: "
		read T
       		su -c "$T $DEPEND" -
		echo  
		echo $"If installation goes ok, run me again, please! "
		echo  
	fi
	exit 0
}

xtmp="/tmp/.xrd-$$.tmp"
> $xtmp
xrdrc="$HOME/.rdesktop/xdrdesktoprc"
title=" XDrdesktop "
xicon="/usr/X11R6/include/X11/pixmaps/monitor.xpm"

function xrd_load()
{
	if [ -f $xrdrc ]; then
		source $xrdrc
	else
		mkdir $HOME/.rdesktop 2> /dev/null
		$DIALOG --stdout --screen-center --title "$title" --icon "$xicon" --backtitle $"Welcome to XDrdesktop" \
		--msgbox $"This seens to be the first time you are running XDrdesktop. Be welcome!\n\n \
	       XDrdesktop is a front-end made with Xdialog for rdesktop.\n \
	       All the 3 are free softwares under GNU GPL license." 14 90
	fi
	[ ."$user" == ."" ] && user=$USER
	[ ."$hname" == ."" ] && hname=$HOSTNAME
	[ ."$cbox" == ."" ] && cbox="Off"
}

function xrd_menu()
{
	$DIALOG --stdout --screen-center --center --title "$title" --icon "$xicon" --no-tags --default-item "xrd_comm" --cancel-label $"Exit" --check $"Show command line before connect" "$cbox" --item-help \
	--menubox "$title " 33 50 17 \
	"" $"Menu" "" \
	"xrd_serv" $"Server" $"Enter IP or Domain of Terminal Service server." \
	"xrd_dup" $"Domain / User / Pass" $"Set Domain, User and Password for authentication." \
	"xrd_geom" $"Resolution" $"Set Resolution Mode." \
	"xrd_bpp" $"Color Depth" $"Set Colors Bits." \
	"xrd_disk" $"Map Disk" $"Map a path as a remote share on server." \
	"xrd_prnmenu" $"Map Printers" $"Add/Remove maps to printers." \
	"xrd_com" $"Map COM" $"Enable a serial redirection to a comport." \
	"xrd_shell" $"Shell" $"Set initall directory and program to execute." \
	"xrd_prot" $"RDP Version and Experience" $"Set RDP version and speed." \
	"xrd_sound" $"Sound" $"Play server's sound on client." \
	"xrd_key" $"Key Map" $"Select the Keyboard language layout." \
	"xrd_hname" $"Hostname" $"The name of your machine and license on server." \
	"xrd_misc" $"Other Options" $"Miscellaneous options" \
	"xrd_comm" $"Connect" $"Confirm options and connect." \
	"" "" "" \
	"xrd_about" $"About" $"XDrdesktop version $xrd_version" \
	> $xtmp
	menu=`cat $xtmp |grep -v error -m1`
	chk=`cat $xtmp |grep checked|grep -v error -m1`
	if [ ."$chk" == ."checked" ]; then
		cbox="On"
	fi
	if [ ."$chk" == ."unchecked" ]; then
		cbox="Off"
	fi
	if [ ."$menu" == ."" ]; then
		xrd=true
	fi
	eval $menu
}

function xrd_serv()
{
	$DIALOG --stdout --screen-center --left --title "$title" --editable \
       	--combobox $"Server" 10 50 "$serv" \
	`for s in $serv1; do echo $s; done` \
      	> $xtmp
	serv=`cat $xtmp|grep -v error`
	serv1="$serv $serv1"
	serv1=`for s in $serv1; do echo $s; done|sort -u|grep -m10 $`
}

function xrd_dup()
{
	[ ."$user" == ."" ] && user="$USER"
	$DIALOG --stdout --screen-center --center --title "$title" --password --separator "*" \
	--3inputsbox $"Domian / User / Password" 30 50 $"Domain" "$dom" $"User Name" "$user" $"Password (Unsafe)" "" \
      	> $xtmp
	dom=`cat $xtmp|grep -v error|cut -d"*" -f1`
	user=`cat $xtmp|grep -v error|cut -d"*" -f2`
	pass=`cat $xtmp|grep -v error|cut -d"*" -f3`
}

function xrd_prot()
{
	$DIALOG --stdout --screen-center --left --title "$title" --no-tags \
       	--radiolist $"RDP Version" 15 40 10 \
	"5" "RDP 5" $"(Default)" \
	"4" "RDP 4" "" \
      	> $xtmp
	prot=`cat $xtmp|grep -v error`
		
	if [ ."$prot" == ."5" ]; then
	$DIALOG --stdout --screen-center --left --title "$title" --no-tags \
      	--radiolist $"RDP Experience" 15 40 10 \
	"b" $"Broadband" $"128 Kbps or >" \
	"l" $"Lan" $"10/100/1000 Mbps" \
	"m" $"Modem" $"28.8 Kbps or <" \
     	> $xtmp
	rexp=`cat $xtmp|grep -v error`
	fi
}

function xrd_shell()
{
	$DIALOG --stdout --screen-center --center --title "$title" --separator "*" \
	--2inputsbox $"Directory and Shell" 20 50 $"Initial directory" "$home" $"Program to execute" "$shell" \
     	> $xtmp
	home=`cat $xtmp|grep -v error|cut -d"*" -f1`
	shell=`cat $xtmp|grep -v error|cut -d"*" -f2`
}

function xrd_geom()
{
	$DIALOG --stdout --screen-center --left --title "$title" --editable \
       	--combobox $"Resolution" 10 50 "$geom" "FullScreen" "640x480" "800x600" "1024x768" "1280x1024" "Desktop" \
      	> $xtmp
	geom=`cat $xtmp|grep -v error`
}

function xrd_bpp()
{
	[ ! $bpp ] && bpp="8"
	$DIALOG --stdout --screen-center --left --icon "$xicon" --title "$title" \
	--msgbox $" If you choose a high bpp, that is only supported when \n connects to a XP/2003 server (up to 16-Bit) or above, \n RDP compression will be disabled. \n\n 8-Bit is recommended for best performance." 14 80
	$DIALOG --stdout --screen-center --left --title "$title" \
       	--combobox $"bpp color depth (bit)" 10 30 "$bpp" "8" "15" "16" "24" "32" \
      	> $xtmp
	bpp=`cat $xtmp|grep -v error`
}

function xrd_disk()
{
	if [ ."$disk" == ."" ]; then
		disk="$HOME"
	fi
	$DIALOG --stdout --screen-center --center --title "$title" \
       	--dselect "$disk" 30 60 \
	> $xtmp
	disk=`cat $xtmp|grep -v error`
}

function xrd_sound()
{
	$DIALOG --stdout --screen-center --left --title "$title" "$btitle" --no-tags \
       	--radiolist $"Server Sounds" 15 40 10 \
	"off" $"Off" $"(Default)" \
	"local" $"Enabled" "" \
	> $xtmp
	sound=`cat $xtmp|grep -v error`
}

function xrd_hname()
{
	[ ."$hname" == ."" ] && hname="$HOSTNAME"
	$DIALOG --stdout --screen-center --left --title "$title" \
	--inputbox $" The Name of your machine (hostname) and the license name on server side.\n Change this if your 90 days license expires." 10 80 "$hname" \
	> $xtmp
	hname=`cat $xtmp|grep -v error`
}

function xrd_misc()
{
	$DIALOG --separator " " --stdout --screen-center --left --title "$title" "$btitle" --no-tags \
       	--checklist $"Other Options" 35 60 13 \
	"-b" $"Force bitmap updates" `if [ \`echo $misc|awk '{print index($0,"b")}'\` != "0" ]; then echo "On"; else echo "Off"; fi` \
	"-B" $"Use BackingStore of X-server (if available)" `if [ \`echo $misc|awk '{print index($0,"B")}'\` != "0" ]; then echo "On"; else echo "Off"; fi` \
	"-e" $"Disable encryption (French TS)" `if [ \`echo $misc|awk '{print index($0,"e")}'\` != "0" ]; then echo "On"; else echo "Off"; fi` \
	"-E" $"Disable encryption from client to server" `if [ \`echo $misc|awk '{print index($0,"E")}'\` != "0" ]; then echo "On"; else echo "Off"; fi` \
	"-m" $"Do not send motion events" `if [ \`echo $misc|awk '{print index($0,"m")}'\` != "0" ]; then echo "On"; else echo "Off"; fi` \
	"-C" $"Use private colour map (8-bit)" `if [ \`echo $misc|awk '{print index($0,"C")}'\` != "0" ]; then echo "On"; else echo "Off"; fi` \
	"-D" $"Hide window manager decorations" `if [ \`echo $misc|awk '{print index($0,"D")}'\` != "0" ]; then echo "On"; else echo "Off"; fi` \
	"-K" $"Keep window manager key bindings" `if [ \`echo $misc|awk '{print index($0,"K")}'\` != "0" ]; then echo "On"; else echo "Off"; fi` \
	"-S \"standard\"" $"Caption button size (single application mode)" `if [ \`echo $misc|awk '{print index($0,"S")}'\` != "0" ]; then echo "On"; else echo "Off"; fi` \
	"-N" $"Enable numlock syncronization" `if [ \`echo $misc|awk '{print index($0,"N")}'\` != "0" ]; then echo "On"; else echo "Off"; fi` \
	"-z" $"Enable rdp compression" `if [ \`echo $misc|awk '{print index($0,"z")}'\` != "0" ]; then echo "On"; else echo "Off"; fi` \
	"-P" $"Use persistent bitmap caching (At least 10MB of disk needed)" `if [ \`echo $misc|awk '{print index($0,"P")}'\` != "0" ]; then echo "On"; else echo "Off"; fi` \
	"-0" $"Attach to console (2003 server or newer)" `if [ \`echo $misc|awk '{print index($0,"0")}'\` != "0" ]; then echo "On"; else echo "Off"; fi` \
	> $xtmp
	misc=`cat $xtmp|grep -v error`
	echo "$misc"
}

function xrd_key
{
	$DIALOG --stdout --screen-center --left --title "$title" --editable \
       	--combobox $"Select keyboard layout" 10 50 \
	`for k in \`ls /usr/share/rdesktop/keymaps/\`; do echo $k; done ` \
	> $xtmp
	key=`cat $xtmp|grep -v error`
}


function xrd_com
{
	$DIALOG --stdout --screen-center --left --title "$title" --editable \
       	--combobox $"Select serial device to map to COM1" 10 50 \
	`for c in \`ls /dev/ttyS*\`; do echo $c; done ` \
	> $xtmp
	com1=`cat $xtmp|grep -v error`
	$DIALOG --stdout --screen-center --left --title "$title" --editable \
       	--combobox $"Select serial device to map to COM2" 10 50 \
	`for c in \`ls /dev/ttyS*\`; do echo $c; done ` \
	> $xtmp
	com2=`cat $xtmp|grep -v error`
}

function xrd_prnmenu()
{
	nop=`echo $prns|awk '{print gsub(";",$1)}'`
	$DIALOG --stdout --screen-center --center --title "$title" --icon "$xicon" --no-tags --default-item "xrd_comm" --item-help \
	--menubox $"Printers " 18 50 2 \
	"" $"Menu" "" \
	"xrd_prnadd" $"Map printer" $"Add new maps to printers." \
	"xrd_prndel" $"Unmap printer" $"Remove maps to printers." \
	"" "" "" \
	"xrd_prnmenu" $"$nop printer(s) current maped(s): " $"Printers" \
	"xrd_prnmenu" "`echo $prns|sed s/\;/\ \ \-\ \ /g`" $"Local printer=Remote printer" \
	> $xtmp
	prnmenu=`cat $xtmp |grep -v error`
	eval $prnmenu
}

function xrd_prnadd
{
	if [ ."`whereis lpstat`" != ."lpstat:" ]; then
	$DIALOG --stdout --screen-center --left --title "$title" --editable \
       	--combobox $"Local printer name" 10 50 "$lprn" \
	`for prn in \`lpstat -p|grep "printer"|cut -d" " -f2\` ; do echo $prn; done` \
	> $xtmp
	lprn=`cat $xtmp|grep -v error`
	
	if [ ."$wprn" == ."" ]; then
		wprn="Generic / Text Only"
	fi
	if [ ."$lprn" != ."" ]; then
		$DIALOG --stdout --screen-center --left --title "$title" --editable \
       		--combobox $"Printer name" 10 50 "$wprn" \
"AGFA-AccuSet 1000" \
"AGFA-AccuSet v52.3" \
"AGFA-Elan 500" \
"Apollo P-1200" \
"Apollo P2200" \
"Apple LaserWriter II NT v47.0" \
"Brother HL-1230 series" \
"Brother HL-1240" \
"Brother HL-1260" \
"Brother HL-1260e" \
"Brother HL-1440 series" \
"Brother HL-1450 series" \
"Brother HL-1470N series" \
"Brother HL-5140 series" \
"Canon Bubble-Jet BJ-200" \
"Canon Bubble-Jet BJ-200e" \
"Canon Bubble-Jet BJC-1000" \
"Canon Bubble-Jet BJC-1000SP" \
"Canon Bubble-Jet BJC-2100" \
"Canon Bubble-Jet BJC-240" \
"Canon Bubble-Jet BJC-250" \
"Canon Bubble-Jet BJC-4000" \
"Canon Bubble-Jet BJC-4200" \
"Canon Bubble-Jet BJC-4300" \
"Canon Bubble-Jet BJC-800" \
"Canon iR2200-3300 PCL5e" \
"Canon iR2200-3300 PCL6" \
"Canon iR C2100S-2100" \
"Citizen GSX-190" \
"Epson ActionLaser 1000" \
"Epson AP-2000" \
"Epson AP-5500 ESC/P 2" \
"Epson AP-5500+ ESC/P 2" \
"Epson Compatible 24 Pin" \
"Epson Compatible 9 Pin" \
"Epson DFX-5000+" \
"Epson DFX-8500" \
"EPSON EPL-3000 EpsonScript" \
"Epson EX-1000" \
"Epson FX-100" \
"Epson FX-100+" \
"Epson FX-1000" \
"Epson FX-1050" \
"Epson FX-1170" \
"Epson FX-1180" \
"EPSON FX-1180+ ESC/P" \
"Epson FX-2170" \
"Epson FX-2180" \
"EPSON FX-2190 ESC/P" \
"Epson FX-286" \
"Epson FX-286e" \
"Epson FX-80" \
"Epson FX-80+" \
"Epson FX-800" \
"Epson FX-850" \
"Epson FX-870" \
"Epson FX-880" \
"EPSON FX-880+ ESC/P" \
"EPSON FX-890 ESC/P" \
"Epson LQ-1050" \
"Epson LQ-1060+" \
"Epson LQ-1070 ESC/P 2" \
"Epson LQ-1070+ ESC/P 2" \
"Epson LQ-1170 ESC/P 2" \
"Epson LQ-2070 ESC/P 2" \
"Epson LQ-2080 ESC/P 2" \
"Epson LQ-2170 ESC/P 2" \
"Epson LQ-2550" \
"Epson LQ-500" \
"Epson LQ-570 ESC/P 2" \
"Epson LQ-570+ ESC/P 2" \
"Epson LQ-850" \
"Epson LQ-950" \
"Epson LX-100" \
"Epson LX-1050" \
"Epson LX-300" \
"Epson LX-300+" \
"Epson LX-400" \
"Epson LX-800" \
"Epson LX-810" \
"Epson SQ-850" \
"Epson Stylus 300 ESC/P 2" \
"EPSON Stylus C42 Series" \
"EPSON Stylus C43 Series" \
"EPSON Stylus C45 Series" \
"EPSON Stylus C60 Series" \
"EPSON Stylus C63 Series" \
"EPSON Stylus C64 Series" \
"Epson Stylus COLOR 440 ESC/P 2" \
"EPSON Stylus COLOR 480" \
"Epson Stylus COLOR 600 ESC/P 2" \
"Epson Stylus COLOR 640 ESC/P 2" \
"EPSON Stylus COLOR 660" \
"Epson Stylus COLOR 740 ESC/P 2" \
"EPSON Stylus COLOR 777" \
"Epson Stylus COLOR 860 ESC/P 2" \
"EPSON Stylus COLOR 880" \
"Epson Stylus COLOR ESC/P 2" \
"Epson T-750" \
"Generic IBM Graphics 9pin wide" \
"Generic / Text Only" \
"HP 2500C Series" \
"HP 2500C Series PCL5Ce" \
"HP Color LaserJet" \
"HP Color LaserJet 5550 PCL 5c" \
"HP Color LaserJet 5550 PCL 6" \
"HP Color LaserJet 8550 PCL" \
"HP D 640" \
"HP DeskJet" \
"HP DeskJet 1100C" \
"HP DeskJet 1200C/PS" \
"HP DeskJet 1220C" \
"HP DeskJet 310" \
"HP DeskJet 340" \
"HP DeskJet 350" \
"hp deskjet 3500 series" \
"hp deskjet 3820 series" \
"HP DeskJet 400" \
"HP DeskJet 400 (Monochrome)" \
"HP DeskJet 500" \
"HP DeskJet 500C" \
"HP DeskJet 520" \
"HP DeskJet 550C" \
"hp deskjet 5550 series" \
"HP DeskJet 560C" \
"HP DeskJet 600" \
"HP DeskJet 600 (Monochrome)" \
"HP DeskJet 610C" \
"hp deskjet 6122 series" \
"HP DeskJet 640C/642C/648C" \
"hp deskjet 640c series" \
"hp deskjet 656c series" \
"HP DeskJet 660C" \
"HP DeskJet 670C" \
"HP DeskJet 680C" \
"HP DeskJet 682C" \
"HP DeskJet 690C" \
"HP DeskJet 692C" \
"HP DeskJet 694C" \
"HP DeskJet 695C" \
"HP DeskJet 710C" \
"HP DeskJet 720C" \
"HP DeskJet 810C" \
"HP DeskJet 820Cse" \
"HP DeskJet 820Cxi" \
"HP DeskJet 840C/841C/842C/843C" \
"hp deskjet 840c series" \
"HP DeskJet 850C" \
"HP DeskJet 870C" \
"HP DeskJet 890C" \
"HP DeskJet 895Cse" \
"HP DeskJet 895Cxi" \
"HP DeskJet 930C/932C/935C" \
"hp deskjet 930c series" \
"HP DeskJet 950C/952C/959C" \
"hp deskjet 950c series" \
"hp deskjet 960c" \
"HP DeskJet 970Cxi" \
"HP LaserJet" \
"hp LaserJet 1000" \
"hp LaserJet 1010" \
"hp LaserJet 1012" \
"hp LaserJet 1015" \
"HP LaserJet 1015 PCL 5e" \
"HP LaserJet 1100 (MS)" \
"hp LaserJet 1150 Driver" \
"hp LaserJet 1150 PCL 5e" \
"hp LaserJet 1160 PCL 5e" \
"HP LaserJet 1200 Series PCL" \
"HP LaserJet 1200 Series PS (MS)" \
"HP LaserJet 1220 Series PCL" \
"HP LaserJet 1220 Series PS (MS)" \
"hp LaserJet 1300 PCL 5e" \
"hp LaserJet 1300 PCL 6" \
"hp LaserJet 1300 PS" \
"hp LaserJet 1320 PCL 5e" \
"hp LaserJet 1320 PCL 6" \
"HP LaserJet 2000" \
"HP LaserJet 2100" \
"HP LaserJet 2100 PCL6" \
"HP LaserJet 2100 Series PS" \
"HP LaserJet 4" \
"HP LaserJet 4000 Series PCL6" \
"HP LaserJet 4000 Series PS" \
"HP LaserJet 4050 Series PCL" \
"HP LaserJet 4050 Series PCL6" \
"HP LaserJet 4050 Series PS" \
"HP LaserJet 4100 Series PCL" \
"HP LaserJet 4100 Series PS" \
"HP LaserJet 4/4M PS" \
"HP LaserJet 4L" \
"HP LaserJet 4M" \
"HP LaserJet 4P" \
"HP LaserJet 4Si" \
"HP LaserJet 5" \
"HP LaserJet 5000 Series PCL6" \
"HP LaserJet 5L" \
"HP LaserJet 5M" \
"HP LaserJet 5MP" \
"HP LaserJet 5P" \
"HP LaserJet 5P/5MP (HP)" \
"HP LaserJet 5Si/5Si MX PS" \
"HP LaserJet 6L" \
"HP LaserJet 6P" \
"HP LaserJet 8150 Series PS" \
"HP LaserJet III" \
"HP LaserJet IIIP" \
"HP LaserJet IIP" \
"HP LaserJet Plus" \
"HP LaserJet Series II" \
"HP OfficeJet G55" \
"HP OfficeJet V40" \
"HP PSC 500" \
"HP PSC 750" \
"HP PSC 950" \
"IBM Proprinter II" \
"Lexmark 1020 Color Jetprinter" \
"Lexmark E210" \
"Lexmark E230" \
"Lexmark Optra" \
"Lexmark Optra E" \
"Lexmark Optra E310 (MS)" \
"Lexmark Optra E312L (MS)" \
"Lexmark Optra E312 (MS)" \
"Lexmark Optra E+ (MS)" \
"Lexmark Optra Ep" \
"Lexmark Optra R Plus Series" \
"Lexmark Optra R Series" \
"Lexmark Optra T614" \
"Lexmark ValueWriter 600" \
"Lexmark Z12 Color Jetprinter" \
"Lexmark Z22-Z32 Color Jetprinter" \
"Microsoft Shared Fax Driver" \
"Microsoft Shared Fax Driver" \
"Microsoft Shared Fax Driver" \
"Minolta Pi3500" \
"Okidata ML 320" \
"OKIDATA ML320/1 TURBO" \
"Okidata ML 320 Turbo/D (DEC)" \
"Okidata ML 320 Turbo/D (IBM)" \
"Okidata ML 321-IBM" \
"Okidata ML 321 Turbo/D (IBM)" \
"Okidata ML 521-IBM" \
"Oki ML 320 Elite (IBM)" \
"Oki ML 320 Elite (Microline)" \
"Oki ML 321 Elite (Microline)" \
"Olivetti DM 209 L" \
"Olivetti DM 95" \
"Olivetti JP 192 (Color)" \
"Panasonic KX-P2180" \
"RICOH Aficio AP2610N PCL 6" \
"RICOH Aficio AP2610 PCL 6" \
"RICOH LASER AP2610N PCL 5e" \
"RICOH LASER AP2610N PCL 6" \
"RICOH LASER AP2610N PS" \
"RICOH LASER AP2610 PCL 5e" \
"RICOH LASER AP2610 PCL 6" \
"RICOH LASER AP2610 PS" \
"SHARP AL-1645CS" \
"Xerox DocuColor 2006" \
"Xerox DocuPrint 4512 PS" \
"Xerox WC M15 Series PCL 6" \
"Xerox WorkCentre 415/Pro 420 XL" \
"Zebra  S600" \
"Zebra  TLP2844" \
"Other..." \
	> $xtmp
	wprn=`cat $xtmp|grep -v error`

	if [ ."$wprn" == ."Other..." ]; then
		$DIALOG --stdout --screen-center --left --title "$title" \
       		--inputbox $"The WinDriver Name of Printer (ie. HP DeskJet 850C) " 10 60 \
		> $xtmp
		wprn=`cat $xtmp|grep -v error`
	fi
fi
	else
		$DIALOG --stdout --screen-center --left --icon "$xicon" --title "$title" \
		--infobox $" I can't find lpstat command on your system. \n lpstat is a command that show printers status. \n\n Are you sure that you have a work printer configured here? " 10 80 10000
	fi

	if [ ."$lprn" != ."" ] && [ ."$wprn" != ."" ]; then
	       	prns="$lprn=$wprn;$prns"
	fi
}

function xrd_prndel()
{
	nop=`echo $prns|awk '{print gsub(";",$1)}'`
	if [ ."$nop" != ."0" ]; then
		$DIALOG --stdout --screen-center --left --title "$title" --editable \
	       	--ok-label $"Remove" --combobox $"Remove maped printer" 10 50 "" \
		`for each in \`seq 1 $nop\`; do echo $prns|cut -d";" -f$each|tr " " "\240"; done` \
	      	> $xtmp
		prndel=`cat $xtmp|grep -v error|tr "\240" " "`
		if [ ."$prndel" != ."" ]; then
			prns=`echo $prns|sed s#"$prndel;"##g`
		fi
	else
		$DIALOG --stdout --screen-center --left --icon "$xicon" --title "$title" \
		--infobox $" No printers current maped! " 10 50 10000
	fi
}

function xrd_comm()
{
	opts="-T $title $serv"
	[ ."$misc" != ."" ] && opts="$misc $opts"
	[ ."$key" != ."" ] && opts="-k '$key' $opts"
	[ ."$prot" != ."" ] && opts="-$prot $opts"
	[ ."$rexp" != ."" ] && opts="-$rexp $opts"
	[ ."$sound" != ."" ] && opts="-r sound:$sound $opts"
	[ ."$bpp" != ."" ] && opts="-a $bpp $opts"
	[ ."$home" != ."" ] && opts="-c '$home' $opts"
	[ ."$shell" != ."" ] && opts="-s '$shell' $opts"
	[ ."$dom" != ."" ] && opts="-d '$dom' $opts"
	[ ."$user" != ."" ] && opts="-u '$user' $opts"
	[ ."$pass" != ."" ] && opts="-p '$pass' $opts"
	[ ."$hname" != ."" ] && opts="-n '$hname' $opts"
	if [ ."$disk" != ."" ]; then
		share=`echo $disk|awk '{print gsub("/",$1)}'`
		share=`echo $disk|cut -d"/" -f$share`
		opts="-r clientname='$hname' -r disk:'$share=$disk' $opts"
	fi
	if [ ."$prns" != ."" ]; then
		nop=`echo $prns|awk '{print gsub(";",$1)}'`
		for each in `seq 1 $nop`; do
			p=`echo $prns|cut -d";" -f$each`
			opts="-r printer:'$p' $opts"
		done
	fi
	if [ ."$com2" != ."" ]; then
	       	opts="-r comport:'COM2=$com2' $opts"
	fi
	if [ ."$com1" != ."" ]; then
	       	opts="-r comport:'COM1=$com1' $opts"
	fi
	case "$geom" in
		FullScreen)
		opts="-f $opts"
		;;
		Desktop)
		opts="-g workarea -D $opts"
		;;
		*)
		if [ ."$geom" != ."" ]; then
			opts="-g '$geom' $opts"
		fi
		;;
	esac

	if [ ."$cbox" == ."On" ]; then
		$DIALOG --stdout --screen-center --left --title "$title" \
		--inputbox "rdesktop" 10 150 "$opts" \
		> $xtmp
		opts=`cat $xtmp|grep -v error`
	fi

	if [ ."$opts" != ."" ] && [ ."$serv" != ."" ]; then
		eval "$RDESKTOP $opts 2> $xtmp;killall -15 Xdialog" &
	        $DIALOG --stdout --screen-center --center --title "$title" --icon "$xicon" --no-buttons \
	        --infobox $"Connecting to $serv... " 4 50 12000000
		case $? in
#			0)
#			echo "Yes chosen.";;
#			1)
#			echo "No chosen.";;
			255)
			killall -15 rdesktop;
		esac
		xdpid=`pidof Xdialog`
		if [ ."$xdpid" != ."" ]; then
			kill -15 $xdpid
		fi
		ret=`cat $xtmp|grep -v "xdrdesktop:"`
		if [ ."$ret" != ."" ] && [ ."$c1" != ."--c" ]; then
			$DIALOG --stdout --screen-center --left --title "$title" --no-cancel --beep \
			--textbox "$xtmp" 40 100
		fi
	else
		if [ ."$serv" == ."" ]; then
		       	if [ ."$c1" != ."--c" ]; then
				$DIALOG --stdout --screen-center --title "$title" --icon "$xicon" "$title"  \
				--infobox $"You need to tell me what server you whant to connect... " 8 70 10000
			fi
			xrd_serv
			if [ ."$serv" != ."" ]; then
				xrd_comm
			fi
		fi
	fi
}

function xrd_quit()
{
	echo "serv=\"$serv\""|sed s/\'//g > $xrdrc
	echo "serv1=\"$serv1\""|sed s/\'//g >> $xrdrc
	echo "dom=\"$dom\""|sed s/\'//g >> $xrdrc
	echo "user=\"$user\""|sed s/\'//g >> $xrdrc
	echo "home=\"$home\""|sed s/\'//g >> $xrdrc
	echo "hname=\"$hname\""|sed s/\'//g >> $xrdrc
	echo "shell=\"$shell\""|sed s/\'//g >> $xrdrc
	echo "key=\"$key\""|sed s/\'//g >> $xrdrc
	echo "prot=\"$prot\"" >> $xrdrc
	echo "rexp=\"$rexp\"" >> $xrdrc
	echo "geom=\"$geom\""|sed s/\'//g >> $xrdrc
	echo "bpp=\"$bpp\"" >> $xrdrc
	echo "sound=\"$sound\"" >> $xrdrc
	echo "disk=\"$disk\""|sed s/\'//g >> $xrdrc
	echo "prns=\"$prns\"" >> $xrdrc
	echo "com1=\"$com1\""|sed s/\'//g >> $xrdrc
	echo "com2=\"$com2\""|sed s/\'//g >> $xrdrc
	echo "misc=\"$misc\"" >> $xrdrc
	echo "cbox=\"$cbox\"" >> $xrdrc
	rm -f $xtmp
	$DIALOG --stdout --screen-center --center --title "$title" --icon "$xicon" --no-buttons \
	--infobox $"Bye! " 4 20 500
	exit 0
}

function xrd_about()
{
	$DIALOG --stdout --screen-center --center --title "$title" --icon "$xicon" --no-close \
	--msgbox $" XDrdesktop version $xrd_version \n Written by Allan Brazute Alves <allan(at)ghsix.com.br>. \n\n For more details see README or go to \n http://sourceforge.net/projects/xdrdesktop" 13 65
}

xrd_init
xrd_load

case "$1" in
'--c')
	c1=$1
	source $2
	xrd_comm
	exit
	;;
'--help')
	echo  
	echo "$title $xrd_version"
	echo  
	echo $"Options: "
	echo 
	echo $"--c [/PATH/rcfile]	Non interactive mode with alternative pre-configured rcfile. "
	echo $"--help			This help. "
	echo  
	exit
	;;
esac

while [ ! $xrd ]; do
xrd_menu
done
xrd_quit
