#
# Installer for Vodia PBX on Debian
#
# TODO:
# Decide what languages you want installed (for audio). Valid languages are:
# dk: Danish
# nl: Dutch
# uk: English (UK)
# en: English (US)
# ca: French (Canada)
# fr: French (France)
# de: German
# gr: Greek
# it: Italian
# pl: Polish
# ru: Russian
# sp: Spanish
# se: Swedish
# tr: Turkish
LANGUAGES="en de"

# TODO:
# Decide where to put all the stuff:
PBX_DIR=/usr/local/pbx

# TODO:
# Decide which version you want to run:
VERSION=58.2

# Below here should be audomatic
# Find out if this is 32 or 64 bit:
BITS=`getconf LONG_BIT`;

DOWNLOAD_PATH=http://vodia.com/downloads/pbx

#
# Check if this is root
if [ "$EUID" -ne 0 ]; then
  echo "This script must be run as root"
  exit 1
fi

#
# Make sure that the programs that we need are installed:
#
if [ -z `which wget` ]; then
  apt-get install wget
fi
if [ -z `which ntpd` ]; then
  apt-get install ntp
fi
if [ -z `which unzip` ]; then
  apt-get install unzip
fi
if [ -z `which killall` ]; then
  apt-get install psmisc
fi


if [ ! -d $PBX_DIR ]; then
  mkdir $PBX_DIR
fi
cd $PBX_DIR

# Get the language files:
for i in $LANGUAGES moh; do
  wget $DOWNLOAD_PATH/audio/audio_$i.zip
  unzip audio_$i.zip
  rm audio_$i.zip
done

# Get the executable:
wget $DOWNLOAD_PATH/debian$BITS/pbxctrl-debian$BITS-$VERSION
wget $DOWNLOAD_PATH/dat/pbxctrl-$VERSION.dat

mv pbxctrl-debian$BITS-$VERSION pbxctrl
mv pbxctrl-$VERSION.dat pbxctrl.dat
chmod a+rx pbxctrl

# Install the startup script:
cd /etc/init.d
cat >pbx <<EOF
#!/bin/bash
#
# pbx	     This takes care of starting and stopping the Vodia PBX.
#
### BEGIN INIT INFO
# Provides:          pbx
# Required-Start:    \$remote_fs \$syslog
# Required-Stop:     \$remote_fs \$syslog
# Should-Start:      \$portmap
# Should-Stop:       \$portmap
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Vodia PBX
# Description:       Vodia PBX is a software-based private branch
#                    exchange (PBX) for devices that support the SIP
#                    protocol. It provides telephony services as well
#                    as associated services like provisioning devices
#                    for telephony.
### END INIT INFO

PBXNAME=pbxctrl
PBXEXE=$PBX_DIR/pbxctrl
PBXDIR=$PBX_DIR

#Service script for the Vodia PBX:
case "\$1" in
    start)
        echo -n "Starting PBX daemon"
        \$PBXEXE --dir \$PBXDIR --respawn || return=\$rc_failed
        echo -e "\$return"
        ;;
    stop)
        echo -n "Shutting down PBX daemon:"
        killall \$PBXNAME || return=\$rc_failed
        echo -e "\$return"
        ;;
    restart)
        \$0 stop && \$0 start || return=\$rc_failed
        ;;
    status)
        echo -n "Checking for service pbx: "
        checkproc pbx && echo OK || echo No process
        ;;
    *)
        echo "Usage: \$0 {start|stop|status|restart}"
        exit 1
esac

# Inform the caller not only verbosely and set an exit status.
test "\$return" = "\$rc_done" || exit 1
exit 0
EOF

chmod a+rx pbx
update-rc.d pbx defaults

# Finally start the service:
./pbx start
