#!/bin/ash
# simple GUI for x11vnc
# Copyright (C) James Budiono 2012
# License: GNU GPL Version 3 or later
#
# Rewritten from for Fatdog64 600
#
# mode of operation
# - started when server is already running - offer to terminate
# - started when server not running - if password file exist & symlink exist, start server
#   otherwise ask
# 131127 internationalisation TODO: x11vnc-doc.txt
# 181114 step: options dialog; tigervnc compatibility
# 181124 step: bsdialog compatibility

# std localisation stanza
export TEXTDOMAIN=fatdog
. gettext.sh
# performance tweak - use "C" if there is no localisation
! [ -e $TEXTDOMAINDIR/${LANG%.*}/LC_MESSAGES/$TEXTDOMAIN.mo ] &&
! [ -e $TEXTDOMAINDIR/${LANG%_*}/LC_MESSAGES/$TEXTDOMAIN.mo ] && LANG=C

### configuration
APPTITLE="$(gettext 'x11vnc Remote Desktop Server')"
PASSWORD_FILE=$HOME/.x11vnc-password
OPTIONS_FILE=$HOME/.x11vnc-options
SYMLINK_SOURCE=$(readlink -f "$0")
SYMLINK_TARGET=$HOME/Startup/${0##*/}

# get user's option settings
set_USER_OPTIONS() {
	[ -r "$OPTIONS_FILE" ] && USER_OPTIONS=$(sed -e 's/^ \+//' -e 's/ \+$//' "$OPTIONS_FILE")
	if [ "${USER_OPTIONS%%-repeat*}" = "$USER_OPTIONS" ] \
		&& [ "${USER_OPTIONS%%-norepeat*}" = "$USER_OPTIONS" ]; then
		# by default make key autorepeat work with tigervnc vncviewer
		# (revert on-the-fly with: x11vnc -R norepeat; or permanently by running this script)
		USER_OPTIONS="-repeat $USER_OPTIONS"
	fi
}

# run the server
exec_x11vnc() {
	set_USER_OPTIONS
	eval_gettext "\${USER}'s options are stored in file \${OPTIONS_FILE}"; echo
	eval_gettext "\${USER}'s password is stored in file \${PASSWORD_FILE}"; echo
	exec x11vnc -modtweak -xkb -clear_all -capslock -nolookup -forever $USER_OPTIONS -bg -passwdfile $PASSWORD_FILE
	# keep "-passwdfile" last as server's SIGNATURE for x11vnc_is_running and stop_x11vnc.
}

# test if server is running, see SIGNATURE
x11vnc_is_running() {
	pgrep -f "x11vnc.*-passwdfile $PASSWORD_FILE" >/dev/null
}

# stop the server, see SIGNATURE
stop_x11vnc() {
	# kill just the server instance that exec_x11vnc can start
	# (allow bsdriver's x11vnc, if any, to keep running)
	pkill -f "x11vnc.*-passwdfile $PASSWORD_FILE"
	rm -f $SYMLINK_TARGET $PASSWORD_FILE
}

show_x11vnc_doc() {
	defaulttexteditor /usr/share/doc/x11vnc-doc.txt &
}

show_options_dialog() {
	set_USER_OPTIONS
	local options ret
	options=$(Xdialog --title "$APPTITLE" --stdout --help "" \
		--inputbox "$(gettext 'Edit start options. They will be saved.')
			$(gettext 'Clear the line to restore defaults.')" 0 0 "$USER_OPTIONS")
	ret=$?
	case $ret in
		0)
			options=$(echo "$options" | sed -e 's/^ \+//' -e 's/ \+$//')
			if [ "$options" ]; then
				echo "$options" > "$OPTIONS_FILE"
			else
				rm -f "$OPTIONS_FILE"
			fi
			;;
		2) show_x11vnc_doc; exec "$0" ;;
		*) return $ret ;;
	esac
	true
}

### main
if x11vnc_is_running; then
	# already running - offer to terminate or to set options
	choice=$(Xdialog --title "$APPTITLE" --stdout --help "" --menubox "$(gettext 'x11vnc is running. Would you like to stop the server?')
	$(gettext 'Please choose from the following:')" 470x159 5 \
	1 "$(gettext 'Stop Server')." \
	2 "$(gettext 'Set Options...')")
	case $? in
		0)
			case $choice in
				1) stop_x11vnc ;;
				2) show_options_dialog ;;
			esac
			;;
		2) show_x11vnc_doc; exec "$0" ;;
	esac
else
	# not running, see if we've got our symlink and password - if yes, assume we want to auto-start it.
	[ -e $SYMLINK_TARGET -a -e $PASSWORD_FILE ] && exec_x11vnc
	
	# if not, ask for details
	choice=$(Xdialog --title "$APPTITLE" --stdout --help "" --menubox "$(gettext 'Please choose from the following:')" 470x159 5 \
	1 "$(gettext 'Start the x11vnc server this one time.')" \
	2 "$(gettext 'Start the x11vnc server now and at every boot.')")
	case $? in
		0) if pass=$(Xdialog --title "$APPTITLE" --stdout --password --inputbox "$(gettext 'Enter password to connect to this server')" 0 0); then
				[ -z "$pass" ] && exec "$0"
				echo $pass > $PASSWORD_FILE
			    case $choice in # cancelling the options dialog cancels running the server
					1) show_options_dialog && exec_x11vnc ;;
					2) ln -sf $SYMLINK_SOURCE $SYMLINK_TARGET; show_options_dialog && exec_x11vnc ;;
			    esac
		   else 
				exec "$0"
		   fi
		   ;;
		2) show_x11vnc_doc; exec "$0" ;;
	esac
fi

