#!/bin/bash

PTT_CR="(c)2012-2025 PaulTT"

usage()
{
	echo "`basename $0` $PTT_CR"
	echo ""
	echo "Usage: `basename $0` [-l] [server_name]"
	echo ""
	echo "You must provide a config file with servers entries and routings,"
	echo "named ~/.config/ptt.d/pttrdesktop.conf or /etc/ptt.d/pttrdesktop.conf"
	echo "filled with lines formatted with the following fields:"
	echo "(no white spaces in between strings)"
	echo "  Mandatory Fields:"
	echo "  . option number (integer)"
	echo "  . server type (RDP | VNC | LRDP | LVNC | CMDL) (L=Local, CMDL=ask for parms)"
	echo "  . server name (string)"
	echo "  . server address (dns name, ip, or ip:port)"
	echo ""
	echo "  Optional Fields:"
	echo "  . server user (string)"
	echo "  . server password (string)"
	echo ""
	echo "  Optional, not used for Local connections:"
	echo "  . ssh server to connect to (string, usually user@server)"
	echo "  . local port to be tunnelled by ssh"
	echo ""
	echo "e.g.:"
	echo "1 RDP hell_server 6.6.6.6:3389 lucifer all_s0uls_4re_m1ne rebel@heaven 6666"
	echo ""
}

# check immediately if configuration file is present
# (newest connections conf lives in .menu.conf) 
PTT_CONFFILE=~/.config/ptt.d/pttrdesktop.conf
PTT_MENUFILE=~/.config/ptt.d/pttrdesktop.menu.conf
if [ ! -f $PTT_MENUFILE ]; then
	PTT_CONFFILE=~/.config/ptt.d/pttrdesktop.env.conf
	PTT_MENUFILE=~/.config/ptt.d/pttrdesktop.conf
fi
if [ ! -f $PTT_MENUFILE ]; then
	PTT_CONFFILE=/etc/ptt.d/pttrdesktop.conf
	PTT_MENUFILE=/etc/ptt.d/pttrdesktop.menu.conf
fi
if [ ! -f $PTT_MENUFILE -o "$1" = "-h" ]; then
	usage
	exit 1
fi

# include some env variables
PTT_RDP_CLIENT=rdesktop
PTT_RDP_ARGS=
PTT_KEYB=it
PTT_GEOMETRY=1200x768
PTT_LONGLIST=no
PTT_ROW=19
PTT_TERM=terminology
if [ -f $PTT_CONFFILE ]; then
	. $PTT_CONFFILE
fi

case $PTT_LONGLIST in
	y* | Y*)
		PTT_ROW=30
	;;
	*)
	;;
esac

if [ ! -t 1 ]; then
#if [ "x$TERMINOLOGY" == "x" ]; then
	$PTT_TERM -T `basename $0` --geometry=80x$((PTT_ROW+8)) -e "$0" &
	exit
else
	sleep 0.1
fi

PTT_GPID=

PTT_PPID=

# CTRL+C signal handler
pttrdesktop_killssh()
{
	#clear
	echo
	choice=0
	if [ "x$PTT_GPID" != "x" ]; then
		echo "Killing ssh..."
		pkill -P $PTT_PPID 
		pkill -P $PTT_GPID
	fi
}

trap pttrdesktop_killssh INT

# load servers from config file
pttrdesktop_loadconf()
{
	PTT_LINE=6
	while read data; do
		index=`echo $data | awk '{ print $1 }'`
		if [[ "x$index" != "x" && $index != *[!0-9]* ]]; then
			PTT_SERVER_TYPE[$index]=`echo $data | awk '{ print $2 }'`
			PTT_SERVER_NAME[$index]=`echo $data | awk '{ print $3 }'`
			PTT_HOST[$index]=`echo $data | awk '{ print $4 }'`
			PTT_USER[$index]=`echo $data | awk '{ print $5 }'`
			PTT_PASS[$index]=`echo $data | awk '{ print $6 }'`
			PTT_SSHD[$index]=`echo $data | awk '{ print $7 }'`
			PTT_PORT[$index]=`echo $data | awk '{ print $8 }'`
			if [ "$2" = "print" ]; then
				tput cup $PTT_LINE $1
				if [ $index -lt 10 ]; then
					echo -n " "
				fi
				echo "$index. ${PTT_SERVER_NAME[$index]}	(${PTT_SERVER_TYPE[$index]})"
				PTT_LINE=$((PTT_LINE+1))
			fi
			if [ "$PTT_SERVER" = "${PTT_SERVER_NAME[$index]}" -a "${PTT_SERVER_TYPE[$index]}" = "${PTT_LOCAL}RDP" ]; then
				choice=$index
			fi
		fi
	done < $PTT_MENUFILE
}

