#!/bin/bash
#180129 sfs
#urusha 20151021
# TODO:
# * implement connections and users history

TLRDP="xfreerdp /bpp:16 -sec-nla /cert-ignore /clipboard"
TLRDP_CONFG="/etc/rdp.conf"
TLRDP_CONF="$HOME/.config/rdp.conf"
TITLE="Клиент удаленного рабочего стола XfreeRDP"

_config() {
[ -r "$TLRDP_CONFG" ] && . "$TLRDP_CONFG"
[ ! -d "$HOME/.config" ] && mkdir -p "$HOME/.config"
[ ! -f "$TLRDP_CONF" ] && touch "$TLRDP_CONF"

# Remove old version's trash
sed -i -r '/^TLRDP_ARGS="(|.*[[:space:]])-(-|f[^a-zA-Z0-9])/d' "$TLRDP_CONF"

. "$TLRDP_CONF"

# Resolution
if echo "$TLRDP_ARGS" | grep -q '/f'; then
    TLRDP_R='full screen'
elif echo "$TLRDP_ARGS" | grep -q '/multimon[^[:space:]]*'; then
    if echo "$TLRDP_ARGS" | grep -q '/monitors:0'; then
	TLRDP_R='monitor-1'
    elif echo "$TLRDP_ARGS" | grep -q '/monitors:1'; then
	TLRDP_R='monitor-2'
    else
	TLRDP_R='multi-monitor'
    fi
elif echo "$TLRDP_ARGS" | grep -q '/w\|/h'; then
    TLRDP_R="`echo "$TLRDP_ARGS" | sed -n 's,^.*/w:\([0-9]\+\)[[:space:]]\+/h:\([0-9]\+\).*$,\1x\2,p'`"
else
    TLRDP_R=""
fi
# bitmap-cache
if echo "$TLRDP_ARGS" | grep -q '\-bitmap-cache'; then
    TLRDP_C='TRUE'
else
    TLRDP_C='FALSE'
fi
# Sound
if echo "$TLRDP_ARGS" | grep -q '/sound'; then
    TLRDP_S='TRUE'
else
    TLRDP_S='FALSE'
fi
# Media
if echo "$TLRDP_ARGS" | grep -q '/drive:LocalMedia,/media /drive:LocalHome,/home'; then
    TLRDP_M='TRUE'
else
    TLRDP_M='FALSE'
fi
# ClearType
if echo "$TLRDP_ARGS" | grep -q '+fonts'; then
    TLRDP_F='TRUE'
else
    TLRDP_F='FALSE'
fi
}

_error() {
    yad --center --window-icon='/usr/share/pixmaps/server.svg' \
	--title "Ошибка" --info --text-align=center --text "$1" --buttons-layout="center" --button 'OK'
    exit
}

_connect() {
    [ -z "$TSSRV" ] && _error "Не указано имя сервера"
    if echo "$TSSRV" | grep -q '[^a-zA-Z0-9_:\.-]'; then
	_error "Неверное имя сервера"
    fi

    if [ "$TLRDP_LAST" != "$TSSRV" ]; then
        if grep -q '^TLRDP_LAST=.*$' "$TLRDP_CONF"; then
	    sed -i 's/^TLRDP_LAST=.*$/TLRDP_LAST="'"$TSSRV"'"/' "$TLRDP_CONF"
	else
	    echo "TLRDP_LAST=\"${TSSRV}\"" >> "$TLRDP_CONF"
	fi
    fi

    if [ "$TLRDP_ULAST" != "$TSUSR" ]; then
        if grep -q '^TLRDP_ULAST=.*$' "$TLRDP_CONF"; then
	    sed -i 's/^TLRDP_ULAST=.*$/TLRDP_ULAST="'"$TSUSR"'"/' "$TLRDP_CONF"
	else
	    echo "TLRDP_ULAST=\"${TSUSR}\"" >> "$TLRDP_CONF"
	fi
    fi

    $TLRDP $TLRDP_ARGS "/d:$TLRDP_DOMAIN" "/u:$TSUSR" "/v:$TSSRV"
    TLRDP_STATUS="$?"
    if [ "$TLRDP_STATUS" -gt "0" ] &&
	[ "$TLRDP_STATUS" -ne "255" ] &&
	[ "$TLRDP_STATUS" -ne "12" ] &&
	[ "$TLRDP_STATUS" -ne "11" ]; then
	_error "Подключение к '$TSSRV' не удалось"
    fi
    exit "$TLRDP_STATUS"
}

