#!/bin/bash
for i in "$@"
do
case $i in
        -s=*|--spb=*)
        spb="${i#*=}"

        ;;
        -f=*|--ftmSupport=*)
        ftmSupport="${i#*=}"
        ;;
        -p=*|--scanPeriod=*)
        scanPeriod="${i#*=}"
        ;;
        --default)
        DEFAULT=YES
        ;;
        *)

        ;;
esac
done
spb=${spb:-2}
ftmSupport=${ftmSupport:-0}
scanPeriod=${scanPeriod:-5}
echo spb= $spb
echo ftmSupport= $ftmSupport
echo scanPeriod= $scanPeriod
if [ -f iw/conf  ]
then
	rm iw/conf
fi

SECONDS=0
iw/iw wlan1 scan |  awk -f processScan.awk spb=$spb ftmResponder=$ftmSupport
maxTargets=5
while true; do
	if [ "$SECONDS" -ge "$scanPeriod" ]
	then
		iw/iw wlan1 scan |  awk -f processScan.awk spb=$spb ftmResponder=$ftmSupport
		SECONDS=0
		echo ===================================== Scan surrounding APs ==================================================
	fi
	if [ -f iw/conf ]
	then
		numOfTargets=($(< iw/conf wc -l))
 		echo ===================================== Repeat ranging APs ==================================================
       		sleep 1
        	for i in `seq 1 ${maxTargets} ${numOfTargets}`; do
			sed -n "${i},$((i+maxTargets-1))p" iw/conf > iw/conf_part
   			iw/iw wlan1 measurement ftm_request iw/conf_part
		done
	else
		echo "There is no available targets!"
	fi
done
