#!/bin/sh

# GLobal settings
BASEDIR=/usr/share/gcaldaemon
CLASSPATH=$(echo "$BASEDIR"/lib/*.jar | sed "s|\s$BASEDIR|:$BASEDIR|g")
CLASS="org.gcaldaemon.standalone.Main"

# Local config
USERDIR=~/.gcaldaemon
CONFIGFILE="$USERDIR/conf/gcal-daemon.cfg"
PIDFILE="$USERDIR/gcaldaemon-running.pid"

# Daemon
CMD=$(which java)
ARGS="-Xmx256m -cp $CLASSPATH $CLASS $CONFIGFILE"

# Will be used by program
PID=0

# Exit silently if package is no longer installed
if [ ! -x "$CMD" ]; then
	echo "ERROR: No java installed or not in PATH variable."
	exit 1
fi

is_running() {
	if [ ! -r "$PIDFILE" ]; then
		PID=0
		return 0
	fi

	PID=$(cat "$PIDFILE")
	ps -p "$PID" > /dev/null 2>&1 && return 1
		
	# PID file exists, but process not running
	# --> Delete PID file
	rm -f "$PIDFILE" 2> /dev/null
	
	PID=0
	return 0
}

is_configured() {
	CHANGED=$(diff "$BASEDIR/conf/gcal-daemon.cfg" "$CONFIGFILE" 2> /dev/null)
	
	if [ -z "$CHANGED" ]; then
		return 0
	fi
	
	return 1	
}

do_configure_warn() {
	do_create_autostart
	do_create_folders

	is_configured
	if [ $? -eq 0 ]; then
		show_warning
	fi
}

do_configure_no_warn() {
	do_create_autostart
	do_create_folders
}

do_create_autostart() {
	# Do not copy!
	
	# The file, e.g. kde4-disabled.desktop will be copied to
	# the directory to enable/disable the starter

	# KDE 4
	if [ -d ~/.kde4 ]; then
		mkdir -p ~/.kde4/Autostart/
	fi

	# KDE 3.5
	if [ -d ~/.kde ]; then
		mkdir -p ~/.kde/Autostart/
	fi
	
	# GNOME
	if [ -d ~/.config ]; then
		mkdir -p ~/.config/autostart/
	fi	
}

do_create_folders() {
		if [ ! -r "$USERDIR" ]; then
			mkdir "$USERDIR" || exit 1
		fi

		if [ ! -r "$USERDIR/calendars" ]; then
			mkdir "$USERDIR/calendars" || exit 1
		fi

		if [ ! -r "$USERDIR/conf" ]; then		
			cp -R "$BASEDIR/conf" "$USERDIR/conf" || exit 1
			
			# Replace $USERDIR in config file 
			sed "s|\$USERDIR|$USERDIR|" "$BASEDIR/conf/gcal-daemon.cfg" > "$CONFIGFILE"
		fi
}

do_set_autostart() {
	MODE=$1
	DESKTOP=$2
	ENVDIR=$3
	AUTODIR=$4

	if [ "$MODE" = "enable" ]; then
		echo -n "Enabling $DESKTOP autostart ... "
		if [ ! -d $ENVDIR ]; then
			echo "ERROR: $DESKTOP is not installed or has not been run as user '`whoami`'."
		else		
			cp -f "$BASEDIR/autostart/$DESKTOP-enabled.desktop" \
				$AUTODIR/gcaldaemon.desktop 2> /dev/null
			
			if [ -f $AUTODIR/gcaldaemon.desktop ]; then
				echo "ok"
			else
				echo "ERROR. Unable to copy to $AUTODIR/gcaldaemon.desktop"
			fi
		fi	
	
	elif [ "$1" = "disable" ]; then
		echo -n "Disabling $DESKTOP autostart ... "
		
		if [ ! -f $AUTODIR/gcaldaemon.desktop ]; then
			echo "ok"
		else 
			cp -f "$BASEDIR/autostart/$DESKTOP-disabled.desktop" \
				$AUTODIR/gcaldaemon.desktop 2> /dev/null
		
			if [ -f $AUTODIR/gcaldaemon.desktop ]; then
				echo "ok"
			else
				echo "ERROR. Unable to copy to $AUTODIR/gcaldaemon.desktop"
			fi
		fi
	fi
}

show_warning() {
	echo "----------------------------------------------------------------------------"
	echo "WARNING: Installation not yet complete. Please configure your personal"
	echo "         GCALDaemon using a normal editor or the config editor GUI."
	echo ""
	echo "* Config file:         $CONFIGFILE"
	echo "* Config editor, type: $0 config"
	echo ""
	echo "Documentation: http://gcaldaemon.sourceforge.net/usage.html"
	echo "----------------------------------------------------------------------------"
}

case "$1" in
install)
	do_configure_no_warn 
	exit 0
	;;
	
start)
	do_configure_warn

	echo -n "Starting GCALDaemon ... "	
	
	is_running
	if [ $? -eq 1 ]; then
		#PID=$(cat "$PIDFILE")
		echo "GCALDaemon is already running as PID $PID"
		
		exit 1
	fi

	start-stop-daemon \
		--start --background --quiet \
		--make-pidfile --pidfile "$PIDFILE" \
		--exec "$CMD" -- $ARGS \
		 || exit 1
		 
	echo "ok"
	;;
	
stop)
	do_configure_warn

	echo -n "Stopping GCALDaemon ... "

	is_running
	if [ $? -eq 1 ]; then
		start-stop-daemon \
			--stop --quiet \
			--retry=TERM/30/KILL/5 \
			--pidfile "$PIDFILE" \
			> /dev/null 2>&1 \
			 || exit 1
	
		# Remove PID file
		rm -f "$PIDFILE"
			 
		echo "ok"
		exit 0
	fi
	
	echo "Not running"
	exit 0
	;;
	
sync-now)
	do_configure_warn

	echo -n "Starting sync ... "	
	WAS_RUNNING=0
	
	is_running
	if [ $? -eq 1 ]; then
		WAS_RUNNING=1
		
		echo "Daemon running, trying to stop."
		$0 stop
		
		if [ $? -gt 0 ]; then
			echo "ERROR: Unable to stop daemon. Sync not possible."
			exit 1
		fi
	fi

	if [ $WAS_RUNNING -eq 1 ]; then
		echo -n "Starting sync ... "
	fi
	
	start-stop-daemon \
		--start --background --quiet \
		--make-pidfile --pidfile "$PIDFILE" \
		--exec "$CMD" -- $ARGS runonce \
		 || exit 1
	
	is_running
	if [ $PID -eq 0 ]; then
		echo "ok"
		echo "Sychronizing ... ok"
	else	
		echo "ok"
		echo -n "Sychronizing ..."
		
		while [ $PID -ne 0 ]; do
			echo -n "."
			
			sleep 1
			is_running
		done
		
		echo " ok"
	fi
		
	if [ $WAS_RUNNING -eq 1 ]; then
		$0 start
	fi	
	;;
	
config)
	do_configure_no_warn

	echo -n "Opening config editor ... "		
	WAS_RUNNING=0
	
	is_running
	if [ $? -eq 1 ]; then
		WAS_RUNNING=1
		
		echo "Daemon running, trying to stop."
		$0 stop
		
		if [ $? -gt 0 ]; then
			echo "ERROR: Unable to stop daemon. Sync not possible."
			exit 1
		fi
	fi
	
	if [ $WAS_RUNNING -eq 1 ]; then
		echo -n "Opening config editor ... "		
	fi
	
	start-stop-daemon \
		--start --background --quiet \
		--make-pidfile --pidfile "$PIDFILE" \
		--exec "$CMD" -- $ARGS configeditor

	is_running
	if [ $PID -eq 0 ]; then
		echo "ok, already closed again."
	else	
		echo "ok"
		echo -n "Editor opened, now waiting until closed ... "
		
		while [ $PID -ne 0 ]; do
			sleep 1
			is_running
		done
		
		echo "ok"
	fi
		
	if [ $WAS_RUNNING -eq 1 ]; then
		$0 start
	fi	
	;;
	
enable)
	do_configure_no_warn
	
	if [ "$2" = "kde" -o "$2" = "kde4" ]; then
		do_set_autostart "enable" "$2" ~/.$2 ~/.$2/Autostart

	elif [ "$2" = "gnome" ]; then
		do_set_autostart "enable" "$2" ~/.config ~/.config/autostart

	elif [ "$2" = "all" ]; then
		do_set_autostart "enable" "kde" ~/.kde ~/.kde/Autostart
		do_set_autostart "enable" "kde4" ~/.kde4 ~/.kde4/Autostart
		do_set_autostart "enable" "gnome" ~/.config ~/.config/autostart

	elif [ -z "$2" ]; then
		echo "Not yet supported, please specify desktop environment."
		exit 1
		
	else
		echo "ERROR: Unsupported desktop environment '$2'."
		echo ""
		echo "To activate the autostart manually, read the documentation of your desktop"
		echo "environment and run '$0 start' to launch the daemon."
		exit 1
	fi
		
	;;
	
disable)
	do_configure_no_warn
	
	if [ "$2" = "kde" -o "$2" = "kde4" ]; then
		do_set_autostart "disable" "$2" ~/.$2 ~/.$2/Autostart

	elif [ "$2" = "gnome" ]; then
		do_set_autostart "disable" "$2" ~/.config ~/.config/autostart

	elif [ "$2" = "all" ]; then
		do_set_autostart "disable" "kde" ~/.kde ~/.kde/Autostart
		do_set_autostart "disable" "kde4" ~/.kde4 ~/.kde4/Autostart
		do_set_autostart "disable" "gnome" ~/.config ~/.config/autostart

	elif [ -z "$2" ]; then
		echo "Not yet supported, please specify desktop environment."
		exit 1
		
	else
		echo "ERROR: Unsupported desktop environment '$2'."
		echo ""
		echo "To activate the autostart manually, read the documentation of your desktop"
		echo "environment and run '$0 start' to launch the daemon."
		exit 1
	fi
		
	;;
	
status)
	do_configure_warn

	is_running
	if [ $? -eq 1 ]; then
		#PID=$(cat "$PIDFILE")
		
		echo "GCALDaemon is running as PID $PID."		
		exit 0
	fi
	
	echo "GCALDaemon is NOT running."	
	exit 0
	;;
	
password-encoder)
	do_configure_warn

	$CMD -cp $BASEDIR/lib/gcal-daemon.jar org.gcaldaemon.core.PasswordEncoder	
	;;
	
restart)
	do_configure_warn

	$0 stop
	sleep 1
	$0 start 	
	exit 0
	;;
	
*)
	echo "Usage:"
	echo "    gcaldaemon [COMMAND]"
	echo ""
	echo "Commands:"
	echo "    start / stop / restart"
	echo "       Start, stop or restart the GCALDaemon for the currently logged in user."
	echo ""
	echo "    status"
	echo "       Prints status of the daemon and if running its PID."
	echo ""
	echo "    enable [kde | kde4 | gnome | all]"
	echo "        Enable autostart of the GCALDaemon on user login for different desktop"
	echo "        environments. Leave empty to choose the one you are currently using."
	echo ""
	echo "    disable [kde | kde4 | gnome | all]"
	echo "        Disable autostart for the given desktop environment. Leave empty to choose"
	echo "        the one you are currently using."
	exit 1
	;;
esac

exit 0
