#!/bin/zsh
# description: Description comes here....

PID_FILE="/var/run/openconnect.pid";

start() {
	echo "initiating connection...";
	~/bin/gp-okta -q ~/.gp-okta.conf;
}

stop() {
	# code to stop app comes here
	sudo pkill -F "$PID_FILE";
	sudo pkill "vpn-slice";
}

status() {
	STATUS="openconnect not running";
	RETCODE=1;
	pPID=`pgrep openconnect`;
	if [ "$pPID" -a -s "$PID_FILE" ];
	then
		rPID=`cat "$PID_FILE"`;
		if [ "$pPID" = "$rPID" ]
		then
			STATUS="openconnect($pPID) is running";
			RETCODE=0;
		fi
	fi
	echo $STATUS;
	exit $RETCODE;
}

case "$1" in
	start)
		if [ status ];
		then
			start;
		else
			echo "- already running";
		fi
		;;
	stop)
		if [ ! status ];
		then
			stop;
		else
			echo "- not running";
		fi
		stop;
		;;
	restart)
		stop;
		sleep 3;
		start;
		;;
	status)
		status;
		;;
	*)
		echo "Usage: $0 {start|stop|status|restart}"
esac

exit 0
