################################
# VoiceOne Vo-tools Methods
#  
# @package voiceone_webservices
# @subpackage setup
################################
###
# setVoiceonePassword: set passwords (db,api,proxy) in voiceone config files 
###
setVoiceonePassword() {
  sed -i "s/.*$2.*/$2=\"$3\";/g" $1
}
###
# setMysqlPassword: set password for database user "voiceone" in mysql
###
setMysqlPassword() {
  echo "SET PASSWORD = PASSWORD('$4')" | mysql --user=$1 --password=$2 $3
}
###
# setAsteriskDbPassword: set password for database user "voiceone" in asterisk's res_mysql file
###
setAsteriskDbPassword() {
  sed -i "s/.*dbpass.*/dbpass = $1/g" $AST_CONF_MYSQL
}
###
# setAsteriskCdrPassword: set password for database user "voiceone" in asterisk's cdr_mysql file
###
setAsteriskCdrPassword() {
  sed -i "s/.*password.*/password=$1/g" $AST_CONF_CDR
}
###
# setAsteriskManagerPassword: set password for asterisk API user "voiceone" in asterisk's manager file
###
setAsteriskManagerPassword() {
  sed -i "/\[voiceone\]/,/\[.*\]/s/.*secret.*/secret=$1/g" $AST_CONF_MANAGER
}
###
# setAutomysqlbackupPassword: set password for database user "voiceone" in automysqlbackup script
###
setAutomysqlbackupPassword() {
  sed -i "s/.*PASSWORD=.*/PASSWORD=$1/g" $SYS_CONF_AUTOMYSQLBACKUP
}
###
# setAsteriskStatPassword: set password for database user "voiceone" in automysqlbackup script
###
setAsteriskStatPassword() {
  sed -i "s/.*define (\"PASS\".*/define (\"PASS\", \"$1\");/g" $SYS_CONF_ASTERISKSTAT
}
###
# setVoiceoneUpdatePassword: set password for database user "voiceone" in update include script
###
setVoiceoneUpdatePassword() {
  sed -i "s/.*DBPASS=.*/DBPASS=\"$1\"/g" $SYS_CONF_VOUPDATE
}
###
# setAstmanproxyPassword: set password for API user "voiceone" in astmanproxy config file
###
setAstmanproxyPassword() {
  sed -i "/VOICEONE_START/,/VOICEONE_END/s/.*host.*/host = localhost, 5038, voiceone, $1, on, off/g" $SYS_CONF_ASTMANPROXYCONF
  sed -i "s/.*voiceone.*/voiceone=$1,,,/g" $SYS_CONF_ASTMANPROXYUSERS
}
###
# setSystemPassword: set password for system (and ssh) user "voiceone"
###
setSystemPassword() {
  $SYS_WRITE_CMD start >/dev/null 2>/dev/null
  echo "voiceone:$1" | chpasswd
  $SYS_WRITE_CMD stop >/dev/null 2>/dev/null
}
###
# install: execute the setup procedure (install agi and set files permissions
###
install() {
  mkdir $AST_SOUND_DIR/voiceone
  mkdir $AST_SOUND_DIR/voiceone/custom
  mkdir $AST_SOUND_DIR/voiceone/IVR
  mkdir $AST_SOUND_DIR/voiceone/voicemail
  mkdir $AST_SOUND_DIR/voiceone/tmp
  mkdir $AST_MOH_DIR/voiceone
  mkdir $AST_MOH_DIR/voiceone/default
    
  chmod a+w $1/$VOICEONE_RELDIR/admin/public/template/cache/
  chmod a+w $1/$VOICEONE_RELDIR/admin/public/script/*
  chmod a+w $1/$VOICEONE_RELDIR/sandbox/
  
  chmod a+x $1/$VOICEONE_WEBSERVICES_RELDIR/config/script/*
  chmod a+w $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/
  chmod a+w $1/$VOICEONE_WEBSERVICES_RELDIR/utility/provisioning/firmware
  
  # CALCULATE THE PHP SHABANG
  if RESULT=`which "php-cgi" 2>/dev/null`; then
    SHABANG="#!$RESULT -q"
  elif RESULT=`which "php4-cgi" 2>/dev/null`; then
    SHABANG="#!$RESULT -q"
  elif RESULT=`which "php" 2>/dev/null`; then
    SHABANG="#!$RESULT -q"
  else
    echo "ERROR: No php interpreter found"
    exit 1
  fi
  
  # WRITE THE AGI FILES
  echo $SHABANG > $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/dial.php
  echo "<? include (\"$1/$VOICEONE_WEBSERVICES_RELDIR/agi/dial.inc.php\"); ?> " >> $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/dial.php
  echo $SHABANG > $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/incoming.php
  echo "<? include (\"$1/$VOICEONE_WEBSERVICES_RELDIR/agi/incoming.inc.php\"); ?> " >> $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/incoming.php
  echo $SHABANG > $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/record.php
  echo "<? include (\"$1/$VOICEONE_WEBSERVICES_RELDIR/agi/record.inc.php\"); ?> " >> $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/record.php
  echo $SHABANG > $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/playivr.php
  echo "<? include (\"$1/$VOICEONE_WEBSERVICES_RELDIR/agi/playivr.inc.php\"); ?> " >> $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/playivr.php
  echo $SHABANG > $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/utility.php
  echo "<? include (\"$1/$VOICEONE_WEBSERVICES_RELDIR/agi/utility.inc.php\"); ?> " >> $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/utility.php
  
  # MOVE THE PHP FILES TO THE AGI DIR
  mv $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/dial.php $AST_AGI_DIR
  mv $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/incoming.php $AST_AGI_DIR
  mv $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/record.php $AST_AGI_DIR
  mv $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/playivr.php $AST_AGI_DIR
  mv $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/utility.php $AST_AGI_DIR
  
  mv $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/agi-callback.agi $AST_AGI_DIR
  
  if [ $(ls $AST_MOH_DIR/*.* | wc -l) -gt 0 ] ; then 
    # LINK DEFAULT MOH FILES FROM ASTRISK FOLDER TO VOICEONE FOLDER
    ln -s $AST_MOH_DIR/*.* $AST_MOH_DIR/voiceone/
    # LINK DEFAULT MOH FILES FROM VOICEONE FOLDER TO DEFAULT FOLDER
    ln -s $AST_MOH_DIR/voiceone/*.* $AST_MOH_DIR/voiceone/default
  fi
  
  # ASSIGN THE SAME OWNER OF THE AGI DIR TO THE FILES INSIDE IT
  UGIDS=`stat -c %U:%G $AST_AGI_DIR`
  
  chown ${UGIDS} $AST_AGI_DIR/dial.php
  chown ${UGIDS} $AST_AGI_DIR/incoming.php
  chown ${UGIDS} $AST_AGI_DIR/agi-callback.agi
  chown ${UGIDS} $AST_AGI_DIR/record.php
  chown ${UGIDS} $AST_AGI_DIR/playivr.php
  chown ${UGIDS} $AST_AGI_DIR/utility.php

  # ASSIGN EXECUTION PERMISSION TO THE PHP AGI FILES
  chmod a+x $AST_AGI_DIR/dial.php
  chmod a+x $AST_AGI_DIR/incoming.php
  chmod a+x $AST_AGI_DIR/agi-callback.agi
  chmod a+x $AST_AGI_DIR/record.php
  chmod a+x $AST_AGI_DIR/playivr.php
  chmod a+x $AST_AGI_DIR/utility.php

  # ASSIGN THE SAME OWNER OF THE SOUNDS DIR TO THE FILES INSIDE IT
  chown -R ${UGIDS} $AST_SOUND_DIR/voiceone
  
  # ASSIGN THE SAME OWNER OF THE MOH DIR TO THE FILES INSIDE IT
  chown -R ${UGIDS} $AST_MOH_DIR/voiceone
}
###
# generatePbxKey: generate pbx public key
###
generatePbxKey() {
  if [ -e "$1mypbx.pub" ]; then
    rm -f "$1mypbx.pub"
  fi
  echo $1
  echo $2
  #echo "$AST_GENKEY \"$1$2\""
  $AST_GENKEY "$1$2"
  if [ ! -e "$1mypbx.pub" ]; then
    cp -f "$1$2.pub" "$1mypbx.pub"
  fi
}