#!/bin/sh
# PURPOSE:	This tool recreates vnc processes if host was rebooted
# AUTHOR:	Reinhard Hanuschik
# DATE: 	January 2013
# VERSION: 	1.0 -- original version
#		1.0.1- source .bashrc instead of .qcrc (after CENTOS migration) (2018-08-01)
# COMMENT:	
# =============================================================================

TOOL_VERSION="1.0.1"
#source ~/.qcrc
#source ~/.dfosrc
source ~/.bashrc

USAGE="dfosVNCserver [-h|-v] [-K] tool to check for configured vnc sessions (create them if missing); designed to run as cronjob (without arguments)."
USAGE1="  -K  kill all running vnc sessions (dialogue)"
USAGE2="Call 'ps -aef | grep vnc | grep desktop | grep $USER | grep -v grep' to see all vnc sessions"
KILL_ALL=NO

while getopts hvK OPTION
do
	case "$OPTION" in
	 h ) echo $USAGE; echo $USAGE1; echo $USAGE2; exit 0;;
         v ) echo "$TOOL_VERSION"; exit 0 ;;
	 K ) KILL_ALL=YES ;;
	 * ) echo $USAGE; echo $USAGE1; echo $USAGE2; exit 0;;
	esac
done

if [ ! -s $DFO_CONFIG_DIR/config.dfosVNCserver ]
then
	echo "***ERROR: no config.dfosVNCserver existing."
	exit -1
fi

USER=`grep "^USER" $DFO_CONFIG_DIR/config.dfosVNCserver | awk '{print $2}'`

rm -f $TMP_DIR/vnc_sessions*
grep "^VNC" $DFO_CONFIG_DIR/config.dfosVNCserver | awk '{print $2,$3,$4}' > $TMP_DIR/vnc_sessions
ps -aef | grep Xvnc | grep $USER | grep -v grep | grep desktop | awk '{print $2,$11}' | sort -k2,2 > $TMP_DIR/vnc_sessions_running

for SESS in `cat $TMP_DIR/vnc_sessions | awk '{print $1}'`
do
	CHECK_RUNNING=`grep "${HOSTNAME}${SESS}$" $TMP_DIR/vnc_sessions_running`
	if [ "Q$CHECK_RUNNING"  = Q ]
	then
		echo "${HOSTNAME}${SESS} missing"
		echo "${HOSTNAME}${SESS}" >> $TMP_DIR/vnc_sessions_missing
	fi
done

for SESS in `cat $TMP_DIR/vnc_sessions_running | awk '{print $2}'`
do
	SESS1=`echo $SESS | sed "s/^.*://"`
	CHECK_CONFIG=`grep  ":${SESS1}[[:space:]]" $DFO_CONFIG_DIR/config.dfosVNCserver | head -1`
	if [ "Q$CHECK_CONFIG" = Q ]
	then
		echo "$SESS owned by $USER is not configured."
		echo "You can kill it by calling 'vncserver -kill :$SESS1'."
	else
		echo ":${SESS1}" >> $TMP_DIR/vnc_sessions_tobe_killed
	fi
done

# -K: kill all configured ones
if [ $KILL_ALL = YES ] && [ -s $TMP_DIR/vnc_sessions_tobe_killed ]
then
	echo "Do you really want to kill all configured and running sessions (Y/N) (N)?"
	read KILL
	if [ Q$KILL != QY ]
	then
		echo "Think about it."
		exit
	else
		for SESS in `cat $TMP_DIR/vnc_sessions_tobe_killed`
		do
			vncserver -kill $SESS
			echo "${HOSTNAME}$SESS killed"
		done
		exit
	fi
elif [ $KILL_ALL = YES ] && [ ! -s $TMP_DIR/vnc_sessions_tobe_killed ]
then
	echo "Nothing to kill."
	exit
fi

# if all configured ones are running, exit
if [ ! -s $TMP_DIR/vnc_sessions_missing ]
then
	echo "All configured sessions running ok:"
	ps -aef | grep vnc | grep desktop | grep $USER | grep -v grep
	exit
fi

# missing session(s) found: delete surviving ones and recreate all from scratch

# remove remaining sessions
for SESS in `cat $TMP_DIR/vnc_sessions | awk '{print $1}'`
do
	vncserver -kill $SESS
done

# remove lock files which might have survived the machine reboot; don't worry, this 
# removes only your files and not the ones of your competitors
rm -f /tmp/.X*-lock
rm -f /tmp/.X11-unix/X*
	
# create new sessions as configured
rm -f $TMP_DIR/vnc_mail
for SESS in `cat $TMP_DIR/vnc_sessions | awk '{print $1}'`
do
	DEPTH=`grep "${SESS}[[:space:]]" $TMP_DIR/vnc_sessions | awk '{print $2}'`
	GEOM=`grep  "${SESS}[[:space:]]" $TMP_DIR/vnc_sessions | awk '{print $3}'`

	vncserver $SESS -depth $DEPTH -geometry $GEOM
	echo "vnc session ${HOSTNAME}$SESS created" >> $TMP_DIR/vnc_mail
done

mail -s "VNC: new sessions created" $OP_ADDRESS <$TMP_DIR/vnc_mail

exit
