#!/bin/bash
#
# Copyright (c) 2019 Clementine Computing LLC.
# 
# This file is part of PopuFare.
# 
# PopuFare is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# PopuFare is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with PopuFare.  If not, see <https://www.gnu.org/licenses/>.
#

org_daemon_base_dir="/home/bus/new_bus/devel/server_daemon"
org_deamon_pid_dir="/tmp"
org_daemon_log_dir="/home/bus/log"

daemon=( avls_server billing_server buspass_server hello_daemon version_server )

start_org_server() {
	path=$1
	lockfile=$2
        logfile=$3
	if [ -e "$lockfile" ]; then
		if ps -p `cat $lockfile` > /dev/null 2>&1; then
			echo $path is already running - skipping start
                        return 0
		else
			echo Lockfile $lockfile is junk. Deleting...
			rm -f $lockfile
			#start_buspass_server
		fi
        fi

        if [ -z "$logfile" ]
        then
                logfile="/dev/null"
        fi
	#else 
		echo Starting $path
		$path  > $logfile 2>&1 &
		echo $! > $lockfile
	#fi
}

stop_org_server() {
	path=$1
	lockfile=$2
	if [ -e $lockfile ]; then
		echo Stopping $path
		kill `cat $lockfile`
		rm -f $lockfile
	else 
		echo $path is not running
	fi
}

echo
case $1 in
	start )
                for d in ${daemon[@]}
                do
                        start_server $org_deamon_base_dir/$d.pl $org_daemon_lockfile_dir/$d.pid $org_daemon_logfile_dir/$d.log
                done
	;;
	stop )
                for d in ${daemon[@]}
                do
                        stop_server $org_deamon_base_dir/$d.pl $org_daemon_lockfile_dir/$d.pid 
                done
	;;
	restart )
		$0 stop
		$0 start
	;;
	* )
                for d in ${daemon[@]}
                do
                        if [ "$d" == $1 ]
                        then
                                case "$2" in
                                        start ) 
                                                start_server $org_deamon_base_dir/$d.pl $org_daemon_lockfile_dir/$d.pid $org_daemon_logfile_dir/$d.log
                                                break
                                        ;;
                                        stop ) 
                                                stop_server $org_deamon_base_dir/$d.pl $org_daemon_lockfile_dir/$d.pid 
                                                break
                                        ;;
                                        restart ) 
                                                stop_server $org_deamon_base_dir/$d.pl $org_daemon_lockfile_dir/$d.pid
                                                start_server $org_deamon_base_dir/$d.pl $org_daemon_lockfile_dir/$d.pid $org_daemon_logfile_dir/$d.log
                                                break
                                        ;;
                                        * ) 
                                                break 
                                                ;;
                                esac
                                return 0
                        fi
                done
                
		echo "Usage:"
		echo "    $0 start"
		echo "    $0 stop"
		echo "    $0 restart"
		;;
esac
