#!/bin/bash


#=== Informations ===================================================================#
#- Goal: restore application's database and data files
#- Author: B. Mauclaire - bm@ekimia.fr - 2019/11/13
#- Licence: GNU Affero General Public License v3.0
#- Usage:
#  * Argument: application's name, database container's ID/NAME, database engine
#  * The script has to be ran with sudo in order to be able to write in volume's directories.
#  * Run in the directory containing vm archives + db.env + docker-compose.yml: so docker app directory's name isn't necessary.
#  * Vm archive's name is built with the application's name, so files' name are generic in this script.
#  * Data files will be restored in "data" directory as data file archives contain subdirectory.


#=== Functions ===================================================================#

#--- Print script's help/usage:
function print_help () 
{
   echo "Usage: sudo ./restore_app.sh APLICATION_NAME(piwigo/prestashop/nextcloud) DATABASE_CONTAINER_ID/NAME [DB_ENGINE(mysql/mariadb/pgsql)]"
   exit 1
}


#--- Main program tasks:
function main_proc () 
{
   echo ""
   echo "***** Restore $APP_NAME application data *****"

   #-- Manage some variable used by the script:
   check_arguments
   DATA_DIR='data'
   
   #-- Restore:
   db_informations
   restore_db
   restore_data
   update_app_config
      
   echo ""
   echo "***** End of $APP_NAME restore *****"
}


#--- Check APP_NAME argument:
function check_arguments ()
{
   if [ "$APP_NAME" != "piwigo" ] && [ "$APP_NAME" != "prestashop" ] && [ "$APP_NAME" != "nextcloud" ] ; then
      echo "Application's name must be one of piwigo/prestashop/nextcloud."
      exit 1
   fi
}


#--- Filling database variable from db.env:
function db_informations ()
{
   echo "*** Filling database variable from db.env ***"
   #-- Use db.env and pwg.env to get user + pswd already in variables without regexp:
   source .env
   case $DB_ENGINE in
      "mysql" | "mariadb")
         #- Fill the following variables: MYSQL_ROOT_PASSWORD, MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD, PUID, PGID, TZ.
         DB_NAME="$MYSQL_DATABASE"
         DB_USER="$MYSQL_USER"
         DB_PWD="$MYSQL_PASSWORD"
         DB_ROOT_USER="root"
         DB_ROOT_PWD="$MYSQL_ROOT_PASSWORD"
         ;;
      "pgsql")
         #- // TO TEST //#
         DB_NAME="$POSTGRES_DB"
         DB_USER="$POSTGRES_USER"
         DB_PWD="$POSTGRES_PASSWORD"
         ;;
   esac
}


#--- Restore database:
function restore_db ()
{
   echo "*** Database restore from sql dump ***"

   #-- Restore database from sql archive:
   #- Database container name is 'db' and it's assumed that it is define like that in app's docker-compose.yml file.
   case $DB_ENGINE in
      "mysql" | "mariadb")
         docker-compose exec -T db bash -c "mysql -u '"$DB_ROOT_USER"' --password='"${DB_ROOT_PWD}"' '"$DB_NAME"'" < ${DB_NAME}.sql
         #docker exec -i $CONT_DB_ID bash -c "mysql -u '"$DB_ROOT_USER"' -p${DB_ROOT_PWD} '"$DB_NAME"'" < ${DB_NAME}.sql
         #cat ${DB_NAME}.sql | docker exec -i $CONT_DB_ID /usr/bin/mysql '"$DB_NAME"' -u '"$DB_ROOT_USER"' --password='"$DB_ROOT_PWD"'
         ;;
      "pgsql")
         #- // TO TEST //#
         docker-compose exec -T db bash -c "PGPASSWORD='"$DB_PWD"' psql -U '"$DB_USER"' '"$DB_NAME"'" < ${DB_NAME}.sql
         #docker exec -i $CONT_DB_ID bash -c "PGPASSWORD='"$DB_PWD"' psql -U '"$DB_USER"' '"$DB_NAME"'" < ${DB_NAME}.sql
         #cat ${DB_NAME}.sql | docker exec -i $CONT_DB_ID PGPASSWORD='"$DB_PWD"' /usr/bin/psql -U '"$DB_USER"' '"$DB_NAME"'
         ;;
   esac
   
   echo "*** Database dump restored! ***"
}


#--- Restore data files:
function restore_data ()
{
   echo "*** Restore application data from ${APP_NAME}_*.tgz to container directory ***"
   echo "** Start uncompressing archive(s) (be patient)... **"
   cd $APP_DIR
   for archfile in ../*.tgz ; do
      echo "Uncompress $archfile..."
      tar -xzf $archfile
   done
   echo "*** Archive(s) *.tgz restored! ***"
}


#--- Update application configuration files with container's setup:
#-- This function depends on application
function update_app_config ()
{
   case $APP_NAME in
      "piwigo")
         #-- Update db_host name to be container's databse's name in docker-compose.yml, assummed 'db'. See docker-compose.yml for "config" volume name:
         #sed -i -e 's/localhost/db/' "$DATA_DIR/config/database.inc.php"
         sed -i -e 's/localhost/$CONT_DB_ID/' "$DATA_DIR/local/config/database.inc.php"
         mv $DATA_DIR/local/config/config.inc.php $DATA_DIR/local/config/config.inc.php.old
         ;;
      "prestashop")
         #-- Todo:
         #Delete the admin dir if exists
         #Change db name and mysql root password in config/settings.inc.php
         ;;
      "nextcloud")
         #-- Todo:
         ;;
   esac
}



#=== Main program ===================================================================#
#--- Manage arguments:
case $# in
   2) APP_NAME="$1" ; CONT_DB_ID="$2" ; DB_ENGINE="mysql"
      main_proc
      ;;
   3) APP_NAME="$1" ; CONT_DB_ID="$2" ; DB_ENGINE="$3"
      main_proc
      ;;
   *) print_help ;;
esac
#=== End of script ==================================================================#