pttrdesktop_rdp()
{
	if [ "$2" = "sleep" ]; then	
		sleep 2
	fi
	$PTT_RDP_CLIENT -g $PTT_GEOMETRY -k $PTT_KEYB $PTT_RDP_ARGS \
		-T "${PTT_SERVER_NAME[$choice]} - ${PTT_HOST[$choice]}" \
		-u "${PTT_USER[$choice]}" -p "${PTT_PASS[$choice]}" $1
	if [ $? -eq 76 ]; then
		echo -n "Retry connection? [Y/n] "
		read reply
		case $reply in
			n* | N*)
				pttrdesktop_killssh
			;;
			y* | Y* | *)
				pttrdesktop_rdp $1 $2
			;;
		esac
	fi
}

pttrdesktop_vnc()
{
	if [ "$2" = "sleep" ]; then	
		sleep 2
	fi
	echo "${PTT_PASS[$choice]}" | xtightvncviewer -bgr233 -autopass $1
}

#########################################
while true; do

# with -l don't try to open an ssh tunnel
PTT_LOCAL=
if [ "$1" = "-l" -o "$1" = "L" ]; then
	PTT_LOCAL="L"
	shift
fi

PTT_SERVER=$1
if [ "x$PTT_SERVER" = "x" ]; then

	# clear screen
	tput clear
	# Move cursor to screen location X,Y (top left is 0,0)
	tput cup $((PTT_ROW+5)) 59
	echo "$PTT_CR"
	#
	tput cup 2 15
	tput setaf 3
	echo "       PTT rdesktop client        "
	tput sgr0
	tput cup 4 15
	# reverse
	tput rev
	echo "           SERVERS LIST           "
	tput sgr0
	#
	pttrdesktop_loadconf 15 print
	#
	tput cup $PTT_ROW 15
	echo "90. Exit"
	#
	tput bold
	tput cup $((PTT_ROW+2)) 15
	read -p "Choose server: " choice
	tput clear
	tput sgr0
	tput rc

else
	choice=90
	pttrdesktop_loadconf
fi

if [[ $choice = "90" || $choice = "q" ]]; then
	exit
fi

case "${PTT_SERVER_TYPE[$choice]}" in
	CMDL)
		typeset -u PTT_SERVER_TYPE[$choice]
		read -p "Type ([L]RDP|[L]VNC): " PTT_SERVER_TYPE[$choice]
		read -p "Host: " PTT_HOST[$choice]
		read -p "User: " PTT_USER[$choice]
		if [ "${PTT_SERVER_TYPE[$choice]}" = "RDP" ]; then
			read -p "sshd: " PTT_SSHD[$choice]
		fi
		PTT_PASS[$choice]=""
		PTT_PORT[$choice]=3456
		PTT_SERVER_NAME[$choice]="${PTT_HOST[$choice]}"
	;;
	*);;
esac

if [ "${PTT_SERVER_TYPE[$choice]}" = "LRDP" ]; then
	pttrdesktop_rdp ${PTT_HOST[$choice]}
elif [ "${PTT_SERVER_TYPE[$choice]}" = "LVNC" ]; then
	pttrdesktop_vnc ${PTT_HOST[$choice]}
else
	pttsshtunnel ${PTT_SSHD[$choice]} ${PTT_PORT[$choice]} ${PTT_HOST[$choice]} &
	PTT_PPID=$!
	PTT_GPID=`ps h -o pgid $PTT_PPID`
	if [ "${PTT_SERVER_TYPE[$choice]}" = "RDP" ]; then
		pttrdesktop_rdp localhost:${PTT_PORT[$choice]} sleep
	elif [ "${PTT_SERVER_TYPE[$choice]}" = "VNC" ]; then
		pttrdesktop_vnc localhost:${PTT_PORT[$choice]} sleep
	fi
	pttrdesktop_killssh
fi

PTT_GPID=
done;

