#!/usr/bin/env bash
# $Id$
# Launch and Stop hellanzb and hellahella with zenity interface.
# Copyright (C) 2005 Meethune Bhowmick <meethune@mercymachines.net>
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#  
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#  
#     * Redistributions in binary form must reproduce the above
#       copyright notice, this list of conditions and the following
#       disclaimer in the documentation and/or other materials provided
#       with the distribution.
#  
#     * Neither the name of Meethune Bhowmick nor the names of
#       its contributors may be used to endorse or promote products
#       derived from this software without specific prior written
#       permission.
#  
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
HELLAEXEC="${HOME}/src/hellanzb/hellanzb.py"
function toggleStatus {
    TOGGLE=${1}
    cd ${HOME}
    if [ "${TOGGLE}" == "start" ]
    then
        echo "starting..."
        ${HELLAEXEC} -D
        paster serve /usr/etc/hella.ini --daemon
        echo "done!"
    else
        echo "stopping...."
        paster serve /usr/etc/hella.ini --stop-daemon $(cat ${HOME}/paster.pid)
        ${HELLAEXEC} shutdown
        echo "done!"
    fi
    cd - >/dev/null 2>&1
    return 0
}

if pgrep -f ${HELLAEXEC} >/dev/null 2>&1
then
    HELLASTATUS="running"
else
    HELLASTATUS="stopped"
fi
if [ -e ${HOME}/paster.pid ]
then
    WEBSTATUS="running"
else
    WEBSTATUS="stopped"
fi

if [ "${HELLASTATUS}" == "running" ] || [ "${WEBSTATUS}" == "running" ]
then
    TOGGLE="shutdown"
else
    TOGGLE="start"
fi

if [ ${#DISPLAY} != 0 ]
then
    if zenity --question --no-wrap --text "hellanzb is:\t ${HELLASTATUS}\nhellahella is:\t ${WEBSTATUS}\n\nDo you want to ${TOGGLE}?"
    then
        if [ "${TOGGLE}" == "start" ]
        then
            toggleStatus "start"
            STATUS="started"
        else
            toggleStatus "stop"
            STATUS="stopped"
        fi
        zenity --info --text "Hellanzb has ${STATUS}."
        exit 0
    fi
elif [ ${#*} != 1 ] || ( [ "${1}" != "start" ] && [ "${1}" != "stop" ] )
then
    echo
    echo "Usage: $(basename ${0}) [start|stop]"
    echo
    exit 1
else
    toggleStatus "${1}"
    exit 0
fi


exit 1

# vim:syntax=sh
# vim:sw=4:softtabstop=4:expandtab
