#!/bin/bash

PMES_DB_PATH="/opt/pmes-framework/pmes-service/conf"                                                                                                                                                                                                                                                                                                                                         
usage ()                                                                                                                                                                          
{                                                                                                                                                                                 
        /bin/cat <<EOT                                                                                                                                                            

Usage: $0 required_options [other_options]
Available options:
        -b                                      Performs a backup of the service database.
        -r <backup_file>                        Restores an existing database snapshot.
        -p <table>                              Purges the information stored on a certain table.
        -s <table>                              Shows the information stored on a certain table.
        -d                                      Drains the PMES service database, cleaning all content.
        -h | --help                             Print this help message.
Available tables:
        users                                   Stores the user details.
        jobs                                    Stores information of the submitted jobs.
        applications                            Stores the registered applications.
        context_vars                            Stores the context variables of an application.
        arguments                               Stores the arguments of an application.
        storage                                 Stores the details of the storage services registered into the system.
        accounting                              Stores the accounting information of the jobs.

EOT
        exit 0;
}

#Check if the user is pmes
if [ $USER != "pmes" ]; then
  echo
  echo "* You are logged in as $USER, please run this script as pmes user"
  echo
  exit 1
fi

while getopts ":h:p:r:s:db" opt; do
  case $opt in
     h)
             usage
             exit 1
             ;;
     b)
             backup_file=pmes_`date +"%H%M_%m%d%Y"`.db.gz
             echo
             echo "* PMES-DB: Backing up database into /opt/pmes-framework/pmes-service/conf/backups/$backup_file ..."
             sqlite3 $PMES_DB_PATH/pmes.db '.dump' | gzip -c > $PMES_DB_PATH/backups/$backup_file
             echo
             ;;

     r)
             backup_file=$OPTARG
             echo
             echo "* PMES-DB: Restoring database snapshot from $backup_file ..."
             mv $PMES_DB_PATH/pmes.db $PMES_DB_PATH/pmes.db.old
             gunzip -c $backup_file | sqlite3 $PMES_DB_PATH/pmes.db
             if [ -f $PMES_DB_PATH/pmes.db ]; then
                rm $PMES_DB_PATH/pmes.db.old
             else
                echo "   - an error ocurred restoring the database, old database copy available at: $PMES_DB_PATH/pmes.db.old"
             fi
             echo
             ;;

     p)
             table=$OPTARG
             echo
             echo "* PMES-DB: purging \"$table\" table ..."
             if [ $table = "users" ]; then 
                sqlite3 $PMES_DB_PATH/pmes.db "DELETE FROM users WHERE username!=\"admin\""
             else
                sqlite3 $PMES_DB_PATH/pmes.db "DELETE FROM $table"
             fi
             echo
             ;;
     s)
             table=$OPTARG
             echo
             echo "* PMES-DB: showing \"$table\" table information:";
             echo
             sqlite3 -header -column $PMES_DB_PATH/pmes.db "SELECT * FROM $table"
             echo
             ;;
     d)
             echo
             echo "* PMES-DB: draining all tables ..."
             echo "   - cleaning \"users\" table ..."
             sqlite3 $PMES_DB_PATH/pmes.db "DELETE FROM users WHERE username!=\"admin\""
             
             echo "   - cleaning \"jobs\" table ..."
             sqlite3 $PMES_DB_PATH/pmes.db "DELETE FROM jobs"
             
             echo "   - cleaning \"applications\" table ..."
             sqlite3 $PMES_DB_PATH/pmes.db "DELETE FROM applications"
             
             echo "   - cleaning \"context_vars\" table ..."
             sqlite3 $PMES_DB_PATH/pmes.db "DELETE FROM context_vars"
             
             echo "   - cleaning \"arguments\" table ..."
             sqlite3 $PMES_DB_PATH/pmes.db "DELETE FROM arguments"
             
             echo "   - cleaning \"storage\" table ..."
             sqlite3 $PMES_DB_PATH/pmes.db "DELETE FROM storage"
             
             echo "   - cleaning \"accounting\" table ..."
             sqlite3 $PMES_DB_PATH/pmes.db "DELETE FROM accounting"
             echo
             ;;
     ?)
             usage
             exit
            ;;
  esac
done

