#!/bin/sh
# VoiceOne tools to allow operations on file and system level
# This file must be set sudo root or set uid and have owner root
# Usage: vo-tools.sh <cmd> [<param1>]  <param2>] etc.
# 

# absolute path to configuration file for shell scripts used by voiceone_webservices
# usually the same dir as the script dircotory /var/www/html/voiceone_webservices/config/script/
VOCFGDIR="/var/www/html/voiceone_webservices/config/script/"
#
### Pre Checks ###
#
# Check if config file exists
test -f "$VOCFGDIR/vo.cfg" && . "$VOCFGDIR/vo.cfg"
if [ "$CFG" != "ok" ]; then echo "Could not find or initialize configuration file:vo.cfg"; exit 1; fi

#
### Functions Part ###
#

# delete an audiofile in asterisk sound dir
deleteAudiofile () {
    if [ -f "$AST_SOUNDDIR/$1" ]; then
      rm  -f "$AST_SOUNDDIR/$1"
      exit 0
    else
	  echo "deleteAudio: File:$AST_SOUNDDIR/$1 does not exists."
	  exit 1
	fi
}

# rename an audiofile in asterisk sound dir
renameAudiofile () {
    if [ -f "$AST_SOUNDDIR/$1" ]; then
      mv  -f "$AST_SOUNDDIR/$1" "$AST_SOUNDDIR/$2"
      exit 0
    else
	  echo "RenameAudio: Old file:$AST_SOUNDDIR/$1 does not exists."
	  exit 1
	fi
}

# execute the setup procedure
install() {
  mkdir $AST_SOUNDDIR/voiceone
  mkdir $AST_SOUNDDIR/voiceone/custom
  mkdir $AST_SOUNDDIR/voiceone/IVR
  
  chmod a+w $1/voiceone/admin/public/template/cache/
  chmod a+w $1/voiceone/admin/public/script/*
  chmod a+w $1/voiceone/sandbox/
  
  chmod a+x $1/voiceone_webservices/config/script/*
  chmod a+w $1/voiceone_webservices/sandbox/
  
  cp $1/voiceone_webservices/sandbox/dial.php $AST_AGIDIR
  cp $1/voiceone_webservices/sandbox/incoming.php $AST_AGIDIR
  cp $1/voiceone_webservices/sandbox/agi-callback.agi $AST_AGIDIR
  cp $1/voiceone_webservices/sandbox/agi-callback.agi $AST_AGIDIR
  cp $1/voiceone_webservices/sandbox/record.php $AST_AGIDIR
  
  UGIDS=`stat -c %U:%G $AST_AGIDIR`
  
  chown ${UGIDS} $AST_AGIDIR/dial.php
  chown ${UGIDS} $AST_AGIDIR/incoming.php
  chown ${UGIDS} $AST_AGIDIR/agi-callback.agi
  chown ${UGIDS} $AST_AGIDIR/record.php

  chmod a+x $AST_AGIDIR/dial.php
  chmod a+x $AST_AGIDIR/incoming.php
  chmod a+x $AST_AGIDIR/agi-callback.agi
  chmod a+x $AST_AGIDIR/record.php

  chown -R ${UGIDS} $AST_SOUNDDIR/voiceone
}

inKey() {
  chmod 644 $1.*
  if [ -e $1.key ]; 
  then
    mv $1.key $AST_KEY
    cp $1.pub $AST_KEY
  else
    mv $1.pub $AST_KEY
  fi

  UGIDS=`stat -c %U:%G $AST_KEY`
  chown ${UGIDS} $AST_KEY/*
}

misdnConf() {
  chown root:root $1/misdn-init.conf
  chmod 644 $1/misdn-init.conf
  mv $1/misdn-init.conf /etc
}

zaptelConf() {
  chown root:root $1/zaptel.conf
  chmod 644 $1/zaptel.conf
  mv $1/zaptel.conf /etc
}

safeAsterisk() {
  $AST_SAFE_ASTERISK $1
}

zaptelInit() {
  $ZAPTEL_INIT $1
}

misdnInit() {
  $MISDN_INIT $1
}

astGenKey() {
  $AST_GENKEY $1
}

shutdown() {
  $SYS_SHUTDOWN $1 now
}

# Only for debugging
debug() {
  echo "$(date +"%Y%m%d-%H:%M"): Debug of vo-tools.sh" >> $DEBUG_VOT
  echo "=>Function:$1" >> $DEBUG_VOT
  echo "=>Param1  :$2" >> $DEBUG_VOT
  echo "=>Param2  :$3" >> $DEBUG_VOT
  echo "=>Param3  :$4" >> $DEBUG_VOT
}

# Debugging enabled ? (Needs to be enabled in vo.cfg)
if [ "$DEBUG_VOT" != "" ]; then debug $1 $2 $3 $4; fi


#
### Evaluate CMD and execute ###
#
case "$1" in
  
  install)
    if [ "$#" -ne "2" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    install $2
	exit 0;;
  
  inkey)
    if [ "$#" -ne "2" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    inKey $2
	exit 0;;
  
  zaptel_conf)
    if [ "$#" -ne "2" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    zaptelConf $2
	exit 0;;

  misdn_conf)
    if [ "$#" -ne "2" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    misdnConf $2
	exit 0;;
  
  safe_asterisk)
    if [ "$#" -ne "2" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    safeAsterisk $2
	exit 0;;
  
  zaptel_init)
    if [ "$#" -ne "2" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    zaptelInit $2
	exit 0;;

  misdn_init)
    if [ "$#" -ne "2" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    misdnInit $2
	exit 0;;

  astgenkey)
    if [ "$#" -ne "2" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    astGenKey $2
	exit 0;;
  
  deleteAudiofile) 
    if [ "$#" -ne "2" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    deleteAudiofile $2
	exit 0;;
	
  renameAudiofile) 
    if [ "$#" -ne "3" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    renameAudiofile $2 $3
	exit 0;;
	
  shutdown) 
    if [ "$#" -ne "3" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    shutdown $2 $3
	exit 0;;
	
  *)
    echo "Error: Unknown Command:$0" 
	echo "Supported CMDs:" 
	echo "deleteAudiofile filename"
	echo "renameAudiofile filename newFilename"
	echo "install WebServerDocumentRoot"
	echo "inkey FullPathFilename"
	echo "zaptel_conf FullPathSandbox"
	echo "misdn_conf FullPathSandbox"
	echo "safe_asterisk cmd"
  echo "zaptel_init cmd"
	echo "misdn_init cmd"
	echo "astgenkey filename"
	echo "shutdown action"
	exit 1;;
esac
