#!/bin/bash
echo
echo "WARNING: THIS SCRIPT HAS TO BE FULLY COMPATIBLE WITH ROULETTE_NEW.GDB!!! "
echo
echo "Starting Database repair LEVEL 2 on `cat /RouletteMaster/ROULETTE_NAME` /data/ROULETTE_DATA.GDB"
echo "Press CTRL+C to cancel now...."
sleep 5
echo "Started...please wait(this process may last up to 30 minutes)."
echo "!!!DO NOT INTERRUPT THIS PROCESS!!!"


service crond stop > /dev/null 2>&1
service roulette stop > /dev/null 2>&1
killall rouletteterminal > /dev/null 2>&1
killall ladyroulette > /dev/null 2>&1
killall symmetricds-keepalive.sh > /dev/null 2>&1
killall java > /dev/null 2>&1
sleep 1 
service firebird stop > /dev/null 2>&1
sleep 3
killall fbserver > /dev/null 2>&1

sleep 5

#rm -rf /data/TEMPDB.GDB > /dev/null 2>&1
rm -rf /data/TEMPBKP.GBK > /dev/null 2>&1


BKPDB=`mktemp /data/ROULETTE_DATA.XXX`;
#if [ ! -e $BKPDB ]; then
    mv /data/ROULETTE_DATA.GDB $BKPDB
    if [ $? -ne 0 ]; then
	echo "ERROR: Error renaming database /data/ROULETTE_DATA.GDB to $BKPDB"
	exit 1
    fi
    echo "Old database renamed to $BKPDB."
    cp /RouletteMaster/EMPTY_DATABASE/ROULETTE_NEW.GDB /data/ROULETTE_DATA.GDB
    if [ $? -ne 0 ]; then
	echo "ERROR: Error copying new database from /RouletteMaster/EMPTY_DATABASE/ROULETTE_NEW.GDB to /data/ROULETTE_DATA.GDB"
	exit 2
    fi
    echo "New database copied to /data/ROULETTE_DATA.GDB"

    chmod 666 /data/ROULETTE_DATA.GDB
    chown firebird:firebird /data/ROULETTE_DATA.GDB
    if [ $? -ne 0 ]; then
	echo "ERROR: Error setting permission to /data/ROULETTE_DATA.GDB"
	exit 3
    fi
    echo "Database permission set to 666 and owned by firebird."
    
    /sbin/service firebird start
    sleep 5

    #preko INSERT statements export-import lahko delamo samo, ce ni BLOB datatype.
    #to je dobro za primere, ko imajo tabele dodane nove stolpce
    #ko kopiramo preko binary mode -S, mora biti struktura obeh tabel enaka.
