#!/bin/bash
# checking for Guru object with GuruTime in the future and move it to JAIL_FOLDER
_self="${0##*/}"
echo "$_self is called"

RIGURU_FOLDER=$HOME"/RiGuru"
SEARCH_FOLDER=$RIGURU_FOLDER"/repository/"
JAIL_FOLDER=$RIGURU_FOLDER"/OutOfSyncGuruTime"

#   convert a decimal to a base 36 number in all capital
function decimal_to_base36(){
	BASE36=($(echo {0..9} {A..Z}));
	arg1=$@;
	for i in $(bc <<< "obase=36; $arg1"); do
		echo -n ${BASE36[$(( 10#$i ))]}
	done && echo
}

# RiGuruTime start at 19900101T000000, delta from linux time = 631152000 seconds
# CurrentGuruTime=$(decimal_to_base36 $((date +%s) - 631152000)   # in seconds
# CurrentGuruTime=$(decimal_to_base36 $(expr 1609459200 - 631152000))   # 20210101
# CurrentGuruTime=$"G6GIO0"     # 20210101

# cueernt GuruTime + 24 hrs
CurrentGuruTime=$(decimal_to_base36 $(date +%s) - 631152000 + 68400)

mkdir -p $JAIL_FOLDER

for file in $(find $SEARCH_FOLDER -type f -name *gat -print)
 do
	name_no_ext=${file%.*} 
	GuruTime=${name_no_ext: -8: 6}
	if [[ "$GuruTime" > "$CurrentGuruTime" ]]; then
		mv "$name_no_ext.gat" $JAIL_FOLDER
		mv "$name_no_ext.gob" $JAIL_FOLDER
	fi
 done

rm $RIGURU_FOLDER"/local/main.jnl"
rm $RIGURU_FOLDER"/local/main.bak"
