#!/bin/bash

TIMESTAMP=`date "+%b-%d-%Y-%T"`

DB_CONFIG="/var/clearos/system_database/root"
MYSQL="/usr/clearos/sandbox/usr/bin/mysql"
MYSQLDUMP="/usr/clearos/sandbox/usr/bin/mysqldump"
DUMPFILE="/var/clearos/system_database/rebuild/database.$TIMESTAMP.sql"

#============================================================================
# Reset database
#============================================================================

reset_db() {
    echo "Resetting the database"

    /etc/rc.d/init.d/system-mysqld stop >/dev/null 2>&1
    sleep 3
    /etc/rc.d/init.d/system-mysqld stop >/dev/null 2>&1
    sleep 3

    rm -rf /var/lib/system-mysql/*
    rm -f /var/clearos/system-database/root
    /usr/clearos/apps/system_database/deploy/bootstrap

    sleep 3
    /etc/rc.d/init.d/system-mysqld start >/dev/null 2>&1

    # TODO: the app should handle this, but do this for now
    [ -x /usr/clearos/apps/reports_database/deploy/initialize-database ] && /usr/clearos/apps/reports_database/deploy/initialize-database
    [ -x /usr/clearos/apps/network_report/deploy/install ] && /usr/clearos/apps/network_report/deploy/install
    [ -x /usr/clearos/apps/resource_report/deploy/install ] && /usr/clearos/apps/resource_report/deploy/install
    [ -x /usr/clearos/apps/proxy_report/deploy/install ] && /usr/clearos/apps/proxy_report/deploy/install
}

#============================================================================
# Rebuild database
#============================================================================

rebuild_db() {
    # Check database status
    #----------------------

    if [ ! -e /var/lib/system-mysql/mysql.sock ]; then
        echo "Database is not running... will not proceeed"
        exit 1
    fi

    # Grab database password
    #-----------------------

    DBPASS=`grep ^password $DB_CONFIG 2>/dev/null | sed "s/^password[[:space:]]*=[[:space:]]*//"`

    if [ -z "$DBPASS" ]; then
        echo "Unable to authenticate with database"
        exit 1
    fi

    # Dump the database
    #------------------

    echo "Exporting the database"

    mkdir -p "/var/clearos/system_database/rebuild"

    $MYSQLDUMP --all-databases -uroot -p"$DBPASS" > $DUMPFILE

    if [ $? != 0 ]; then
        echo "Unable to backup database... bailing"
        rm -f $DUMPFILE
        exit 1
    fi

    # Reset database to nil
    #----------------------

    echo "Stopping the database"
    /etc/rc.d/init.d/system-mysqld stop >/dev/null 2>&1
    sleep 3
    /etc/rc.d/init.d/system-mysqld stop >/dev/null 2>&1
    sleep 3

    echo "Cleaning the database"
    rm -rf /var/lib/system-mysql/*
    #/usr/clearos/apps/system_database/deploy/bootstrap

    # Reload database
    #----------------

    /etc/rc.d/init.d/system-mysqld start >/dev/null 2>&1
    sleep 3

#    DBPASS=`grep ^password $DB_CONFIG 2>/dev/null | sed "s/^password[[:space:]]*=[[:space:]]*//"`

#    if [ -z "$DBPASS" ]; then
#        echo "Unable to authenticate with database"
#        exit 1
#    fi

    echo "Rebuilding the database"
#    $MYSQL -uroot -p"$DBPASS" < $DUMPFILE
    $MYSQL -uroot < $DUMPFILE

    /etc/rc.d/init.d/system-mysqld stop >/dev/null 2>&1
    sleep 3
    /etc/rc.d/init.d/system-mysqld stop >/dev/null 2>&1
    sleep 3
    /etc/rc.d/init.d/system-mysqld start >/dev/null 2>&1

    echo "Done... you may want to copy/remove the backup file $DUMPFILE"
}

#============================================================================
# Main
#============================================================================

if [ "$1" == "rebuild" ]; then
    rebuild_db
elif [ "$1" == "reset" ]; then
    reset_db
else
    echo "system-database <action> (action is either 'rebuild' or 'reset')"
fi
