#!/bin/bash
### DEPENDECIES
# - OPENSSH
# - XFREERDP
# - DMENU
##VARS TO DECLARE
RDP_USER=$USER
RDP_WIDTH=1600
RDP_HEIGHT=800
SSH_USER=$USER
SSH_KEY=/home/$USER/.ssh/id_rsa
RDP_HOSTLIST_FILE=/home/$USER/.local/rdp_hosts
SSH_HOSTLIST_FILE=/home/$USER/.local/ssh_hosts


[[ -z ${1} ]] && echo "Please provide either 'ssh' or 'rdp' as the first parameter for connection type" && exit 1
if [[ "$1" == "rdp" ]]
then
	host=$(cat $RDP_HOSTLIST_FILE | dmenu -p "Select a server or provide new hostname or IP to connect to and add to RDP list: ")
	grep $host $RDP_HOSTLIST_FILE &>/dev/null || echo $host >> $RDP_HOSTLIST_FILE
	printf "=====================\e[1;32mConnecting to $host\e[0;29m=====================\n"
	xfreerdp /log-level:ERROR /u:$RDP_USER /w:$RDP_WIDTH /h:$RDP_HEIGHT /v:$host 
elif [[ "$1" == "ssh" ]]
then
	ps aux | grep ssh-agent &>/dev/null || eval `ssh-agent -s` &>/dev/null
	grep "`ssh-add -L`" $SSH_KEY.pub &>/dev/null || ssh-add $SSH_KEY
	host=$(cat $SSH_HOSTLIST_FILE | dmenu -p "Select a server or provide new hostname or IP to connect to and add to SSH list:")
	grep $host $SSH_HOSTLIST_FILE &>/dev/null || echo $host >> $SSH_HOSTLIST_FILE
	printf "=====================\e[1;32mConnecting to $host\e[0;29m=====================\n"
	ssh "${@:2}" -l $SSH_USER -i $SSH_KEY $host
fi
