#!/bin/bash
# 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 directory /var/www/html/voiceone_webservices/script/
# VOCFGDIR="/var/www/voiceone_webservices/script"
if [ -f "/var/www/html/voiceone_webservices/script/vo-tools.sh" ] ; then
    VOCFGDIR="/var/www/html/voiceone_webservices/script"
elif [ -f "/var/www/voiceone_webservices/script/vo-tools.sh" ]  ; then
    VOCFGDIR="/var/www/voiceone_webservices/script"
else
    echo "ERROR: Could not find vo.cfg directory.";
    exit 1; 
fi
########################################
# Init and check if config file exists
########################################
test -f "$VOCFGDIR/vo.cfg" && . "$VOCFGDIR/vo.cfg"
if [ "$CFG" != "ok" ]; then echo "ERROR: Could not find or initialize configuration file: vo.cfg"; exit 1; fi

########################################
# Calculate global used vars
########################################
# Locate and store type of Distro used
if [ -d /etc/sysconfig ]; then
  DISTRO=REDHAT
elif [ -f /etc/debian_version ]; then
  DISTRO=DEBIAN
else
  DISTRO=UNKNOWN
fi

# Locate and store AST_SPOOL_DIR
AST_SPOOL_DIR=`grep astspooldir $AST_CONF_DIR/asterisk.conf | cut -d '>' -f 2`
AST_SPOOL_DIR="`expr "$AST_SPOOL_DIR" : '[ ]*\(.*\)[ ]*$'`" 

# Locate and store AST_MONITOR_DIR
AST_MONITOR_DIR="$AST_SPOOL_DIR/monitor"

# Locate and store AST_OUTGOING_DIR
AST_OUTGOING_DIR="$AST_SPOOL_DIR/outgoing"

# Locate and store AST_SOUND_DIR
AST_SOUND_DIR=`grep astvarlibdir $AST_CONF_DIR/asterisk.conf | cut -d '>' -f 2`
AST_SOUND_DIR="`expr "$AST_SOUND_DIR" : '[ ]*\(.*\)[ ]*$'`"
AST_SOUND_DIR="$AST_SOUND_DIR/sounds" 

# Locate and store AST_AGI_DIR
AST_AGI_DIR=`grep astagidir $AST_CONF_DIR/asterisk.conf | cut -d '>' -f 2`
AST_AGI_DIR="`expr "$AST_AGI_DIR" : '[ ]*\(.*\)[ ]*$'`" 

# Locate and store AST_KEY_DIR
AST_KEY_DIR=`grep astvarlibdir $AST_CONF_DIR/asterisk.conf | cut -d '>' -f 2`
AST_KEY_DIR="`expr "$AST_KEY_DIR" : '[ ]*\(.*\)[ ]*$'`" 
AST_KEY_DIR="$AST_KEY_DIR/keys"

# Locate and store AST_MOH_DIR
AST_MOH_DIR=`grep astvarlibdir $AST_CONF_DIR/asterisk.conf | cut -d '>' -f 2`
AST_MOH_DIR="`expr "$AST_MOH_DIR" : '[ ]*\(.*\)[ ]*$'`"
AST_MOH_DIR="$AST_MOH_DIR/moh"

# Locate and store AST_RUN_DIR
AST_RUN_DIR=`grep astrundir $AST_CONF_DIR/asterisk.conf | cut -d '>' -f 2`
AST_RUN_DIR="`expr "$AST_RUN_DIR" : '[ ]*\(.*\)[ ]*$'`"

########################################
# Check some important global used vars
########################################
# Check if Distro used on this system is supported
if [ "$DISTRO" = "UNKNOWN" ]; then echo "ERROR: Could not locate type of Linux Distribution"; exit 1; fi
# Check access to asterisk configuration 
if [ ! -f "$AST_CONF_DIR/asterisk.conf" ]; then echo "ERROR: Could not locate Asterisk configuration"; exit 1; fi

########################################
# Import script methods from modules
########################################
MODULES_DIR="$VOCFGDIR/../modules"
for i in $( ls $MODULES_DIR); do
  if [ -f "$MODULES_DIR/$i/script/methods.sh" ]; then
    source "$MODULES_DIR/$i/script/methods.sh"
  fi
done

########################################
# Evaluate CMD and execute method
########################################
if [ "$#" -gt "0" ]; then
  $*
fi
