#!/usr/bin/env bash

if [ ! -f "${HOME}/.config/winapps/winapps.conf" ] && [ ! -f "${HOME}/.winapps" ]; then
	echo "You need to create a ~/.config/winapps/winapps.conf configuration. Exiting..."
	exit
fi

DIR="$(dirname "$(readlink -f "$0")")"
RUN="$(date)-${RANDOM}"

if [ ! -d "${HOME}/.local/share/winapps" ]; then
	mkdir -p "${HOME}/.local/share/winapps"
fi

RDP_SCALE=100

if [ -f "${HOME}/.config/winapps/winapps.conf" ]; then
	. "${HOME}/.config/winapps/winapps.conf"
else
	. "${HOME}/.winapps"
fi

function dprint() {
	if [ "${DEBUG}" = "true" ]; then
		echo "[${RUN}] ${1}" >> "${HOME}/.local/share/winapps/winapps.log"
	fi
}

dprint "START"

if [ -f "${HOME}/.local/share/winapps/run" ]; then
	LAST_RAN=$(stat -t -c %Y "${HOME}/.local/share/winapps/run")
	dprint "LAST_RAN:${LAST_RAN}"
	touch "${HOME}/.local/share/winapps/run"
	THIS_RUN=$(stat -t -c %Y "${HOME}/.local/share/winapps/run")
	dprint "THIS_RUN:${THIS_RUN}"
	if (( $THIS_RUN - $LAST_RAN < 2 )); then
		exit
	fi
else
	touch "${HOME}/.local/share/winapps/run"
fi

if [ -z "$(which xfreerdp)" ]; then
	echo "You need xfreerdp!"
	echo "  sudo apt-get install -y freerdp2-x11"
	exit
fi

if [ -z "${RDP_IP}" ]; then
	if [ -z "$(groups |grep libvirt)" ]; then
		echo "You are not a member of the libvirt group. Run the below then reboot."
		echo '  sudo usermod -a -G libvirt $(whoami)'
		echo '  sudo usermod -a -G kvm $(whoami)'
		exit
	fi
	if [ -z "$(virsh list |grep RDPWindows)" ]; then
		echo "RDPWindows is not running, run:"
		echo "  virsh start RDPWindows"
		exit
	fi
	RDP_IP=$(virsh net-dhcp-leases default |grep RDPWindows |awk '{print $5}')
	RDP_IP=${RDP_IP%%\/*}
fi

dprint "1:${1}"
dprint "2:${2}"
dprint "@:${@}"

MULTI_FLAG="span"
if [ "${MULTIMON}" = "true" ]; then
	MULTI_FLAG="multimon"
fi

if [ "${1}" = "windows" ]; then
	xfreerdp /d:"${RDP_DOMAIN}" /u:"${RDP_USER}" /p:"${RDP_PASS}" /v:${RDP_IP} /scale:${RDP_SCALE} /dynamic-resolution +auto-reconnect +home-drive /wm-class:"Microsoft Windows" 1> /dev/null 2>&1 &
elif [ "${1}" = "check" ]; then
	dprint "CHECK"
	xfreerdp /d:"${RDP_DOMAIN}" /u:"${RDP_USER}" /p:"${RDP_PASS}" /v:${RDP_IP} +auto-reconnect +home-drive -wallpaper /scale:${RDP_SCALE} /dynamic-resolution /${MULTI_FLAG} /app:"explorer.exe"
elif [ "${1}" = "manual" ]; then
	dprint "MANUAL:${2}"
	xfreerdp /d:"${RDP_DOMAIN}" /u:"${RDP_USER}" /p:"${RDP_PASS}" /v:${RDP_IP} +auto-reconnect +home-drive -wallpaper /scale:${RDP_SCALE} /dynamic-resolution /${MULTI_FLAG} /app:"${2}" 1> /dev/null 2>&1 &
elif [ "${1}" != "install" ]; then
	dprint "DIR:${DIR}"
	. "${DIR}/../apps/${1}/info"
	if [ -n "${2}" ]; then
		dprint "HOME:${HOME}"
		FILE=$(echo "${2}" | sed 's|'"${HOME}"'|\\\\tsclient\\home|;s|/|\\|g;s|\\|\\\\|g')
		dprint "FILE:${HOME}"
		xfreerdp /d:"${RDP_DOMAIN}" /u:"${RDP_USER}" /p:"${RDP_PASS}" /v:${RDP_IP} +auto-reconnect +home-drive -wallpaper /scale:${RDP_SCALE} /dynamic-resolution /${MULTI_FLAG} /wm-class:"${FULL_NAME}" /app:"${WIN_EXECUTABLE}" /app-icon:"${DIR}/../apps/${1}/icon.svg" /app-cmd:"\"${FILE}\"" 1> /dev/null 2>&1 &
	else
		xfreerdp /d:"${RDP_DOMAIN}" /u:"${RDP_USER}" /p:"${RDP_PASS}" /v:${RDP_IP} +auto-reconnect +home-drive -wallpaper /scale:${RDP_SCALE} /dynamic-resolution /${MULTI_FLAG} /wm-class:"${FULL_NAME}" /app:"${WIN_EXECUTABLE}" /app-icon:"${DIR}/../apps/${1}/icon.svg" 1> /dev/null 2>&1 &
	fi
fi

dprint "END"
