#!/bin/bash

PSQL_BIN="/usr/local/pgsql/bin"
REC_PATH="/usr/local/recordum"



if [ -d "$1" ] && [ "$1" != "" ]
then 
    echo "Deleting old data from database: test"
    $PSQL_BIN/psql -Upostgres -c 'DELETE FROM global_config_history' test
    $PSQL_BIN/psql -Upostgres -c 'DELETE FROM global_config' test

    echo "Restoring table: global_config"
    $PSQL_BIN/pg_restore -Fc -dtest -Upostgres -a "$1/DB_test_table_global_config"

    echo "Dropping database: webinterface"
    $PSQL_BIN/dropdb -Upostgres webinterface

    echo "Restoring database: webinterface"
    $PSQL_BIN/createdb -EUNICODE -Opostgres -Ttemplate1 -Upostgres webinterface
    $PSQL_BIN/pg_restore -Fc -dwebinterface -Upostgres "$1/DB_webinterface"

    echo "Copying recordum config files"
    cp "$1/config" $REC_PATH/conf/
    cp "$1/linlog.config" $REC_PATH/conf/
    cp "$1/watchdog.config" $REC_PATH/conf/

    echo "Copying system config files"
    cp "$1/ifcfg-eth0" /etc/sysconfig/network/
    cp "$1/wvdial.conf" /etc/
    cp "$1/ddclient.conf" /etc/

    echo "Done."
else
    echo "Cannot find backup directory!"
    echo "Usage: $0 BACKUPDIR"
    exit 1
fi



