#!/bin/bash
#===============================================================================
#
#         FILE:  zmdbrestore
#
#        USAGE:  ./zmdbrestore
#
#  DESCRIPTION:  Restore a ZoneMinder DB from a backup created by zm_db_backup
#
#      OPTIONS:  ---
# REQUIREMENTS:  ---
#         BUGS:  ---
#        NOTES:  ---
#       AUTHOR:   (),
#      COMPANY:
#      VERSION:  1.0
#      CREATED:  05/29/2006 04:45:06 PM PDT
#     REVISION:  ---
#===============================================================================

ZM_CONFIG=/etc/zm/zm.conf
ZM_BACKUP=/var/lib/zm/zm_backup.sql
EVENTS_DIR=events

loadcfg() {
        if [ -f $ZM_CONFIG ]; then
                . $ZM_CONFIG
        else
                echo "ERROR: $ZM_CONFIG not found."
                exit 1
        fi
}

chkcfg(){
for n in ZM_DB_HOST ZM_DB_NAME ZM_DB_USER ZM_DB_PASS; do
        eval "val=\$$n"
        if [ "$val" = "" ]; then
                echo "ERROR($ZM_CONFIG): $n should not be empty."
                echo "Enter a $n for ZM to use the Database."
                        if [ "$n" = "ZM_DB_PASS" ]; then
                                echo -n "Will not echo on screen $n : "
                                stty -echo    # Turns off screen echo.
                                read newval
                                stty echo     # Restores screen echo.
                                echo ""
                                ### The following can be used to generate a random password
                                # randstr newval 16
                        else
                                echo -n "$n : "
                                read newval
                        fi
                cp $ZM_CONFIG /tmp/$$ &&
                sed 's/^'$n='.*$/'$n=$newval'/g' /tmp/$$ >$ZM_CONFIG

        fi
done

        if [ "$ZM_DB_HOST" = "localhost" ]
        then
                ClientHost=localhost
        else
                ClientHost=`hostname`
        fi
}

reloadcfg(){
        loadcfg
}

chk_backup_ver(){
        if
                [ -e $ZM_BACKUP  ]
        then
                BACKUP_VER=$(cat $ZM_BACKUP | head -n 2 |tail -n 1 |cut -f 8 -d " ")
        else
                echo "$ZM_BACKUP doesn't exist"
                exit 1
        fi


        if
                [ $BACKUP_VER != $ZM_VERSION ]
        then
                echo "$ZM_BACKUP is from version $BACKUP_VER"
                echo "ZoneMinder version is $ZM_VERSION"
                exit 1
        fi
}

getmylogin(){
        echo  "Enter MySQL Administrator username"
        echo  "(Default: root and password is blank)"
        echo -n "MySQL Admin: "
        read MYADMIN
        echo -n "Password:  "
        read MYPASS
        if [ "X$MYPASS" != "X" ]; then MYPASS="-p$MYPASS"; fi
        echo "\q" |mysql -u $MYADMIN $MYPASS || exit 0

}

checkfordb(){

        if
                echo "show databases" |mysql -u $MYADMIN "$MYPASS" |grep zm
        then
                echo "A $ZM_DB_NAME database exists."
                while [ true ]
                        do
                                echo "Choose one of the following options:"
                                echo "[D]rop the old database and reinitialize"
                                echo "[E]xit and do nothing"
                                read OPTION
                                case $OPTION in
                                        "D"|"d")
                                        echo "drop database zm;"|mysql -u $MYADMIN $MYPASS
                                        return
                                        ;;
                                        "E"|"e")
                                        exit 0
                                        ;;
                                esac
                        done

        fi
}

initdb(){
        sql=/tmp/zm.crdb.sql
        echo "" >$sql
        chmod 600 $sql

        echo "CREATE DATABASE $ZM_DB_NAME;" >>$sql
        echo "USE $ZM_DB_NAME;" >>$sql

        echo "GRANT all on $ZM_DB_NAME.* TO '$ZM_DB_USER'@'$ClientHost' IDENTIFIED BY '$ZM_DB_PASS';" >>$sql

        cat $sql | mysql -B -h $ZM_DB_HOST -u $MYADMIN $MYPASS
        rm -f $sql

        cat $ZM_PATH_UPDATE/zm_create.sql | mysql -h $ZM_DB_HOST -u $ZM_DB_USER -p$ZM_DB_PASS $ZM_DB_NAME

}

restoredb(){
        if
                [ -e $ZM_BACKUP  ]
        then
                cat $ZM_BACKUP | mysql -h $ZM_DB_HOST -u $ZM_DB_USER -p$ZM_DB_PASS $ZM_DB_NAME
        else
                echo "$ZM_BACKUP doesn't exist"
                exit 1
        fi
}

restore_events(){
        for SQL in  $(find $ZM_PATH_WEB/$EVENTS_DIR -name .sql)
        do
                cat $SQL | mysql -h $ZM_DB_HOST -u $ZM_DB_USER -p$ZM_DB_PASS $ZM_DB_NAME
        done
}

loadcfg
chkcfg
reloadcfg
chk_backup_ver
getmylogin
checkfordb
initdb
restoredb
restore_events
exit 0
