#!/bin/bash

RDP_NAME="$1" ; shift 1
RDP_USER="$USER"
RDP_PASSWORD=""
RDP_APP=""
RDP_DOMAIN=""
RDP_DEBUG=""
RDP_USE_FILE=""

RDP_USER_PROMPT=""

OPTION_PASSWORD=""
OPTION_APP=""
OPTION_DOMAIN=""
OPTION_SEC=""
OPTION_SIZE="" #"/dynamic-resolution"
OPTION_PROMPT_PASS=""
OPTION_PROMPT_PASS_ARG=""
OPTION_USERNAME=""
OPTION_CERT="/cert:tofu"
OPTION_BPP="/bpp:15"

print_help() {
cat <<EOF
foxde-rdp is a wrapper around xfreerdp making it easier to work with in .desktop files and to bring remote, not always cooperating applications to your local linux desktop. Most options have a direct xfreerdp equivalent.

Usage: foxde-rdp <servername> <<options>>
	--user <user>       - set the username for the connection, defaults to \$USER {/u:…}
	--domain <domain>   - the domain to use for login {/d:…}
	--pass <password>   - set the password for the rdp connection {/p:…}
		WARNING: If you set the password this way is will be visible to other processes!
		         Do not use unless you know what you are doing!
	--sec <sec>         - Which security protocol to use {/sec:…}
	--cert              - which method to use for validating the certificate, default tofu {/cert:…}
		         
	--app <application> - sometimes applications can be exported without any desktop {/app:||…}
	--shell <shell>     - While deprected, this works quite well at exporting less cooperative applications. {/shell:||…}
	--size <width>x<height> - the viewport size in pixels to use for the connection {/size:…}
	                          if left unset and no --app or --shell is specified {/dynamic-resolution} will be set
	                          if set to dynamic the {/dynamic-resolution} flag will always be set
	--bpp <bits>        - Sets the color depth {/bpp:…}, default 15

	--user-prompt       - Spawn a kdialog to ask the user for their username, prefilled with whatever is given using --user or \$USER
	--pass-prompt       - Spawn a kdialog asking the user for their password.

	--debug             - Currently only waits 10 Seconds before exiting to get the chance to at least take a screenshot of an autoclosing terminal.
	--help              - Displays this helptext.
EOF
}

if [ "$RDP_NAME" = "--help" ] ; then
	print_help
	exit 0
fi

while [ "$#" -gt 0 ]; do
  case "$1" in
    --user) RDP_USER="$2"; OPTION_USERNAME="/u:$2"; shift 2;;
    --pass) RDP_PASSWORD="$2"; OPTION_PASSWORD="/p:$2"; shift 2;;
    --app) RDP_APP="$2"; OPTION_APP="/app:||'$2'"; shift 2;;
    --shell) RDP_APP="$2"; OPTION_APP="/shell:||'$2'"; shift 2;;
    --domain) RDP_DOMAIN="$2"; OPTION_DOMAIN="/d:$2"; shift 2;;
    --sec) OPTION_SEC="/sec:$2"; shift 1;;
    --cert) OPTION_CERT="/cert:$2"; shift 2;;
    --debug) RDP_DEBUG="y"; shift 1;;
    --is-rdp-file) RDP_USE_FILE="y"; shift 1;;
    --pass-prompt) OPTION_PROMPT_PASS="--ask-pass-prompt"; shift 1;;
    --user-prompt) RDP_USER_PROMPT="yes"; shift 1;;
    --size) OPTION_SIZE="/size:$2"; shift 2;;
    --bpp) OPTION_BPP="/bpp:$2"; shift 2;;
    --help) print_help; exit 0;;
	    
    *) printf "Unknown option: %s\nUse --help to see what this script can do.\n" "$1" >&2; exit 1;;
  esac
done

if [ -z "$RDP_APP" -a -z "$OPTION_SIZE" ] || [ "_$OPTION_SIZE" = "_/size:dynamic" ]; then
	OPTION_SIZE="/dynamic-resolution"
fi

if [ ! -z $RDP_USER_PROMPT ] ; then
	RDP_USER="$(kdialog --title "Remotedesktopverbindung" --inputbox "Nutzername für $RDP_NAME:" "$RDP_USER")"
	if [ -z "$RDP_USER" ] ; then
		exit 130
	fi
	OPTION_USERNAME="/u:$RDP_USER"
fi

if [ ! -z "$OPTION_PROMPT_PASS" ] ; then
	OPTION_PROMPT_PASS_ARG="$RDP_USER@$RDP_NAME"
fi

if [ -z "$RDP_USE_FILE" ] ; then

xargs -t -L 30 treuchtlingen-xfreerdp-gui <<EOF
$OPTION_PROMPT_PASS
$OPTION_PROMPT_PASS_ARG
$OPTION_CERT
+clipboard
$OPTION_BPP
/compression
$OPTION_SIZE
$OPTION_SEC
$OPTION_PASSWORD
$OPTION_APP
$OPTION_DOMAIN
/u:$RDP_USER 
/v:$RDP_NAME
EOF

else

xargs -t -L 30 treuchtlingen-xfreerdp-gui <<EOF
$OPTION_PROMPT_PASS
$OPTION_PROMPT_PASS_ARG
"/usr/share/rdp/$RDP_NAME.rdp"
$OPTION_USERNAME
EOF

fi

[ ! -z "$RDP_DEBUG" ] && sleep 10