_options() {
[ -z "$OUTPUT" ] && exit

TSSRV1="`echo "$OUTPUT" | cut -d "|" -f 2`"
TSSRV2="`echo "$OUTPUT" | cut -d "|" -f 3`"
TSUSR1="`echo "$OUTPUT" | cut -d "|" -f 5`"
TSUSR2="`echo "$OUTPUT" | cut -d "|" -f 6`"
TLRDP_R_N="`echo "$OUTPUT" | cut -d "|" -f 8`"
TLRDP_C_N="`echo "$OUTPUT" | cut -d "|" -f 9`"
TLRDP_S_N="`echo "$OUTPUT" | cut -d "|" -f 10`"
TLRDP_F_N="`echo "$OUTPUT" | cut -d "|" -f 11`"
TLRDP_M_N="`echo "$OUTPUT" | cut -d "|" -f 12`"

[ -z "$TSSRV2" ] && TSSRV="$TSSRV1" || TSSRV="$TSSRV2"
[ -z "$TSUSR2" ] && TSUSR="$TSUSR1" || TSUSR="$TSUSR2"
[ -z "$TSUSR" ] && TSUSR="$USER"

if [ "$TLRDP_R" != "$TLRDP_R_N" ] ||
    [ "$TLRDP_C" != "$TLRDP_C_N" ] ||
    [ "$TLRDP_S" != "$TLRDP_S_N" ] ||
    [ "$TLRDP_F" != "$TLRDP_F_N" ] ||
    [ "$TLRDP_M" != "$TLRDP_M_N" ]; then
    if [ -z "$TLRDP_R_N" ]; then
	TLRDP_ARGS="`echo "$TLRDP_ARGS" | sed 's,/f,,g; s,/multimon[^[:space:]]*,,g; s,/monitors:[^[:space:]]*,,g; s,/w:[^[:space:]]*,,g; s,/h:[^[:space:]]*,,g;'`"
    elif [ "$TLRDP_R_N" = "full screen" ]; then
	TLRDP_ARGS="`echo "$TLRDP_ARGS" | sed 's,/f,,g; s,/multimon[^[:space:]]*,,g; s,/monitors:[^[:space:]]*,,g; s,/w:[^[:space:]]*,,g; s,/h:[^[:space:]]*,,g; s,$, /f,'`"
    elif [ "$TLRDP_R_N" = "multi-monitor" ]; then
	TLRDP_ARGS="`echo "$TLRDP_ARGS" | sed 's,/f,,g; s,/multimon[^[:space:]]*,,g; s,/monitors:[^[:space:]]*,,g; s,/w:[^[:space:]]*,,g; s,/h:[^[:space:]]*,,g; s,$, /multimon,'`"
    elif [ "$TLRDP_R_N" = "monitor-1" ]; then
	TLRDP_ARGS="`echo "$TLRDP_ARGS" | sed 's,/f,,g; s,/multimon[^[:space:]]*,,g; s,/monitors:[^[:space:]]*,,g; s,/w:[^[:space:]]*,,g; s,/h:[^[:space:]]*,,g; s,$, /multimon /monitors:0,'`"
    elif [ "$TLRDP_R_N" = "monitor-2" ]; then
	TLRDP_ARGS="`echo "$TLRDP_ARGS" | sed 's,/f,,g; s,/multimon[^[:space:]]*,,g; s,/monitors:[^[:space:]]*,,g; s,/w:[^[:space:]]*,,g; s,/h:[^[:space:]]*,,g; s,$, /multimon /monitors:1,'`"
    else
	TLRDP_R_N2="`echo "$TLRDP_R_N" | sed 's,^\([0-9]\+\)x\([0-9]\+\)$,/w:\1 /h:\2,'`"
	TLRDP_ARGS="`echo "$TLRDP_ARGS" | sed 's,/f,,g; s,/multimon[^[:space:]]*,,g; s,/w:[^[:space:]]*,,g; s,/h:[^[:space:]]*,,g; s,$, '"$TLRDP_R_N2,"`"
    fi
    if [ "$TLRDP_C_N" = "TRUE" ]; then
	TLRDP_ARGS=${TLRDP_ARGS//-bitmap-cache}
	TLRDP_ARGS="$TLRDP_ARGS -bitmap-cache"
    else
	TLRDP_ARGS=${TLRDP_ARGS//-bitmap-cache}
    fi
    if [ "$TLRDP_S_N" = "TRUE" ]; then
	TLRDP_ARGS=${TLRDP_ARGS//\/sound}
	TLRDP_ARGS="$TLRDP_ARGS /sound"
    else
	TLRDP_ARGS=${TLRDP_ARGS//\/sound}
    fi
    if [ "$TLRDP_F_N" = "TRUE" ]; then
	TLRDP_ARGS=${TLRDP_ARGS//+fonts}
	TLRDP_ARGS="$TLRDP_ARGS +fonts"
    else
	TLRDP_ARGS=${TLRDP_ARGS//+fonts}
    fi
    if [ "$TLRDP_M_N" = "TRUE" ]; then
	TLRDP_ARGS="`echo ${TLRDP_ARGS} | sed 's,/drive:[^[:space:]]*,,g'`"
	TLRDP_ARGS="$TLRDP_ARGS /drive:LocalMedia,/media /drive:LocalHome,/home"
    else
	TLRDP_ARGS="`echo ${TLRDP_ARGS} | sed 's,/drive:[^[:space:]]*,,g'`"
    fi

    # Removing spaces
    TLRDP_ARGS="${TLRDP_ARGS#"${TLRDP_ARGS%%[![:space:]]*}"}"
    TLRDP_ARGS="${TLRDP_ARGS%"${TLRDP_ARGS##*[![:space:]]}"}"
    TLRDP_ARGS="${TLRDP_ARGS//  / }"

    if grep -q '^TLRDP_ARGS=.*$' "$TLRDP_CONF"; then
	sed -i 's|^TLRDP_ARGS=.*$|TLRDP_ARGS="'"$TLRDP_ARGS"'"|' "$TLRDP_CONF"
    else
	echo "TLRDP_ARGS=\"${TLRDP_ARGS}\"" >> "$TLRDP_CONF"
    fi
fi
}

_config

#	--image='/usr/share/pixmaps/server.svg' --image-on-top \
#	--buttons-layout='center' \
case "$1" in
    "")
	OUTPUT=`yad --center --title "$TITLE" \
	--window-icon='/usr/share/pixmaps/server.svg' \
	--text="<b>$TITLE</b>" \
	--image='server' --image-on-top \
	--button 'gtk-edit!!Редактировать конфиг' \
	--button 'gtk-connect' --button 'gtk-quit' \
	--form --columns=1 \
	--field='Сервер\::CB' "${TLRDP_LAST}!${TLRDP_LIST}" \
	--field='Другой\:: ' '' \
	--field=':LBL' '' \
	--field='Пользователь@домен или:LBL' '' \
	--field='пользователь\::CB' "${TLRDP_ULAST}!${TLRDP_ULIST}" \
	--field='Другой\:: ' '' \
	--field=':LBL' '' \
	--field='Разрешение\::CB' "${TLRDP_R}!${TLRDP_RLIST}" \
	--field='Отключить битмап-кэш:CHK' "${TLRDP_C}" \
	--field='Звук:CHK' "${TLRDP_S}" \
	--field='Сглаживание шрифтов:CHK' "${TLRDP_F}" \
	--field='Общие папки:CHK' "${TLRDP_M}" \
	`
	e="$?"
	echo e=$e
	[ "$e" = 2 ] || [ "$e" = 252 ] && exit 1
	[ "$e" = 0 ] && { sudo leafpad "$TLRDP_CONFG" ; exec $0  ; exit ; }
        echo "$OUTPUT"
        _options
        _connect
	;;
    *)
	TSSRV="$1"
	_connect
	;;
esac
