#!/bin/bash
#  This script is used to perform a quick restore of a database that was backed up via the nightly system backup
#  The restore only does the database it does NOT restore system files (even if they are in the backup file)
#
# - supplemental web, process.py  , lh_system_restore

. /etc/systemconfig
BACKUPDIR=/data/storage/disk0/backup/system_backups
RESTOREDIR=$BACKUPDIR/restore
backupfile=$1
restore_status=0

MYTH_RUN_STATUS="1"
. /etc/profile

function usage(){
    echo "------------------------------------------------------"
    echo "This program will restore the database from a system backup."

    echo "System backup files are expected to be in the $BACKUPDIR"
    echo ""
    echo "Usage:"
    echo ""
    echo "lh_system_restore_job \$filename  [cleanup  partial]"
    echo ""
    echo "example: lh_system_restore_job backup.2011-12-22_15-34.tgz "
    echo ""
    echo ""

    echo "If the script is called with cleanup, it will cleanup the restore dir."
    echo "If the script is called with partial, it will restore the db,"
    echo "    and then copy back some settings."
    echo "    partial is intended to be used to restore R7 databases only."
    echo "------------------------------------------------------"

    exit 1
}

function restore_status_check(){
    if [ $1 -ne 0 ]
    then
        restore_status=1
    fi
}


function run_cleanup(){
    echo "Cleaning up the restore dir"
    rm -rf $RESTOREDIR/$DIR
    rm -f $RESTOREDIR/$backupfile
}

function expand_restore_file(){
    mkdir -p $RESTOREDIR
    DIR=`echo $backupfile |cut -d. -f2`
    cp $BACKUPDIR/$backupfile $RESTOREDIR
    cd $RESTOREDIR && tar -xf  $backupfile && cd $DIR
    restore_status_check $?
}

function restore_db(){
    CSQL="create database mythconverg;"
    DSQL="drop database mythconverg; "
    MYSQL="mysql -u mythtv -pmythtv"

    if [ -f mythconverg ]
    then
        #drop the db
        $MYSQL -e "$DSQL"
        #create the db
        $MYSQL  -e "$CSQL"
        #restore the database_backup
        echo "Restoring database  $DIR"
        $MYSQL  mythconverg < mythconverg
        rc=$?
        restore_status_check $rc

        if [ $rc = 0 ]
        then
            echo "Restore of mythconverg complete"
        else
            echo "An error occurred restoring mythconverg"
        fi

     else
        echo "Couldn't find file mythconverg  to restore"
        restore_status=1
    fi

}

function restore_ncid(){
    CSQL="create database ncid;"
    DSQL="drop database ncid; "
    MYSQL="mysql  -u root "

    if [ -f ncid ]
    then
        #drop the db
        $MYSQL -e "$DSQL"
        #create the db
        $MYSQL  -e "$CSQL"
        #restore the database_backup
        echo "Restoring database  $DIR"
        $MYSQL  ncid < ncid
        rc=$?
        #restore_status_check $rc

        if [ $rc = 0 ]
        then
            echo "Restore of ncid complete"
        else
            echo "An error occurred restoring ncid"
        fi

     else
        echo "Couldn't find file ncid  to restore"
    fi

}


function run_prestore(){
    echo "Partial restore of database"
    echo "---------------------------------"
    echo
    #save settings to group
        echo "    Saving current database settings for selective import"
        mythutil --export-settings --outfile $RESTOREDIR/pre_db.exports.xml  --tablelist settings --generic 2> /dev/null >> /tmp/prestore.out

    #save storage groups
        mysqldump   mythconverg storagegroup > $RESTOREDIR/sg.sql

    #stop mythbackend
        echo "    Stopping mythbackend"
        lh_backend_control.sh stop

    # restore mythconverg
        restore_db
        mysqldump   mythconverg storagegroup > $RESTOREDIR/backupfile_sg.sql
        mysql mythconverg -e "truncate storagegroup;"

        restore_ncid
    # restore exported settings
        echo "    Importing previous settings into settings_pre_db"
        mythutil --import-settings --infile $RESTOREDIR/pre_db.exports.xml --hostname `hostname` 2>/dev/null  >> /tmp/prestore.out

    # restore storage_groups
        mysql mythconverg < $RESTOREDIR/sg.sql

    #start mythbackend
        echo "    Starting mythbackend"
        lh_backend_control.sh start

    echo
    echo "Partial restore complete."
    echo "All data for `hostname` has been restored except:"
    echo "*   storage groups definitions"
    echo "*   service menu settings"


}

function run_full_restore(){
    echo "Full restore"
    echo "---------------------------------"
    echo
    cp -f etc/systemconfig /etc/systemconfig

    if [ $SystemType = Master_backend -o  $SystemType = Standalone ]
    then
    #stop mythbackend
        echo "    Stopping mythbackend"
        lh_backend_control.sh stop
        echo

    # restore mythconverg
        restore_db
        echo
        restore_ncid
        echo

    #start mythbackend
        echo "    Starting mythbackend"
        lh_backend_control.sh start
        echo

    else
    # didn't restore the database, so restore FE settings
        if [ -e settings ]
        then
            rsync -arvp settings/* /usr/MythVantage/templates/settings/
        fi

        if [ -e /usr/MythVantage/templates/settings/syssettings ]
        then
            myth_settings_wrapper.sh -c restore -t syssettings
            restore_status_check $?
        else
            echo "Couldn't find syssettings file"
        fi
    fi
    systemconfig.py -m all
    echo "Restore complete"
}

#-----------------------Main program --------------------------

if [ x$backupfile = x ]
then
    usage
fi

prestore="false"
cleanup="false"

for i in $*; do
    #echo $i
    if [ $i = "cleanup" ]
    then
        cleanup="true"
    fi

    if [ $i = "partial" ]
    then
        prestore="true"
    fi
done

expand_restore_file

if  [ $prestore = "true" ]
then
    run_prestore
else
    run_full_restore
fi

if [ $cleanup = "true" ]
then
    run_cleanup
fi

exit $restore_status