#vzorec za kopiranje preko InsertStatements
#    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME"
#    fbexport -Si -D $BKPDB -P masteraloha -Q $QUERY -F TMP.SQL
#    cat TMP.SQL | /RouletteMaster/sqlexec.sh
# dela tudi preko binary, samo za izbrane field-e, ce dolocis select in insert statements

    TABLE_NAME=DIGITAL_COUNTERS
    SELECT_SQL="select                  CLIENT_IP, COUNTER_VALUE, COUNTER_INDEX, COUNTING_FROM from $TABLE_NAME"
    INSERT_SQL="insert into $TABLE_NAME(CLIENT_IP, COUNTER_VALUE, COUNTER_INDEX, COUNTING_FROM)"
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME";
    fbexport -S -D $BKPDB -P masteraloha -Q "$SELECT_SQL" -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -F - -R -Q "$INSERT_SQL";

    TABLE_NAME=CONFIGURATION
    SELECT_SQL="select                  CONFIG_ID,CLIENT_IP,ATTR_NAME,ATTR_NUMERIC_VALUE,ATTR_STRING_VALUE,ATTR_MODIFIED,ATTR_CREATED,ATTR_ACCESSED,DESCRIPTION_ID,USER_MODIFIED from $TABLE_NAME"
    INSERT_SQL="insert into $TABLE_NAME(CONFIG_ID,CLIENT_IP,ATTR_NAME,ATTR_NUMERIC_VALUE,ATTR_STRING_VALUE,ATTR_MODIFIED,ATTR_CREATED,ATTR_ACCESSED,DESCRIPTION_ID,USER_MODIFIED)"
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME";
    fbexport -S -D $BKPDB -P masteraloha -Q "$SELECT_SQL" -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -F - -R -Q "$INSERT_SQL";


    TABLE_NAME=CREDIT_TRANSFERS
    SELECT_SQL="select                  CT_ID,GAME_ID,CLIENT_IP,FROM_IP,TO_IP,CREDIT_VALUE,IS_CACHABLE,TIME_STAMP,CREDIT_TYPE from $TABLE_NAME"
    INSERT_SQL="insert into $TABLE_NAME(CT_ID,GAME_ID,CLIENT_IP,FROM_IP,TO_IP,CREDIT_VALUE,IS_CACHABLE,TIME_STAMP,CREDIT_TYPE)"
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME";
    fbexport -S -D $BKPDB -P masteraloha -Q "$SELECT_SQL" -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -F - -R -Q "$INSERT_SQL";

    #too many fields, VERBATIM COPY-
    #TABLE_NAME=BETS_HISTORY
    #fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME"
    #fbexport -S -D $BKPDB -P masteraloha -V $TABLE_NAME -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -V $TABLE_NAME -F - -R

    TABLE_NAME=GAME_PHASE_DATA
    SELECT_SQL="select                  GAME_ID,GAME_P20_START,GAME_P22_START,GAME_P23_START,GAME_P30_START,GAME_P31_START,GAME_WIN_NUM,GAME_LAST_PHASE,GAME_P40_START,GAME_P21_START,GAME_P41_START,GAME_P24_START,RNG_HOST_IP from $TABLE_NAME"
    INSERT_SQL="insert into $TABLE_NAME(GAME_ID,GAME_P20_START,GAME_P22_START,GAME_P23_START,GAME_P30_START,GAME_P31_START,GAME_WIN_NUM,GAME_LAST_PHASE,GAME_P40_START,GAME_P21_START,GAME_P41_START,GAME_P24_START,RNG_HOST_IP)"
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME";
    fbexport -S -D $BKPDB -P masteraloha -Q "$SELECT_SQL" -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -F - -R -Q "$INSERT_SQL";


    TABLE_NAME=MONETARY
    SELECT_SQL="select                  MON_ID,CLIENT_IP,MON_OPERATION_TIME,MON_OPERATION_TYPE,MON_CREDIT_VALUE,MON_C_ID,GAME_ID,CONFIRMED,MON_MONEY_VALUE from $TABLE_NAME"
    INSERT_SQL="insert into $TABLE_NAME(MON_ID,CLIENT_IP,MON_OPERATION_TIME,MON_OPERATION_TYPE,MON_CREDIT_VALUE,MON_C_ID,GAME_ID,CONFIRMED,MON_MONEY_VALUE)"
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME";
    fbexport -S -D $BKPDB -P masteraloha -Q "$SELECT_SQL" -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -F - -R -Q "$INSERT_SQL";


    TABLE_NAME=MONETARY_CONVERSION
    SELECT_SQL="select                  MON_C_ID,MON_C_COEFFICIENT,MON_DESCRIPTION,MON_VALUE,MON_CURRENCY,MON_CREDIT_TYPE from $TABLE_NAME"
    INSERT_SQL="insert into $TABLE_NAME(MON_C_ID,MON_C_COEFFICIENT,MON_DESCRIPTION,MON_VALUE,MON_CURRENCY,MON_CREDIT_TYPE)"
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME";
    fbexport -S -D $BKPDB -P masteraloha -Q "$SELECT_SQL" -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -F - -R -Q "$INSERT_SQL";


    TABLE_NAME=DESCRIPTIONS2
    SELECT_SQL="select                  ATTR_NAME,LANG_ID,DESCRIPTION,CAPTION from $TABLE_NAME"
    INSERT_SQL="insert into $TABLE_NAME(ATTR_NAME,LANG_ID,DESCRIPTION,CAPTION)"
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME";
    fbexport -S -D $BKPDB -P masteraloha -Q "$SELECT_SQL" -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -F - -R -Q "$INSERT_SQL";

    TABLE_NAME=LANGUAGES
    SELECT_SQL="select                  LANG_ID,LANG_CAPTION,LANG_SOUND_FOLDER,VISIBLE from $TABLE_NAME"
    INSERT_SQL="insert into $TABLE_NAME(LANG_ID,LANG_CAPTION,LANG_SOUND_FOLDER,VISIBLE)"
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME";
    fbexport -S -D $BKPDB -P masteraloha -Q "$SELECT_SQL" -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -F - -R -Q "$INSERT_SQL";

    TABLE_NAME=LANG_MESSAGES
    SELECT_SQL="select                  LANG_MSG_ID,LANG_MSG_SUB_ID,LANG_ID,LANG_MESSAGE from $TABLE_NAME"
    INSERT_SQL="insert into $TABLE_NAME(LANG_MSG_ID,LANG_MSG_SUB_ID,LANG_ID,LANG_MESSAGE)"
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME";
    fbexport -S -D $BKPDB -P masteraloha -Q "$SELECT_SQL" -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -F - -R -Q "$INSERT_SQL";

    TABLE_NAME=JACKPOT_CLIENTS
    SELECT_SQL="select                  CLIENT_IP,JACKPOT_ID,STATUS,LAST_UPDATE,LAST_BET_VALUE_CENTS,LAST_INGAME_BET_CENTS from $TABLE_NAME"
    INSERT_SQL="insert into $TABLE_NAME(CLIENT_IP,JACKPOT_ID,STATUS,LAST_UPDATE,LAST_BET_VALUE_CENTS,LAST_INGAME_BET_CENTS)"
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME";
    fbexport -S -D $BKPDB -P masteraloha -Q "$SELECT_SQL" -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -F - -R -Q "$INSERT_SQL";

    TABLE_NAME=JACKPOT_PHASE_DATA
    SELECT_SQL="select                  STATUS,POT_VALUE_MONEY,JACKPOT_ID,LAST_UPDATE,DESCRIPTION,POT_RELOAD_MONEY from $TABLE_NAME"
    INSERT_SQL="insert into $TABLE_NAME(STATUS,POT_VALUE_MONEY,JACKPOT_ID,LAST_UPDATE,DESCRIPTION,POT_RELOAD_MONEY)"
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME";
    fbexport -S -D $BKPDB -P masteraloha -Q "$SELECT_SQL" -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -F - -R -Q "$INSERT_SQL";

    TABLE_NAME=KIOSK_TICKETS
    SELECT_SQL="select                  KIOSK_ID, TICKET_ID, ORDER_KEYWORD, TICKET_STATUS, RNG_INSTANCE_ID, RNG_GAME_ID_FROM, RNG_GAME_COUNT, ORDER_TIMESTAMP, VALIDATE_TIMESTAMP, PAYOUT_TIMESTAMP, ESTIMATE_FINISH_TIMESTAMP, ORDERED_BY, VALIDATED_BY, PAID_BY, BET_ON_0, BET_ON_1, BET_ON_2, BET_ON_3, BET_ON_4, BET_ON_5, BET_ON_6, BET_ON_7, BET_ON_8, BET_ON_9, BET_ON_10, BET_ON_11, BET_ON_12, BET_ON_13, BET_ON_14, BET_ON_15, BET_ON_16, BET_ON_17, BET_ON_18, BET_ON_19, BET_ON_20, BET_ON_21, BET_ON_22, BET_ON_23, BET_ON_24, BET_ON_25, BET_ON_26, BET_ON_27, BET_ON_28, BET_ON_29, BET_ON_30, BET_ON_31, BET_ON_32, BET_ON_33, BET_ON_34, BET_ON_35, BET_ON_36, BET_ON_00, BET_ON_RED, BET_ON_BLACK, BET_ON_ODD, BET_ON_EVEN, BET_ON_1_18, BET_ON_19_36, BET_ON_1ST12, BET_ON_2ND12, BET_ON_3RD12, BET_ON_2TO1L, BET_ON_2TO1M, BET_ON_2TO1H, JP_PARTICIPATE, JP_POT_VALUE_GOLD, JP_POT_VALUE_SILVER, JP_POT_VALUE_BRONZE, JP_FEE, JP_GAMBLE_RESULT, JP_GAMBLE_RESULT_CAPTION, JP_WIN_SUM, TICKET_PRICE, TICKET_MAX_POSSIBLE_WIN, TICKET_WIN, CURRENCY_TAG, TICKET_STATUS_STRING from $TABLE_NAME"
    INSERT_SQL="insert into $TABLE_NAME(KIOSK_ID, TICKET_ID, ORDER_KEYWORD, TICKET_STATUS, RNG_INSTANCE_ID, RNG_GAME_ID_FROM, RNG_GAME_COUNT, ORDER_TIMESTAMP, VALIDATE_TIMESTAMP, PAYOUT_TIMESTAMP, ESTIMATE_FINISH_TIMESTAMP, ORDERED_BY, VALIDATED_BY, PAID_BY, BET_ON_0, BET_ON_1, BET_ON_2, BET_ON_3, BET_ON_4, BET_ON_5, BET_ON_6, BET_ON_7, BET_ON_8, BET_ON_9, BET_ON_10, BET_ON_11, BET_ON_12, BET_ON_13, BET_ON_14, BET_ON_15, BET_ON_16, BET_ON_17, BET_ON_18, BET_ON_19, BET_ON_20, BET_ON_21, BET_ON_22, BET_ON_23, BET_ON_24, BET_ON_25, BET_ON_26, BET_ON_27, BET_ON_28, BET_ON_29, BET_ON_30, BET_ON_31, BET_ON_32, BET_ON_33, BET_ON_34, BET_ON_35, BET_ON_36, BET_ON_00, BET_ON_RED, BET_ON_BLACK, BET_ON_ODD, BET_ON_EVEN, BET_ON_1_18, BET_ON_19_36, BET_ON_1ST12, BET_ON_2ND12, BET_ON_3RD12, BET_ON_2TO1L, BET_ON_2TO1M, BET_ON_2TO1H, JP_PARTICIPATE, JP_POT_VALUE_GOLD, JP_POT_VALUE_SILVER, JP_POT_VALUE_BRONZE, JP_FEE, JP_GAMBLE_RESULT, JP_GAMBLE_RESULT_CAPTION, JP_WIN_SUM, TICKET_PRICE, TICKET_MAX_POSSIBLE_WIN, TICKET_WIN, CURRENCY_TAG, TICKET_STATUS_STRING)"
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME";
    fbexport -S -D $BKPDB -P masteraloha -Q "$SELECT_SQL" -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -F - -R -Q "$INSERT_SQL";

    TABLE_NAME=INSTANCES
    SELECT_SQL="select                  INSTANCE_TYPE,INSTANCE_TYPE_NUMBER,INSTANCE_STATUS,INSTANCE_MAC_ADDRESS,INSTANCE_LISTEN_PORT,INSTANCE_PHYSICAL_IP,INSTANCE_SSH_PORT,INSTANCE_SCP_FOLDER,INSTANCE_VIDEO_URL,INSTANCE_AUDIO_URL,INSTANCE_ID,INSTANCE_SUPPORTED_FEATURES,INSTANCE_LAST_UPDATE_TIMESTAMP from $TABLE_NAME"
    INSERT_SQL="insert into $TABLE_NAME(INSTANCE_TYPE,INSTANCE_TYPE_NUMBER,INSTANCE_STATUS,INSTANCE_MAC_ADDRESS,INSTANCE_LISTEN_PORT,INSTANCE_PHYSICAL_IP,INSTANCE_SSH_PORT,INSTANCE_SCP_FOLDER,INSTANCE_VIDEO_URL,INSTANCE_AUDIO_URL,INSTANCE_ID,INSTANCE_SUPPORTED_FEATURES,INSTANCE_LAST_UPDATE_TIMESTAMP)"
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME";
    fbexport -S -D $BKPDB -P masteraloha -Q "$SELECT_SQL" -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -F - -R -Q "$INSERT_SQL";

    #ta tabela ima BLOB field, zato moramo kopirati z -S, verbatim copy
    TABLE_NAME=MULTIGAME_CONFIG
    SELECT_SQL="select                  CLIENT_ID,GAME_ID,GAME_NAME,RTP,PLACEMENT_ORDER,AVAILABLE,ENABLED,VALIDATION,SAS_LOCK,DISPLAY_TYPE,CUSTOM_CONFIG from $TABLE_NAME";
    INSERT_SQL="insert into $TABLE_NAME(CLIENT_ID,GAME_ID,GAME_NAME,RTP,PLACEMENT_ORDER,AVAILABLE,ENABLED,VALIDATION,SAS_LOCK,DISPLAY_TYPE,CUSTOM_CONFIG)";
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME";
    #fbexport -S -D $BKPDB -P masteraloha -Q "$SELECT_SQL" -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -F - -R -Q "$INSERT_SQL";
    fbexport -S -D $BKPDB -P masteraloha -V $TABLE_NAME -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -V $TABLE_NAME -F - -R

    #!!!!!!! ta tabela ima BLOB field, zato moramo kopirati z -S, verbatim copy
    TABLE_NAME=MULTIGAME_CONFIG_V7
    #TALE SQL SE UPORABI SAMO ZA TESTIRANJE CE JE STRKTURA ENAKA V STARI KOT V NOVI
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME";
#ce nam gre COMPARE_SQL skozi identično na obeh bazah, uporabimo verbatim copy, verbatim bo skopiral blob stoplec..
#ce COMPARE_SQL ne gre skozi, potem kopiramo preko INSERT_SQL, ta NE bo skopiral CONFIG stoplca, bo pa vse ostale, ki so zgodovinsko noter in ohranil stukturo nove tabele
#potrebno bi bilo pogruntati kako prekopirati BLOB stolpec, preko INSERT_SQL-a!!!
    COMPARE_SQL="select RDB\$FIELD_NAME from RDB\$RELATION_FIELDS where RDB\$RELATION_NAME = '$TABLE_NAME'"
    echo "*******************executing test SQL: $SELECT_SQL"
    SQLOUT1=`mktemp`
    SQLOUT2=`mktemp`
    fbexport -Si -D $BKPDB -P masteraloha -Q "$COMPARE_SQL" -F $SQLOUT1
    fbexport -Si -D /ROULETTE_DATA.GDB -P masteraloha -Q "$COMPARE_SQL" -F $SQLOUT2
    if [ "`md5sum $SQLOUT1|cut -d' ' -f1-1`" == "`md5sum $SQLOUT2|cut -d' ' -f1-1`"  ]; then
       echo "***SEEMS THAT $TABLE_NAME IS WITH THE SAME STRUCTURE IN BOTH TABLES; DOING VERBATIM COPY TO PRESERVE BLOB FIELDS...."
       #VERBATIM COPY, ta kopira tudi strukturo tabele iz SOURCE_DB, in lahko to delamo samo ko je struktura v obeh enaka!
       fbexport -S -D $BKPDB -P masteraloha -V $TABLE_NAME -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -V $TABLE_NAME -F - -R
    else
       echo "***$TABLE_NAME IS DIFFERENT BY STRUCTURE; DOING TXT COPY OF ONLY KNOWN FIELDS."
       echo
       echo
       echo "!!!!! PER GAME CONFIGURATION WILL BE LOST DURING COPY !!!!!"
       echo
       echo
       SELECT_SQL="select                  GAME_ID,CONFIGURATION_NAME,PLACEMENT_ORDER,ENABLED,SAS_LOCK,DISPLAY_TYPE,PRESET,VERSION,STATUS,CHANGED_BY from $TABLE_NAME";
       INSERT_SQL="insert into $TABLE_NAME(GAME_ID,CONFIGURATION_NAME,PLACEMENT_ORDER,ENABLED,SAS_LOCK,DISPLAY_TYPE,PRESET,VERSION,STATUS,CHANGED_BY)";
       fbexport -S -D $BKPDB -P masteraloha -Q "$SELECT_SQL" -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -F - -R -Q "$INSERT_SQL";
    fi
    rm -rf $SQLOUT1
    rm -rf $SQLOUT2


    TABLE_NAME=GAMES
    SELECT_SQL="select                  GAME_ID,GAME_NAME,TYPE,APPROVED_FOR,FEATURES from $TABLE_NAME"
    INSERT_SQL="insert into $TABLE_NAME(GAME_ID,GAME_NAME,TYPE,APPROVED_FOR,FEATURES)"
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME";
    fbexport -S -D $BKPDB -P masteraloha -Q "$SELECT_SQL" -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -F - -R -Q "$INSERT_SQL";

    TABLE_NAME=GAMES_V7
    SELECT_SQL="select                  GAME_ID,GAME_NAME,TYPE,APPROVED_FOR,FEATURES,AVAILABLE from $TABLE_NAME"
    INSERT_SQL="insert into $TABLE_NAME(GAME_ID,GAME_NAME,TYPE,APPROVED_FOR,FEATURES,AVAILABLE)"
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME";
    fbexport -S -D $BKPDB -P masteraloha -Q "$SELECT_SQL" -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -F - -R -Q "$INSERT_SQL";

    TABLE_NAME=GAMES_PACKAGE_V7
    SELECT_SQL="select                  GAME_ID,UPDATE_MODE,PACKAGE_STATUS,STATUS_DESCRIPTION,LAST_UPDATE_TIMESTAMP,PACKAGE_VERSION,PACKAGE_BUILD_TIMESTAMP,DOWNLOADED_TIMESTAMP,ON_REBOOT_ACTION from $TABLE_NAME"
    INSERT_SQL="insert into $TABLE_NAME(GAME_ID,UPDATE_MODE,PACKAGE_STATUS,STATUS_DESCRIPTION,LAST_UPDATE_TIMESTAMP,PACKAGE_VERSION,PACKAGE_BUILD_TIMESTAMP,DOWNLOADED_TIMESTAMP,ON_REBOOT_ACTION)"
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME";
    fbexport -S -D $BKPDB -P masteraloha -Q "$SELECT_SQL" -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -F - -R -Q "$INSERT_SQL";

    TABLE_NAME=GAMES_OWNERSHIP_V7
    SELECT_SQL="select                  GAME_ID,CONFIGURATION_NAME,VALIDATION,VALID_FROM,VALID_TO,CREATED_TIMESTAMP,NOTE,CONTRACT_TYPE from $TABLE_NAME"
    INSERT_SQL="insert into $TABLE_NAME(GAME_ID,CONFIGURATION_NAME,VALIDATION,VALID_FROM,VALID_TO,CREATED_TIMESTAMP,NOTE,CONTRACT_TYPE)"
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME";
    fbexport -S -D $BKPDB -P masteraloha -Q "$SELECT_SQL" -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -F - -R -Q "$INSERT_SQL";

    TABLE_NAME=RAM_CLEAR_HISTORY
    SELECT_SQL="select                  COUNTING_FROM_TIMESTAMP,RAM_CLEAR_TIMESTAMP,DENOMINATION,CURRENCY,COUNTER_SCALE,\"IN\",OUT,BET,WIN from $TABLE_NAME"
    INSERT_SQL="insert into $TABLE_NAME(COUNTING_FROM_TIMESTAMP,RAM_CLEAR_TIMESTAMP,DENOMINATION,CURRENCY,COUNTER_SCALE,\"IN\",OUT,BET,WIN)"
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME";
    fbexport -S -D $BKPDB -P masteraloha -Q "$SELECT_SQL" -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -F - -R -Q "$INSERT_SQL";

    TABLE_NAME=LANG_MESSAGES2
    SELECT_SQL="select                  LANG_MSG_ID,LANG_MSG_SUB_ID,LANG_ID,LANG_MESSAGE  from $TABLE_NAME"
    INSERT_SQL="insert into $TABLE_NAME(LANG_MSG_ID,LANG_MSG_SUB_ID,LANG_ID,LANG_MESSAGE)"
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME";
    fbexport -S -D $BKPDB -P masteraloha -Q "$SELECT_SQL" -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -F - -R -Q "$INSERT_SQL";

    TABLE_NAME=LOG
    SELECT_SQL="select                  LOG_ID,LOG_TYPE_ID,CLIENT_IP,GAME_ID,LOG_TIME,LOG_MESSAGE  from $TABLE_NAME"
    INSERT_SQL="insert into $TABLE_NAME(LOG_ID,LOG_TYPE_ID,CLIENT_IP,GAME_ID,LOG_TIME,LOG_MESSAGE)"
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from $TABLE_NAME";
    fbexport -S -D $BKPDB -P masteraloha -Q "$SELECT_SQL" -F - | fbexport -I -D /ROULETTE_DATA.GDB -P masteraloha -F - -R -Q "$INSERT_SQL";

