#!/usr/bin/sh
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Apache control script designed to allow an easy command line interface
# to controlling Apache.  Written by Marc Slemko, 1997/08/23
# Modified to easy control GranJefe for private use by Salvador Ortiz
#
# The exit codes returned are:
#	0 - operation completed successfully
#	1 -
#	2 - usage error
#	3 - httpd could not be started
#	4 - httpd could not be stopped
#	5 - httpd could not be started during a reload
#	6 - httpd could not be restarted during a reload
#	7 - httpd could not be restarted during a graceful reload
#	8 - configuration syntax error
#
# When multiple arguments are given, only the error from the _last_
# one is reported.  Run "gjctl help" for usage info
#
#
if [ -z "$_guard" ]; then
    exec env -i _guard=1 HOME="$HOME" USER="$USER" GJIDE="$GJIDE" LANG="$LANG" \
	MOD_PERL_TRACE="$MOD_PERL_TRACE" XDG_RUNTIME_DIR="$XDG_RUNTIME_DIR" \
	"$0" "$@"
else
    unset _guard
fi
# In clean environment
PATH="/usr/local/bin:/bin:/usr/bin";
PERL_UNICODE=SDAL
export PATH PERL_UNICODE
DEFINES=""
VERBOSE=0
STATUSBASE="http://localhost"
STATUSURI="/magic/status"
if [ -w /etc ]; then
    if [ -f /etc/sysconfig/gj ]; then
	    . /etc/sysconfig/gj
    fi
    # the path to your PID file
    PIDFILE=/run/gj/gj-httpd.pid
    SVRROOT=/var/GranJefe
else
    if [ -d "$HOME/GranJefe" ]; then
	[ -f "$HOME/GranJefe/config" ] && . "$HOME/GranJefe/config"
	export MOD_PERL_GJUSER="$USER"
	export GJIDE
	PIDFILE=$XDG_RUNTIME_DIR/gj/gj-httpd.pid
	VERBOSE=1
	SVRROOT=$HOME/GranJefe
    else
	echo "You must setup your \$HOME/GranJefe directory";
	exit 0;
    fi
fi
#
# the path to your httpd binary, including options if necessary
#HTTPD=$GJ_DIR/sbin/gj-httpd
HTTPD=/usr/sbin/httpd
#
# a command that outputs a formatted text version of the HTML at the
# url given on the command line.  Designed for elinks, however other
# programs may work.
LYNX="elinks -dump"

ERROR=0
ARGV="$@"
if [ "x$ARGV" = "x" ] ; then
    ARGS="help"
fi

for ARG in $@ $ARGS
do
    # check for pidfile
    if [ -f "$PIDFILE" ] ; then
	PID=$(cat "$PIDFILE")
	if [ "x$PID" != "x" ] && kill -0 "$PID" 2>/dev/null ; then
	    RUNNING=1
	    eval PORT="$(/usr/sbin/ss -tlpn 2>/dev/null | grep -m 1 "pid=$PID" | cut -b 21-44 | cut -d: -f2)"
	    STATUSBASE=$STATUSBASE:$PORT
	    STATUSURL=${STATUSBASE}${STATUSURI}
	    STATUS="GranJefe (pid $PID) already running at $STATUSBASE"
	else
	    STATUS="GranJefe (pid $PID?) not running"
	    RUNNING=0
	fi
    else
	STATUS="GranJefe (no pid file) not running"
	RUNNING=0
    fi

    case $ARG in
    status)
	echo -n "$STATUS"
	[ $VERBOSE -eq 1 ] && echo
	ERROR=0
	;;
    start)
	if [ $RUNNING -eq 1 ]; then
	    echo -n "$STATUS"
	    [ $VERBOSE -eq 1 ] && echo
	    ERROR=1
	    continue
	fi
	if $HTTPD -d "$SVRROOT" $DEFINES; then
	    [ $VERBOSE -eq 1 ] && echo "GranJefe started"
	else
	    [ $VERBOSE -eq 1 ] && echo "GranJefe could not be started"
	    ERROR=3
	fi
	;;
    stop)
	if [ $RUNNING -eq 0 ]; then
	    echo -n "$STATUS"
	    [ $VERBOSE -eq 1 ] && echo
	    ERROR=1
	    continue
	fi
	if kill "$PID" ; then
	    while [ -f "$PIDFILE" ]; do
	      sleep 0.1
	    done
	    [ $VERBOSE -eq 1 ] && echo "GranJefe stopped"
	else
	    [ $VERBOSE -eq 1 ] && echo "GranJefe could not be stopped"
	    ERROR=4
	fi
	;;
    reload)
	if [ $RUNNING -eq 0 ]; then
	    echo "GranJefe not running, trying to start"
	    if $HTTPD $DEFINES ; then
		echo "GranJefe started"
	    else
		echo "GranJefe could not be started"
		ERROR=5
	    fi
	else
	    if $HTTPD $DEFINES -t >/dev/null 2>&1; then
		if kill -HUP "$PID" ; then
		    echo "GranJefe restarted"
		else
		    echo "GranJefe could not be restarted"
		    ERROR=6
		fi
	    else
		echo "Configuration broken, ignoring restart"
		echo "(run 'gjctl configtest' for details)"
		ERROR=6
	    fi
	fi
	;;
    graceful)
	if [ $RUNNING -eq 0 ]; then
	    echo "GranJefe not running, trying to start"
	    if $HTTPD $DEFINES ; then
		echo "GranJefe started"
	    else
		echo "GranJefe could not be started"
		ERROR=5
	    fi
	else
	    if $HTTPD $DEFINES -t >/dev/null 2>&1; then
		if kill -USR1 $PID ; then
		    echo "GranJefe gracefully restarted"
		else
		    echo "GranJefe could not be restarted"
		    ERROR=7
		fi
	    else
		echo "$0 $ARG: configuration broken, ignoring restart"
		echo "$0 $ARG: (run 'gjctl configtest' for details)"
		ERROR=7
	    fi
	fi
	;;
    sstatus)
	echo "Status from '$STATUSURL':"
	$LYNX "$STATUSURL" | awk ' /servers$/ { print; exit } { print } '
	;;
    fullstatus)
	$LYNX "$STATUSURL"
	;;
    configtest)
	if $HTTPD $DEFINES -t; then
	    :
	else
	    ERROR=8
	fi
	;;
    *)
	echo "usage: $0 <command>"
	cat <<EOF
<command> can be: 
start      - start gj-httpd
stop       - stop gj-httpd
reload     - restart gj-httpd if running by sending a SIGHUP or start if
             not running
fullstatus - dump a full status screen; requires elinks
sstatus    - dump a short status screen; requires elinks
graceful   - do a graceful restart by sending a SIGUSR1 or start if not running
configtest - do a configuration syntax test
help       - this screen

EOF
	ERROR=2
    ;;

    esac

done

exit $ERROR
