#!/bin/bash
#
CMD=$(basename $0)
CMDVER="1.0"
CMDSTR="$CMD v$CMDVER (2017/11)"

set -e -u

cfgdir="$HOME/.vnc"

desktop_id_file="$cfgdir/current-desktop-id"
desktop_id=

[ -f "$desktop_id_file" ] && {
	desktop_id=$(cat "$desktop_id_file")
	ps=$(ps axuww | grep "vnc.* :$desktop_id " | fgrep -v grep) || :

	[ "X$ps" = "X" ] && {
		echo "  warning: removing stale desktop-id file (desktop_id=$desktop_id)" >&2
		rm -f "$desktop_id_file"
		desktop_id=
	}
}

if [ "$desktop_id" != "" ]
then
	echo -e "\nkilling server :$desktop_id\n"
	vncserver -kill :$desktop_id
	rm -f "$desktop_id_file"
else
	echo -e "\nno vncserver running\n"
fi

exit 0
