#! /bin/sh
#
#------------------------------------------------------------------------------
# ::do_not_edit::
# ::maintainer::
#
# mnt-vncserver
#
#	starts vncserver for root, if enabled (see ENABLED in
#	/etc/default/mnt-vncserver)
#
### BEGIN INIT INFO
# Provides:          mnt-vncserver
# Required-Start:    $syslog $network $local_fs $remote_fs 
# Required-Stop:     $syslog $network $local_fs $remote_fs
# Should-Start:      
# Should-Stop:       
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts Vncserver for root at boot
# Description:       Starts Vncserver for root at boot
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON="Xvnc4"
NAME="mnt-vncserver"
DESC="Xvnc4 Server"

START_CMD="/usr/bin/ku-vncserver-start"
STOP_CMD="/usr/bin/ku-vncserver-stop"

. /lib/lsb/init-functions

test -x "$START_CMD" || exit 0
test -x "$STOP_CMD" || exit 0

set -e -u

# parms
#
ENABLED=false
VNCSERVER_USERS=

[ -f /etc/default/$NAME ] && {
	. /etc/default/$NAME
}

sanity_checks()
{
  	[ "X$ENABLED" = "Xtrue" -o "X$ENABLED" = "yes" ] || {
		echo "disabled in /etc/default/$NAME"
		return 1
	}
	[ "X$VNCSERVER_USERS" = "X" ] && {
		echo "no users defined in /etc/default/$NAME"
		return 1
	}
	return 0
}


case "$1" in
  start)
	log_action_begin_msg "starting $DESC "

	out=`sanity_checks` || {
		log_warning_msg " $out"
		exit 1
	}

	stat=0
	outerr=
  	for user in $VNCSERVER_USERS
	do
		if out=`su - $user -c "$START_CMD" 2>&1`
		then
			log_action_cont_msg " $user "
		else
			log_action_cont_msg " $user(failed) "
			outerr="$outerr\nUSER $user\n$out\n"
			stat=1
		fi
	done
	log_action_end_msg $stat
	[ "X$outerr" != "X" ] && log_warning_msg `/bin/echo -e "$outerr"`
	exit $stat
	;;
  stop)
	log_action_begin_msg "stopping $DESC "

	out=`sanity_checks` || {
		log_warning_msg " $out"
		exit 1
	}

	stat=0
	outerr=
  	for user in $VNCSERVER_USERS
	do
		if out=`su - $user -c "$STOP_CMD" 2>&1`
		then
			log_action_cont_msg " $user "
		else
			log_action_cont_msg " $user(failed) "
			outerr="$outerr\nUSER $user\n$out\n"
			stat=1
		fi
	done
	log_action_end_msg $stat
	[ "X$outerr" != "X" ] && log_warning_msg `/bin/echo -e "$outerr"`
	exit $stat
	;;
  reload|restart|force-reload)
	$0 stop
	exec $0 start
	;;
  status)
  	echo -n "checking $DESC: "
	sanity_checks || exit 1
	echo

	stat=0
  	for user in $VNCSERVER_USERS
	do
		tag="  $NAME (user $user)"
  		vnc_home=`getent passwd $user | cut -d':' -f6`/.vnc || {
			echo "$tag: not found" >&2
			stat=1
			continue
		}
		uname=`uname -n`
		id_file="$vnc_home/current-desktop-id"
		if [ ! -f "$id_file" ]
		then
			echo "$tag: '$id_file' not exists, not running" >&2
			stat=1
			continue
		fi
		current_id=`cat "$id_file"`

  		pid_file="$vnc_home/$uname:$current_id.pid"
		if [ ! -f "$pid_file" ]
		then
			echo "$tag: '$pid_file' not exists, not running" >&2
			stat=1
			continue
		fi

		PID=`cat "$pid_file"`
		if ps -p $PID >/dev/null 2>/dev/null
		then
			echo "$tag: running, pid=$PID" >&2
		else
			echo "$tag: not running" >&2
			stat=1
		fi
	done
	exit $stat
	;;
  *)
	/bin/echo -e "\nusage: $0 { start | stop | restart | reload | force-reload | status }\n" >&2
	exit 1
	;;
esac

exit 0
