#!/bin/bash
#
# pbxportal     This shell script takes care of starting/stopping \
#               Thirdlane PBX
# chkconfig: 2345 70 17
# description: Thirdlane PBX is based on Asterisk software
#
# config: /etc/sysconfig/pbxportal.conf
# processname: safe_asterisk

ROOT_UID=0       # root uid is 0
E_NOTROOT=67     # Non-root exit error

echo
# check to see if we are root
if [ "$UID" -ne "$ROOT_UID" ]
then
        echo "Sorry, you must be root to run this script."
        echo
        exit $E_NOTROOT
fi

# make sure config file exists
if [ ! -e "/etc/pbxportal.conf" ]       # Check if file exists.
then
    echo;
    echo "/etc/pbxportal.conf does not exist!";
    echo "Have you installed the ASTERROID configuration?";
    exit;
fi

source /etc/pbxportal.conf

chown_asterisk() {
        echo SETTING FILE PERMISSIONS
        chown -R $ASTRUNGROUP:$ASTRUNUSER $ASTRUNDIR
        chown -R $ASTRUNGROUP:$ASTRUNUSER $ASTETCDIR
        chmod -R g+w $ASTETCDIR
        chown -R $ASTRUNGROUP:$ASTRUNUSER $ASTVARLIBDIR
        chmod -R g+w $ASTVARLIBDIR
        chown -R $ASTRUNGROUP:$ASTRUNUSER $ASTLOGDIR
        chmod -R g+w $ASTLOGDIR
        chown -R $ASTRUNGROUP:$ASTRUNUSER $ASTSPOOLDIR
        chmod -R g+w $ASTSPOOLDIR
#        chmod u+x $ASTVARLIBDIR/agi-bin/*
        chown $ASTRUNUSER /dev/tty9
        # Ensure that various hardware devices are owned correctly.
        [ -e /dev/zap ] && chown -R $ASTRUNGROUP:$ASTRUNUSER /dev/zap 
        [ -e /dev/dahdi ] && chown -R $ASTRUNGROUP:$ASTRUNUSER /dev/dahdi
        [ -e /dev/capi20 ] && chown -R $ASTRUNGROUP:$ASTRUNUSER /dev/capi20
        [ -e /dev/misdn ] && chown -R $ASTRUNGROUP:$ASTRUNUSER /dev/misdn
        [ -e /dev/dsp ] && chown -R $ASTRUNGROUP:$ASTRUNUSER /dev/dsp

        echo Permissions OK
}

check_asterisk() {
# check to see if asterisk is running
# Note, this isn't fool-proof.  If safe_asterisk is constantly restarting a dying asterisk, 
# then there is a chance pidof will return non zero.  We call this twice to reduce chances of this happening
pid_length=`pidof asterisk|awk '{print length($0)}'`
        if [ "$pid_length" == "0" -a "$pid_length" != "" ]
        then
            killall -9 safe_asterisk
            killall -9 mpg123 > /dev/null
            echo
            echo "-----------------------------------------------------"
            echo "Asterisk could not start!"
            echo "Use 'tail $ASTLOGDIR/full' to find out why."
            echo "-----------------------------------------------------"
            exit 0
        fi
}

run_asterisk() {
# check to see if asterisk is running
echo
echo "STARTING ASTERISK"
pid_length=`pidof asterisk|awk '{print length($0)}'`
        if [ "$pid_length" != "0" -a "$pid_length" != "" ]
        then
            echo "Asterisk is already running"
        else
            export LD_LIBRARY_PATH=/usr/local/lib
            /usr/sbin/safe_asterisk -U $ASTRUNUSER -G $ASTRUNGROUP
            sleep 5
            check_asterisk
            sleep 1
            check_asterisk
            echo "Asterisk Started"
        fi
}

stop_asterisk() {
echo
echo "STOPPING ASTERISK"
pid_length=`pidof asterisk|awk '{print length($0)}'`
        if [ "$pid_length" != "0" -a "$pid_length" != "" ]
        then
            /usr/sbin/asterisk -rx "stop gracefully"
            echo "Asterisk Stopped"
        fi
}

kill_pbx() {
        echo
        echo "KILLING PBX PROCESSES"
        killall -9 safe_asterisk
        killall -9 asterisk
        killall -9 mpg123
}

case "$1" in
        start)
                chown_asterisk
                run_asterisk
        ;;
        stop)
                stop_asterisk
        ;;
        restart)
                stop_asterisk
                sleep 5
                chown_asterisk
                run_asterisk
        ;;
        chown)
                chown_asterisk
        ;;
        kill)
                kill_pbx
        ;;
        *)
                echo "----------PBX Control Script-----------"
                echo "Usage: pbxportal start|stop|kill|chown"
                echo
                echo "start: Starts Asterisk "
                echo "stop: Gracefully stops Asterisk"
                echo "restart: Stop and Starts"
                echo "kill: Kills Asterisk"
                echo "chown: Sets appropriate permissions on files"
                echo
                exit 1
        ;;
esac
