#!/bin/bash


# w1.txt s4.sh 2

# echo $1

wfile=$1
fn=$2
# let tmo=$3
tmo=$3
run_first=$4
opath=$(pwd)
path=$5
# qx=$(stat -c "%s %Y %n" $wfile)
# bash $2
# echo $3

# v2=z

# ${$1}

function rnx(){
	echo z

}



function rn_fn(){
	if [ "$path" != "" ]; then
	 cd $path
	# else
		# echo else $path
	fi
	bash $fn
	cd $opath
}
function rn(){
	let cnt=0
	echo TMO $tmo
	# if [ "$tmo" -gt "0.1" ]; then
	# if [[ "$tmo" -gt "0.1" ]]; then
	# if (( $(echo "$num1 > $num2" |bc -l) )); then
	if (( $(echo "$tmo > 1.1" |bc -l) )); then
	 # cd $path
	 # tmo=1
	 echo GTR $n
	else
	 # echo !GTR $n
	 echo EXITING! $wfile $fn $tmo $run_first $opath $path
      

	 # echo EXITING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	 # echo EXITING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	 return
	 tmo=1
		# echo else $path
	fi


	if [ "$run_first" != "" ]; then
	 # cd $path
	 rn_fn
	# else
		# echo else $path
	fi
	file_stat=$(stat -c "%s %Y %n" $wfile)||"DNE"
	file_stat0=$(stat -c "%s %Y %n" $wfile)||"DNE"


while true; do
	# file_stat0=$(stat -c "%s %Y %n" $wfile)||DNE
	# file_stat0=$(stat -c "%s %Y %n" $wfile ||echo "DNE")
	file_stat0=$(stat -c "%s %Y %n" $wfile 2>/dev/null ||echo "DNE")
	if [ "$file_stat0" != "$file_stat" ]; then
	 # cd $path
	 file_stat=$file_stat0
	 rn_fn
	# else
		# echo else $path
	fi
	sleep 1
	# echo RN $cnt tmo:$tmo fn $fn $wfile $file_stat
	# echo RN $cnt $zpath $v2
	let cnt=cnt+1
sleep $tmo
done 
}

function rn0(){
while true; do
echo C $chng

if [[ "$chng" == "tru"  ]]; then
		echo TRU...
gpull_loop
	# else
		# echo ELSE...
		#statements
fi

# flist
file_uptd
sleep $tmo0
done 
}





# echo J 
# echo J 
# echo J 
# echo J 
# echo J 
# echo J 
# echo J 
# echo J $*

# echo wfile      $wfile      
# echo fn         $fn   
# echo tmo        $tmo    
# echo run_first  $run_first          
# echo opath      $opath      
# echo path       $path     

# echo
# echo
# echo
# echo





# echo qx $qx
# rnx
rn
# rnx 