#!/bin/bash

# . /home/ROULETTE/common.config #here is configuration of ROULETTE and FC5 locations
. /home/ROULETTE/terminal/terminal.config #here is configuration of TERMINAL folder locations


#send pre-RAMClear email
$HOME_DIR/host_execute.sh sudo chroot /home/ROULETTE/FC5  /RouletteMaster/daily_email_report.sh "Final report before RAM Clear"
$HOME_DIR/host_execute.sh 'sudo chroot /home/ROULETTE/FC5  /RouletteMaster/command.sh \"GAME PAUSE\"'
$HOME_DIR/host_execute.sh 'sudo chroot /home/ROULETTE/FC5  /RouletteMaster/command.sh \"RHRIB CBRAMCLEAR\"'
sleep 5 #da se poslje komanda na mehanske stevce, na control board

$HOME_DIR/host_execute.sh sudo chroot /home/ROULETTE/FC5 service crond stop

#za svico ne smemo brisati, pa ne brisemo nikoder!
#delete from JACKPOT_CLIENTS;
#delete from JACKPOT_PHASE_DATA;

#mora biti generator pred delete from, ker je generator atomic, se ne zaklene in lahko druga transakcija uskaci vmes
SQL_TO_EXEC=$(cat <<-END-OF-STRING
select RES_RESULT from CMD_NEW_2('RAM_CLEAR.SH', 'PLATFORM', 'RESET_MECHANICAL_CTG', NULL, NULL, NULL, 40, NULL);
delete from DIGITAL_COUNTERS where COUNTER_INDEX>=0;
delete from CONFIGURATION where (ATTR_NAME like 'MEMORY\_PACK\_SN' or ATTR_NAME like 'AFT\_CURRENT%' or ATTR_NAME like 'AFT\_AUTO\_CASHOUT\_STATE' or ATTR_NAME like 'AFT\_REGISTRATION\_%' or ATTR_NAME like 'AFT\_TRANSFER\_%' or ATTR_NAME like 'PREV\_RESTRICTED\_EXPIRATION' or ATTR_NAME like 'PREV\_POOL\_ID' or ATTR_NAME like 'AFT\_HISTORY\_%') and CLIENT_IP not like 'ROULETTE' ESCAPE '\';
execute procedure J_RESET('LEVEL1');
execute procedure J_RESET('LEVEL2');
execute procedure J_RESET('LEVEL3');
execute procedure J_RESET('LEVEL4');
SET GENERATOR GEN_LOG TO 10;
delete from LOG;
delete from CURRENT_GAME;
delete from BETS_HISTORY;
SET GENERATOR GEN_CREDIT_TRANSFERS TO 10;
delete from CREDIT_TRANSFERS;
delete from BUF_LOG_TOTALS_ALL_DAILY2;
SET GENERATOR GEN_GAME_ID TO 10;
delete from GAME_PHASE_DATA;
SET GENERATOR GEN_MONETARY TO 10;
delete from MONETARY;
delete from KIOSK_TICKETS;
delete from MULTIGAME_SAVE_DATA;
execute procedure FIX_GENERATORS;
select result from SET_PARAM('GLOBAL', 'RAM_CLEAR', 'DEMO', NULL, NULL, 11, 0, '0');
delete from CONFIGURATION where ATTR_NAME like 'DEMO' and CLIENT_IP not like 'GLOBAL';
delete from CONFIGURATION where ATTR_NAME like 'GAMEROUNDCOMPLETED' or ATTR_NAME like 'GAMEROUND_ID' or ATTR_NAME like 'LASTPLAYEDGAMEID';
update configuration set ATTR_STRING_VALUE='1' where ATTR_NAME = 'GAMEROUND_ID';
select result from SET_PARAM('GLOBAL', 'RAM_CLEAR', 'RAM_CLEAR_DATE', 'RAM Clear Date', 'Timestamp of last invoked RAM-clear.', 11, 0, current_timestamp);
select 'END' as result from LOG;
END-OF-STRING
)

echo $SQL_TO_EXEC | $HOME_DIR/sqlexec_from_terminal.sh;

#brisemo vso zgodovino terminalov
$HOME_DIR/host_execute.sh sudo $HOME_DIR/../mount_VER_ramdisks.sh umount 
$HOME_DIR/host_execute.sh sudo rm -rf $OLD_GAMES_DIR/10.5.10.*
$HOME_DIR/host_execute.sh sudo rm -rf $OLD_GAMES_DIR/15*
$HOME_DIR/host_execute.sh sudo rm -rf $OLD_GAMES_DIR/20*
$HOME_DIR/host_execute.sh sudo rm -rf $OLD_GAMES_DIR/*.log

#mora biti vse v eni komandi, ker nam bo master ugasnil postaje!!!
$HOME_DIR/host_execute.sh 'sudo chroot /home/ROULETTE/FC5  /RouletteMaster/command.sh \"RHRIB RELAY_OFF\" && sleep 5 && init 6;'
#$HOME_DIR/host_execute.sh 'sudo chroot /home/ROULETTE/FC5  /RouletteMaster/command.sh \"RHRIB RELAY_OFF\"'

exit 0
