#! /bin/bash -ex

echo $1, $2



deploy_flag=0
req_flag=0
rm_flag=0

while getopts 'snd' flag; do
  case "${flag}" in
    s) deploy_flag=1 ;;
    n) req_flag=1 ;;
	d) rm_flag=1 ;;
  esac
done

# echo $1, $2, $3
if [[ $deploy_flag -eq 1 ]]
then
	i=0
	for (( i=1; $i <= $2; i++ ))
	do
		echo "console.log('running test $i')" > "testjs"$i".js"
	done

    for (( i=1; $i <= $2; i++ ))
	do
		curl -X POST http://10.129.2.182:8082/serverless/deploy  -F runtime=container   -F serverless="@/home/ub-01/mahendra/xanadu_coldstart/testfiles/testjs"$i".js" -F nicfunction=@/home/ub-01/mahendra/xanadu_coldstart/testfiles/test5nic.c  >> curl_output.txt
		echo "\n" >> curl_output.txt
	done

	cat curl_output.txt | awk '{ print substr( $0, 84 ) }' | grep [0-9]* -o >> fids.txt

	cnt=1
	echo "{'fids':[" >> fids.json
	while IFS="" read -r p || [ -n "$p" ]
	do
	#   printf '%s\n' "$p"
		# sudo ip netns exec ns_server python benchmark_dispatcher.py --fid $p --c 50 --t 100 --n 1
		echo $p" , "  >> fids.json

	done < fids.txt
	echo "]}" >> fids.json
fi
# ####
if [[ $req_flag -eq 1 ]]
then
	sudo ip netns exec ns_server python send_coldstart.py --c 1 --t 2 --n $2
	# python send_coldstart.py --c 1 --t 2 --n $2
fi

if [[ $rm_flag -eq 1 ]]
then
    rm -f curl_output.txt fids.txt
fi


# curl -X POST http://localhost:8080/serverless/deploy  -F runtime=container   -F serverless=@/home/pcube/mahendra/speedo_basic_coldstart/xanadu/testfiles/test1.js -F nicfunction=@/home/pcube/nilanjan/xanadu/testfiles/test5nic.c  >> curl_output.txt
# echo "\n" >> curl_output.txt
# curl -X POST http://localhost:8080/serverless/deploy  -F runtime=container   -F serverless=@/home/pcube/mahendra/speedo_basic_coldstart/xanadu/testfiles/test2.js -F nicfunction=@/home/pcube/nilanjan/xanadu/testfiles/test5nic.c   >> curl_output.txt
# echo "\n" >> curl_output.txt
# curl -X POST http://localhost:8080/serverless/deploy  -F runtime=container   -F serverless=@/home/pcube/mahendra/speedo_basic_coldstart/xanadu/testfiles/test3.js -F nicfunction=@/home/pcube/nilanjan/xanadu/testfiles/test5nic.c   >> curl_output.txt
# echo "\n" >> curl_output.txt
# curl -X POST http://localhost:8080/serverless/deploy  -F runtime=container   -F serverless=@/home/pcube/mahendra/speedo_basic_coldstart/xanadu/testfiles/test4.js -F nicfunction=@/home/pcube/nilanjan/xanadu/testfiles/test5nic.c   >> curl_output.txt
# echo "\n" >> curl_output.txt
# curl -X POST http://localhost:8080/serverless/deploy  -F runtime=container   -F serverless=@/home/pcube/mahendra/speedo_basic_coldstart/xanadu/testfiles/test5.js -F nicfunction=@/home/pcube/nilanjan/xanadu/testfiles/test5nic.c   >> curl_output.txt
# echo "\n" >> curl_output.txt
# curl -X POST http://localhost:8080/serverless/deploy  -F runtime=container   -F serverless=@/home/pcube/mahendra/speedo_basic_coldstart/xanadu/testfiles/test6.js -F nicfunction=@/home/pcube/nilanjan/xanadu/testfiles/test5nic.c     >> curl_output.txt
# echo "\n" >> curl_output.txt
# curl -X POST http://localhost:8080/serverless/deploy  -F runtime=container   -F serverless=@/home/pcube/mahendra/speedo_basic_coldstart/xanadu/testfiles/test7.js -F nicfunction=@/home/pcube/nilanjan/xanadu/testfiles/test5nic.c   >> curl_output.txt
# echo "\n" >> curl_output.txt
# curl -X POST http://localhost:8080/serverless/deploy  -F runtime=container   -F serverless=@/home/pcube/mahendra/speedo_basic_coldstart/xanadu/testfiles/test8.js -F nicfunction=@/home/pcube/nilanjan/xanadu/testfiles/test5nic.c   >> curl_output.txt
# echo "\n" >> curl_output.txt
# curl -X POST http://localhost:8080/serverless/deploy  -F runtime=container   -F serverless=@/home/pcube/mahendra/speedo_basic_coldstart/xanadu/testfiles/test9.js -F nicfunction=@/home/pcube/nilanjan/xanadu/testfiles/test5nic.c   >> curl_output.txt
# echo "\n" >> curl_output.txt
# curl -X POST http://localhost:8080/serverless/deploy  -F runtime=container   -F serverless=@/home/pcube/mahendra/speedo_basic_coldstart/xanadu/testfiles/test10.js -F nicfunction=@/home/pcube/nilanjan/xanadu/testfiles/test5nic.c   >> curl_output.txt