#BRISEMO LAST DB COMMAND, ker tabele COMMANDS nismo prekopirali in index-i več ne štimajo
#echo "delete from CONFIGURATION where ATTR_NAME='LAST_DB_COMMAND'" > /RouletteMaster/sqlexec.sh
echo -n "Deleting LAST_DB_COMMAND from CONFIGURATION because we did not copy COMMANDS table..."
fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from CONFIGURATION where ATTR_NAME='LAST_DB_COMMAND'";
echo "  Done."

    #prepisemo se vrednosti generatorjev
    fbcopy d sysdba:masteraloha@$BKPDB sysdba:masteraloha@/data/ROULETTE_DATA.GDB|grep "G:GEN_"|fbcopy C sysdba:masteraloha@$BKPDB sysdba:masteraloha@/data/ROULETTE_DATA.GDB

    #resetiramo generator command queue
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "delete from COMMAND_QUEUE";
    fbexport -X -D /ROULETTE_DATA.GDB -p masteraloha -q "set generator GEN_COMMAND_QUEUE_ID to 1";

    #naredimo backup & restore nove baze
    /opt/firebird/bin/gfix -sh -force 0 /ROULETTE_DATA.GDB -user sysdba -password masteraloha
    /opt/firebird/bin/gfix -v /ROULETTE_DATA.GDB -user sysdba -password masteraloha
    /opt/firebird/bin/gfix -v -f /ROULETTE_DATA.GDB -user sysdba -password masteraloha
    /opt/firebird/bin/gfix -mend /ROULETTE_DATA.GDB -user sysdba -password masteraloha
    /opt/firebird/bin/gbak -b -g /ROULETTE_DATA.GDB "/data/TEMPBKP.GBK" -user sysdba -password masteraloha
    rm /data/ROULETTE_DATA.TMP -rf > /dev/null 2>&1
    /opt/firebird/bin/gbak -Z 2>&1 | grep "Firebird 2.5" >/dev/null
    if [ $? -ne 0 ]; then #FIREBIRD KI NE PODPIRA FIX_FSS_METADATA
        /opt/firebird/bin/gbak -c -rep "/data/TEMPBKP.GBK" /data/ROULETTE_DATA.TMP -user sysdba -password masteraloha
    else
        /opt/firebird/bin/gbak -c -rep "/data/TEMPBKP.GBK" /data/ROULETTE_DATA.TMP -user sysdba -password masteraloha -FIX_FSS_METADATA WIN1250 -FIX_FSS_DATA WIN1250
    fi
    mv /data/ROULETTE_DATA.TMP /data/ROULETTE_DATA.GDB -f
    /opt/firebird/bin/gfix -online /ROULETTE_DATA.GDB -user sysdba -password masteraloha
    rm -rf /data/TEMPBKP.GBK > /dev/null 2>&1 

#fi

#ponovno zazenemo symmetricds in zahtevamo reverse_load
#/RouletteMaster/host_execute.sh nohup /home/ROULETTE/symmetricds-keepalive.sh &
#sleep 1
#/RouletteMaster/symmetric_init_load_reload.sh REVERSELOAD

#delete older backups than 60 days
find /data/ROULETTE_DATA* ! -name ROULETTE_DATA.GDB -type f -mtime +60 -delete

echo
echo "DB REPAIR LEVEL2 was finished successfully."
echo

sleep 3
exit 0

