################################
# VoiceOne Vo-tools Methods
#  
# @package voiceone_webservices
# @subpackage backup
################################
###
# restoreBackup: untar backup in sandbox, restore database from dump and 
#                copy sounds file
###
restoreBackup () {
	UXTRACT_TMP_DIR="extract_temp_dir"
  if [ -d "$1/$UXTRACT_TMP_DIR" ]; then
  	rm -rf "$1/$UXTRACT_TMP_DIR"
  fi
  if [ -e "$1/restored.ok" ]; then
  	rm -f "$1/restored.ok"
  fi
	mkdir "$1/$UXTRACT_TMP_DIR"
  if [ -e "$1/$UXTRACT_TMP_DIR" ]; then
  	if [ -e "$1/$2.tar.gz" ]; then
      cp -p "$1/$2.tar.gz" "$1/$UXTRACT_TMP_DIR/"
      if [ -e "$1/$UXTRACT_TMP_DIR/$2.tar.gz" ]; then
      	cd "$1/$UXTRACT_TMP_DIR"
    		gunzip "$1/$UXTRACT_TMP_DIR/$2.tar.gz"
        if [ -e "$1/$UXTRACT_TMP_DIR/$2.tar" ]; then
          tar xf "$1/$UXTRACT_TMP_DIR/$2.tar"
          #restore database
          if [ -e "$1/$UXTRACT_TMP_DIR/$2_sql.gz" ]; then
      			gunzip "$1/$UXTRACT_TMP_DIR/$2_sql.gz"
            if [ -e "$1/$UXTRACT_TMP_DIR/$2_sql" ]; then
            	mysql --user=$3 --password=$4 $5 < "$1/$UXTRACT_TMP_DIR/$2_sql"
            else
  						echo "Error: unable to unzip database dump"
              exit 0
            fi
          else
  					echo "Error: unable to find database dump"
            exit 0
          fi
          ##restore moh
          #if [ -d "$AST_MOH_DIR/voiceone" ]; then
          #  if [ -e "$1/$UXTRACT_TMP_DIR/$2_moh.tar.gz" ]; then
          #  	gunzip "$1/$UXTRACT_TMP_DIR/$2_moh.tar.gz"
          #    if [ -e "$1/$UXTRACT_TMP_DIR/$2_moh.tar" ]; then
          #    	cd "$AST_MOH_DIR/voiceone"
          #      rm -rf $AST_MOH_DIR/voiceone/*
          #      if [ $(ls -l "$AST_MOH_DIR/voiceone/" | wc -l) -eq "1" ]; then
          #        tar xf "$1/$UXTRACT_TMP_DIR/$2_moh.tar"
          #      else
          #      	echo "Error: unable to remove sounds folder content"
          #  			exit 0
          #      fi
          #    else
  				#			echo "Error: unable to unzip moh dump"
          #  		exit 0
          #    fi
          #  else
  				#		echo "Error: unable to find moh dump"
          #  	exit 0
          #  fi
          #else
  				#	echo "Error: unable to find mohmp3/voiceone directory"
          #  exit 0
          #fi
          #restore sounds
          if [ -d "$AST_SOUND_DIR/voiceone" ]; then
            if [ -e "$1/$UXTRACT_TMP_DIR/$2_sounds.tar.gz" ]; then
            	gunzip "$1/$UXTRACT_TMP_DIR/$2_sounds.tar.gz"
              if [ -e "$1/$UXTRACT_TMP_DIR/$2_sounds.tar" ]; then
              	cd "$AST_SOUND_DIR"
                #rm -rf $AST_SOUND_DIR/voiceone/*
                #if [ $(ls -l "$AST_SOUND_DIR/voiceone/" | wc -l) -eq "1" ]; then
                  tar xf "$1/$UXTRACT_TMP_DIR/$2_sounds.tar"
                #else
                #	echo "Error: unable to remove sounds folder content"
            		#	exit 0
                #fi
              else
  							echo "Error: unable to unzip sounds dump"
            		exit 0
              fi
            else
  						echo "Error: unable to find sounds dump"
            	exit 0
            fi
          else
  					echo "Error: unable to find sounds/voiceone directory"
            exit 0
          fi
        else
  				echo "Error: unable to unzip backup"
        	exit 0
      	fi
      else
  			echo "Error: unable to copy backup in temp directory"
        exit 0
      fi
    else
  		echo "Error: unable to find backup"
      exit 0
    fi
    #delete temp dir
    rm -rf "$1/$UXTRACT_TMP_DIR"
    echo OK
    touch "$1/restored.ok"
  	exit 1
  else
  	echo "Error: unable to create temp directory"
    exit 0
  fi
}
###
# createBackup: create a tar backup in sandbox with database dump and a copy of 
#                voiceone sounds file
###
createBackup () {
  # backup database
  if [ -e "$1/$2_sql.gz" ]; then
  	rm -f "$1/$2_sql.gz"
  fi
	mysqldump --user=$3 --password=$4 --databases $5 --ignore-table=$5.cdr --no-create-db | gzip > "$1/$2_sql.gz"
  mysqldump --user=$3 --password=$4 --databases $5 --tables cdr --no-data --no-create-db | gzip >> "$1/$2_sql.gz"
  ## backup moh
  #if [ -e "$1/$2_moh.tar.gz" ]; then
  #	rm -f "$1/$2_moh.tar.gz"
  #fi
  #if [ -d "$AST_MOH_DIR/voiceone" ]; then
 	#	cd "$AST_MOH_DIR/voiceone"
  #  tar cf $1/$2_moh.tar ./*
  #  gzip $1/$2_moh.tar
  #fi
  # backup sounds
  if [ -e "$1/$2_sounds.tar.gz" ]; then
  	rm -f "$1/$2_sounds.tar.gz"
  fi
  if [ -d "$AST_SOUND_DIR/voiceone" ]; then
 		cd "$AST_SOUND_DIR"
    tar cf $1/$2_sounds.tar ./*/voiceone ./voiceone
    gzip $1/$2_sounds.tar
  fi
  # backup codec licenses
  if [ -e "$1/$2_licenses.tar.gz" ]; then
  	rm -f "$1/$2_licenses.tar.gz"
  fi
  if [ -d "$AST_LICENSES_DIR" ]; then
 		cd "$AST_LICENSES_DIR"
    tar cf $1/$2_licenses.tar .
    gzip $1/$2_licenses.tar
  fi
  # create tar.gz file
  if [ -e "$1/$2_sql.gz" ]; then
  	#if [ -e "$1/$2_moh.tar.gz" ]; then
    	if [ -e "$1/$2_sounds.tar.gz" ]; then
        if [ -e "$1/$2_licenses.tar.gz" ]; then
      	  cd "$1"
      	  #tar cf "$1/$2.tar" "$2_sql.gz" "$2_moh.tar.gz" "$2_sounds.tar.gz" "$2_licenses.tar.gz"
          tar cf "$1/$2.tar" "$2_sql.gz" "$2_sounds.tar.gz" "$2_licenses.tar.gz"
          gzip "$1/$2.tar"
        else
          cd "$1"
          #tar cf "$1/$2.tar" "$2_sql.gz" "$2_moh.tar.gz" "$2_sounds.tar.gz"
          tar cf "$1/$2.tar" "$2_sql.gz" "$2_sounds.tar.gz"
          gzip "$1/$2.tar"
        fi
    	fi
    #fi
  fi
  rm -f "$1/$2_sql.gz"
  #rm -f "$1/$2_moh.tar.gz"
  rm -f "$1/$2_sounds.tar.gz"
  rm -f "$1/$2_licenses.tar.gz"
}