################################
# VoiceOne Vo-tools Methods
#  
# @package voiceone_webservices
# @subpackage core
################################
 
# copy voicemail description in webservices's sandbox
voicemail () {
    if [ -e "$AST_CONF_DIR/asterisk.conf" ]; then
      if [ ! -d "$2/voicemail/" ]; then
          mkdir $2/voicemail
      fi
      if [ ! -d "$2/voicemail/$1" ]; then
          mkdir $2/voicemail/$1
      fi
      if [ -d "$AST_SPOOL_DIR/voicemail/default/$1/INBOX" ]; then
        rm -rf $2/voicemail/$1/*
        echo INBOX >> $2/voicemail/$1/$1_list.txt
        dir $AST_SPOOL_DIR/voicemail/default/$1/INBOX/ -1 | grep .wav >> $2/voicemail/$1/$1_list.txt
        chmod 666 $2/voicemail/$1/$1_list.txt
        cat $AST_SPOOL_DIR/voicemail/default/$1/INBOX/*.txt >> $2/voicemail/$1/$1_msg.txt
        chmod 666 $2/voicemail/$1/$1_msg.txt
        if [ -d "$AST_SPOOL_DIR/voicemail/default/$1/Old" ]; then
          echo Old >> $2/voicemail/$1/$1_list.txt
          dir $AST_SPOOL_DIR/voicemail/default/$1/Old/ -1 | grep .wav >> $2/voicemail/$1/$1_list.txt
          chmod 666 $2/voicemail/$1/$1_list.txt
          cat $AST_SPOOL_DIR/voicemail/default/$1/Old/*.txt >> $2/voicemail/$1/$1_msg.txt
          chmod 666 $2/voicemail/$1/$1_msg.txt
        fi
        if [ -d "$AST_SPOOL_DIR/voicemail/default/$1/Family" ]; then
          echo Family >> $2/voicemail/$1/$1_list.txt
          dir $AST_SPOOL_DIR/voicemail/default/$1/Family/ -1 | grep .wav >> $2/voicemail/$1/$1_list.txt
          chmod 666 $2/voicemail/$1/$1_list.txt
          cat $AST_SPOOL_DIR/voicemail/default/$1/Family/*.txt >> $2/voicemail/$1/$1_msg.txt
          chmod 666 $2/voicemail/$1/$1_msg.txt
        fi
        if [ -d "$AST_SPOOL_DIR/voicemail/default/$1/Friends" ]; then
          echo Friends >> $2/voicemail/$1/$1_list.txt
          dir $AST_SPOOL_DIR/voicemail/default/$1/Friends/ -1 | grep .wav >> $2/voicemail/$1/$1_list.txt
          chmod 666 $2/voicemail/$1/$1_list.txt
          cat $AST_SPOOL_DIR/voicemail/default/$1/Friends/*.txt >> $2/voicemail/$1/$1_msg.txt
          chmod 666 $2/voicemail/$1/$1_msg.txt
        fi 
        if [ -d "$AST_SPOOL_DIR/voicemail/default/$1/Work" ]; then
          echo Work >> $2/voicemail/$1/$1_list.txt
          dir $AST_SPOOL_DIR/voicemail/default/$1/Work/ -1 | grep .wav >> $2/voicemail/$1/$1_list.txt
          chmod 666 $2/voicemail/$1/$1_list.txt
          cat $AST_SPOOL_DIR/voicemail/default/$1/Work/*.txt >> $2/voicemail/$1/$1_msg.txt
          chmod 666 $2/voicemail/$1/$1_msg.txt
        fi 
      fi
    fi
}

# Transfer voicemail message from asterisk's folder to wb's folder
transferVoiceMailMessage (){
    if [ -e "$AST_CONF_DIR/asterisk.conf" ]; then
      if [ -d "$2/voicemail/$1" ]; then
        if [ ! -d "$2/voicemail/$1/$4" ]; then
          mkdir $2/voicemail/$1/$4       
        fi
        if [ -d "$AST_SPOOL_DIR/voicemail/default/$1" ]; then     
          if [ -e "$AST_SPOOL_DIR/voicemail/default/$1/$4/$3" ]; then  
            cp $AST_SPOOL_DIR/voicemail/default/$1/$4/$3 $2/voicemail/$1/$4
            chmod 777 $2/voicemail/$1/$4/$3
            exit 0  
          fi
        else
          exit 1   
        fi     
      else
        exit 1   
      fi
    fi
}

# Move voicemail message in another voicemail's folder
messageChangeFolder () {
    if [ -e "$AST_CONF_DIR/asterisk.conf" ]; then
        if [ ! -d "$AST_SPOOL_DIR/voicemail/default/$1/$3" ]; then
        mkdir $AST_SPOOL_DIR/voicemail/default/$1/$3
      fi
      if [ -d "$AST_SPOOL_DIR/voicemail/default/$1/$5" ]; then
        if [ -e "$AST_SPOOL_DIR/voicemail/default/$1/$5/$4" ]; then
          mv $AST_SPOOL_DIR/voicemail/default/$1/$5/$4 $AST_SPOOL_DIR/voicemail/default/$1/$3/$2
        fi
      fi  
    fi
}

# Delete a voicemail message
deleteVoicemailMessage () {
    if [ -e "$AST_CONF_DIR/asterisk.conf" ]; then
      rm -f $AST_SPOOL_DIR/voicemail/default/$1/$2.*
    fi
}

# Transfer voicemail message from sounds folder to voicemail folder
transferVoiceMailAnswer(){
    if [ -e "$AST_CONF_DIR/asterisk.conf" ]; then
      if [ -d "$AST_SPOOL_DIR/voicemail/default/$1" ]; then   
        if [ ! -d "$AST_SPOOL_DIR/voicemail/default/$1/ANSWER" ]; then 
          mkdir $AST_SPOOL_DIR/voicemail/default/$1/ANSWER
        fi
        if [ -e "$AST_SOUND_DIR/voiceone/voicemail/$2.gsm" ]; then
          mv -f $AST_SOUND_DIR/voiceone/voicemail/$2.gsm $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.gsm
          #sox $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.gsm -r 8000 -c 1 -s -w $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.wav
          sox $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.gsm -r 8000 -c 1 -s $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.wav
          #sox $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.gsm -r 8000 -c 1 -s -w $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.WAV
          sox $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.gsm -r 8000 -c 1 -s $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.WAV
        fi
      fi
    fi
}

# Put in the ws's sendbox the names of the files in ANSWER folder 
getVoiceMailAnswers(){
    if [ -e "$AST_CONF_DIR/asterisk.conf" ]; then
      if [ -d "$2/voicemail/$1" ]; then
        if [ -d "$AST_SPOOL_DIR/voicemail/default/$1/ANSWER" ]; then
          rm -rf $2/voicemail/$1/$1_answers.txt
          if [ -e "$AST_SPOOL_DIR/voicemail/default/$1/unavail.gsm" ]; 
            then 
              echo "*unavailable" `md5sum $AST_SPOOL_DIR/voicemail/default/$1/unavail.gsm` >> $2/voicemail/$1/$1_answers.txt;  
            else 
              echo "*unavailable none" >> $2/voicemail/$1/$1_answers.txt;
          fi
          if [ -e "$AST_SPOOL_DIR/voicemail/default/$1/busy.gsm" ]; 
            then 
              echo "*busy" `md5sum $AST_SPOOL_DIR/voicemail/default/$1/busy.gsm` >> $2/voicemail/$1/$1_answers.txt;
            else
              echo "*busy none" >> $2/voicemail/$1/$1_answers.txt;
          fi
          if [ -e "$AST_SPOOL_DIR/voicemail/default/$1/temp.gsm" ]; 
            then 
              echo "*temp" `md5sum $AST_SPOOL_DIR/voicemail/default/$1/temp.gsm` >> $2/voicemail/$1/$1_answers.txt;
            else
              echo "*temp none" >> $2/voicemail/$1/$1_answers.txt;  
          fi
          if [ -e "$AST_SPOOL_DIR/voicemail/default/$1/greet.gsm" ]; 
            then 
              echo "*greet" `md5sum $AST_SPOOL_DIR/voicemail/default/$1/greet.gsm` >> $2/voicemail/$1/$1_answers.txt;
            else
              echo "*greet none" >> $2/voicemail/$1/$1_answers.txt;  
          fi
          md5sum $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/*.gsm >> $2/voicemail/$1/$1_answers.txt
          chmod 666 $2/voicemail/$1/$1_answers.txt  
        fi
      fi
    fi
}

# Transfer and rename voicemail message from ANSWER folder to mailbox folder
setVoiceMailAnswer(){
    if [ -e "$AST_CONF_DIR/asterisk.conf" ]; then
      if [ -d "$AST_SPOOL_DIR/voicemail/default/$1" ]; then   
        if [ -e "$AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.gsm" ]; then 
          chmod 755 $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.gsm 
          cp -f $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.gsm $AST_SPOOL_DIR/voicemail/default/$1/$3.gsm
        fi
        if [ -e "$AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.wav" ]; then 
          chmod 755 $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.wav 
          cp -f $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.wav $AST_SPOOL_DIR/voicemail/default/$1/$3.wav
        fi
        if [ -e "$AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.WAV" ]; then 
          chmod 755 $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.WAV 
          cp -f $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.WAV $AST_SPOOL_DIR/voicemail/default/$1/$3.WAV
        fi
      fi
    fi
}

# remove from "voicemail/default/<exten>" folder, "<filename>.*" files 
deleteVoiceMailAnswer(){
    if [ -e "$AST_CONF_DIR/asterisk.conf" ]; then
      if [ -e "$AST_SPOOL_DIR/voicemail/default/$1/$2.gsm" ]; then
        rm -rf $AST_SPOOL_DIR/voicemail/default/$1/$2.*
      fi
    fi 
}

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

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

# return asterisk status 
asterisk_status () {
  if [ -e "$AST_RUN_DIR/asterisk.pid" ]; then 
    if [ -d "/proc/`cat $AST_RUN_DIR/asterisk.pid`" ]; then
      echo "ON"
    elif [ $(ps ax | grep safe_asterisk | grep -v grep | wc -l) -ne "0" ]; then
      kill -9 `ps ax |grep safe_asterisk |grep -v grep|awk '{print $1;}'` >/dev/null 2>/dev/null
      rm $AST_RUN_DIR/asterisk.* >/dev/null 2>/dev/null
      echo "OFF"
    else 
      rm $AST_RUN_DIR/asterisk.* >/dev/null 2>/dev/null
      echo "OFF"
    fi
    else echo "OFF"
  fi  
}

# start asterisk 
asterisk_start () {
  if [ ! -e "$AST_RUN_DIR/asterisk.pid" ]; then
    $AST_ASTERISK $AST_ASTERISK_START >/dev/null 2>/dev/null
    sleep 3
  fi
  if [ -e "$AST_RUN_DIR/asterisk.pid" ]; then 
    if [ -d "/proc/`cat $AST_RUN_DIR/asterisk.pid`" ]; then
      echo "ON"
    elif [ $(ps ax | grep safe_asterisk | grep -v grep | wc -l) -ne "0" ]; then
      kill -9 `ps ax |grep safe_asterisk |grep -v grep|awk '{print $1;}'` >/dev/null 2>/dev/null
      rm $AST_RUN_DIR/asterisk.* >/dev/null 2>/dev/null
      echo "OFF"
    else
      rm $AST_RUN_DIR/asterisk.* >/dev/null 2>/dev/null 
      echo "OFF"
    fi
    else echo "OFF"
  fi 
}

# stop asterisk 
asterisk_stop () {
  if [ -e "$AST_RUN_DIR/asterisk.pid" ]; then
    $AST_ASTERISK $AST_ASTERISK_STOP >/dev/null 2>/dev/null
    sleep 3
  fi
  if [ -e "$AST_RUN_DIR/asterisk.pid" ]; then 
    if [ -d "/proc/`cat $AST_RUN_DIR/asterisk.pid`" ]; then
      echo "ON"
    elif [ $(ps ax | grep safe_asterisk | grep -v grep | wc -l) -ne "0" ]; then
      kill -9 `ps ax |grep safe_asterisk |grep -v grep|awk '{print $1;}'` >/dev/null 2>/dev/null
      rm $AST_RUN_DIR/asterisk.* >/dev/null 2>/dev/null
      echo "OFF"
    else
      rm $AST_RUN_DIR/asterisk.* >/dev/null 2>/dev/null 
      echo "OFF"
    fi
    else echo "OFF"
  fi  
}
#inkey: used by setup and office
inkey() {
  chmod 644 $1.*
  if [ -e $1.key ]; 
  then
    cp $1.pub $AST_KEY_DIR
    mv $1.key $AST_KEY_DIR
  else
    mv $1.pub $AST_KEY_DIR
  fi
  UGIDS=`stat -c %U:%G $AST_KEY_DIR`
  chown ${UGIDS} $AST_KEY_DIR/*
}

shutdown() {
  $SYS_SHUTDOWN $1 now
}
#save configuration to permanent memory
saveConf() {
  $SYS_SAVE_CMD
}

# print vars used in this script
dump_vars() {
  echo "Variables used in vo-tools.sh and vo.cfg:"
  echo "AST_CONF_DIR=$AST_CONF_DIR"
  echo "AST_SPOOL_DIR=$AST_SPOOL_DIR"
  echo "AST_SOUND_DIR==$AST_SOUND_DIR"
  echo "AST_AGI_DIR=$AST_AGI_DIR"
  echo "AST_KEY_DIR=$AST_KEY_DIR"
  echo "AST_GENKEY=$AST_GENKEY"
  echo "MISDN_INIT=$MISDN_INIT"
  echo "ZAPTEL_INIT=$ZAPTEL_INIT"
  echo "DISTRO=$DISTRO"
  echo "ZAPTEL_KMOD=$ZAPTEL_KMOD"
  echo "SYS_SHUTDOWN=$SYS_SHUTDOWN"
  echo "VOICEONE_RELDIR=$VOICEONE_RELDIR"
  echo "VOICEONE_WEBSERVICES_RELDIR=$VOICEONE_WEBSERVICES_RELDIR"  
}


# 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

######## FUNCTIONS INDEX ############
#inkey                inKey $2
#astgenkey            astGenKey $2 $3
#deleteAudiofile      deleteAudiofile $2
#renameAudiofile      renameAudiofile $2 $3	
#shutdown             shutdown $2 $3
#voicemail            voicemail $2 $3
#transferVoiceMailMessage transferVoiceMailMessage $2 $3 $4 $5
#messageChangeFolder      messageChangeFolder $2 $3 $4 $5 $6
#deleteVoicemailMessage   deleteVoicemailMessage $2 $3
#getVoiceMailAnswers      getVoiceMailAnswers $2 $3
#transferVoiceMailAnswer  transferVoiceMailAnswer $2 $3
#setVoiceMailAnswer       setVoiceMailAnswer $2 $3 $4
#deleteVoiceMailAnswer    deleteVoiceMailAnswer $2 $3
#getMohDir                getMohDir
#mohMakeSongsList         mohMakeSongsList $2 $3
#mohSetSong               mohSetSong "$2" "$3" "$4"
#mohDelFolder             mohDelFolder "$2"
#mohMoveSong              mohMoveSong "$2" "$3"
#mohDelSong               mohDelSong "$2"
#getSoundsDir             getSoundsDir
#soundsInit               soundsInit $2
#soundsCopy             soundsCopy $2 $3
#soundsMoveToSandbox    soundsMoveToSandbox $2 $3 $4
#soundsClear            soundsClear $2
#soundsMoveToSounds     soundsMoveToSounds $2 $3
#soundsCreateEmptyFile  soundsCreateEmptyFile $2 $3 
#asterisk_status        asterisk_status
#asterisk_start         asterisk_start
#asterisk_stop          asterisk_stop
  
#dump_vars              dump_vars
