#!/bin/bash

# sr.sh

set vt=var_QWERTYUIOP

# if $rf
if [ "$rf" == "" ]; then
	 echo rf == ""
	 rf=rf1.sh
	else
	 echo rf != ""
fi
if [ "$log_file" == "" ]; then
	 echo log_file == ""
	 log_file=default_log.log
	else
	 echo log_file != ""
fi



# cd /usr/games/qsrv
declare -A arr
# PATH=$PATH:/usr/games:/usr/local/games:/snap/bin
# arr+=( ["key2"]=val2 ["key3"]=val3z )
function list_sp(){
	echo list_sp.o. ${!arr[@]}
	echo list_sp.i. ${arr[@]}
	for key in ${!arr[@]}; do
    	echo ${key} ${arr[${key}]}
	done
}
function discard(){
    	#statements
    echo $1
    echo $*
    echo ~~~
    rmv=$1
	for key in ${!arr[@]}; do
	    if [[ ${key} == ${rmv} ]]; then
    	echo rmv ${key} ${arr[${key}]}
    	unset arr[${key}]
    	fi


	done
}


function stop_all_sp(){
    # rmv=$1
    echo STOP ALL
	for key in ${!arr[@]}; do
		echo STOP all k $key
	    # if [[ ${key} == ${rmv} ]]; then
    	# echo rmv ${key} ${arr[${key}]}
    		kill ${arr[${key}]}
		kill -9 ${arr[${key}]}
    	unset arr[${key}]
    	# fi



	done
}
function restart_spk(){


	# echo RS1 $1
	# echo RS2 $2
	# echo RS3 $3
	# echo RS4 $4
	# echo RS5 $5
	# echo RS6 $6
	# echo RS7 $7
	# echo RS8 $8




	echo restart_spk $*

	stop_sp $*


	zpath=RS_x
	v2=RS_qwerty

	key=$1
	shift
	fn0=$1
	shift



	# bash $*.sh &
	# bash "$*" &
	# bash $fn0 "$*" &
	bash $fn0 "$@" &
	# bash $fn0 "$@" &
	# bash $fn0 $@ &
	# bash $fn0 '$*' &
	# 

	arr[$key]=$!

	echo rs $key



}
function restart_sp(){
	echo restart_sp $*
	# echo restart_sps $* >> tl2.log
	# 

	# stop_sp $*
	stop_sp $*


	zpath=RS_x
	v2=RS_qwerty


	# bash $*.sh &
	fn0=$1
	key=$1
	shift
	# bash $fn0 "$*" &
	bash $fn0 "$@" &
	# 
	echo RS $*
	echo RS 1 $1 2 $2 





	# key="abc"
	# arr+=( [$1]=$! )
	# arr+=( ${key}=$! )
	
	# arr+=( ["3"]="qwe123" )
	# arr+=( ["kz"]=$! )
	# arr+=( ["k2"]=val2 ["k3"]=val3z )
	# arr+=( ["k2"]=val2  )
	# arr[$1]=$!
	arr[$key]=$!
	# echo n ${!arr[$@]}
	# echo v ${arr[$@]}
	# echo v $@
	# echo $* started $1
	# echo $! z  ${arr[$1]}  started2
	# export arr
	# list_sp
	echo rs



}
function stop_sp(){
	echo stop_sp $* ... $1
	key=$1
	# echo sk $key
	# sk=${arr["$1"]}
	# echo poi $sk
	# k2="abc1"
	# echo kill ${arr[${k2}]}
	# echo kill ${arr[${key}]}
	# echo kill ${arr[$1]}
	echo kill ${arr[$1]} ZZZ $1
	kill ${arr[$1]}
	kill -9 ${arr[$1]}
	# kill -9 10233
	discard $1

	# echo ssp $*
}


rflag=true
# rflag2=t2

# function run_com(){
function run_com_0(){
	# bash rf1.sh
	bash $srvi_v$rf
}
# function run_com_0(){
function run_com(){
	# input="rf1.sh"
	input="$srvi_v$rf"
	# echo rcom0 >> tl2.log
	while IFS= read -r line
	do
	echo "$line"
	eline "$line"
	done < "$input"
	# echo rcom1 >> tl2.log
}

function rloop(){
	while $rflag; do
	# pull_loop
	echo $rflag
	echo r2 $rflag2

	sleep .8
	rflag=false

	done 

}
function fnx(){
	echo fnx
}


function eline(){
	echo eline2 $*
	# echo eline2 $* >> sre.log

	# echo zz 
	# $*
	# $*
	# $@
	eval $@
	# $@
}
export -f eline
export -f restart_sp
export -f restart_spk
export -f stop_sp
export -f discard
export -f list_sp




# function stop_all_sp


# export arr
function tr(){
	# tail -f rf1.txt | eline $*
	# tail -f rf1.txt | xargs -n 1 echo eline $*
	# tail -f rf1.txt | xargs -L 1 echo eline $*
	# tail -f rf1.txt | xargs -L 1 echo $* | eline 
	# tail -f rf1.txt | xargs -L 1 bash -c 'eline $*' 
	# tail -f rf1.txt | xargs -L 1 bash -c 'eline "$@"' {}
	# tail -f rf1.txt | xargs -L 1 bash -c 'eline "$@"' _ {}
	# tail -f rf1.txt | xargs -L 1 bash -c 'eline "$@"' {}
	# tail -f rf1.txt | xargs -E fnx -L 1 bash -c 'eline "$@"' {}
	# tail -f rf1.txt | xargs -E fnx -L 1 bash -c 'eline "$@"' {}
	run_com
let cnt=0
while true; do
	# echo tr run forever: $cnt >> logs/fout.txt
	# echo tr run forever: $cnt 
	# echo I1: $b
	let cnt=1+cnt

	sleep 12.8
done

while false; do
# while $rflag; do
	# pull_loop
	echo tr000 >> tl2.log
	list_sp
	echo tr001 >> tl2.log
	echo tail123
	tail --version >> tv.log
	xargs --version >> xv.log

	# tail -f rf1.txt | xargs -E fnx -L 1 echo z $*
	# echo tail456
	# echo tr0zzzzz >> tl2.log
	run_com
	# echo > rf1.txt
	# echo tzfin >> tl2.log
	done 

}

# arr+=( ["i1"]=10233 )
# arr+=( ["i1x"]=8634 )

# stop_sp k2
# arr+=(qwe)
# echo qqqqq >> tl3.log
# echo $HOME >> tl3.log
# echo $PATH >> tl3.log
# echo lq >> sre.log


list_sp
tr

