#!/bin/sh


get_AP_info()
{
	#run airodump in the background for 5s
	(airodump-ng -a --essid "$SSID" wlan0mon 2> APtracker.txt) &
	PID=$!
	clear
	echo "Getting MAC and Channel..."	#user message
	sleep 10s
	kill -TERM $PID

	#parse and store
	MAC=`grep "$SSID" APtracker.txt | tail -1 | cut -d' ' -f2`
	CHAN=`grep "$SSID" APtracker.txt | tail -1 | cut -d' ' -f26`
}


draw_signal()
{
	COUNT=0
	
	#for each column of the window
	while [ "$COUNT" -lt "$COLS" ] ; do

		#increment counter
		COUNT=$(($COUNT + 1))

		#draw 100% signal line
		if [ "$COUNT" -eq "$(( $COLS - $(( $TIMES * 100 )) ))" ] ; then
			echo -n "\e[38;5;007m"
			echo -n "█"
			echo -n "\e[38;5;255m"
		
		#draw signal bar
		elif [ "$COUNT" -gt $(( $COLS - $(($SIGNAL_P * $TIMES)) )) ] ; then
			
			#draw red-yellow-green-blue-turqouise signal
			if [ "$COUNT" -gt $(( $COLS - $((20 * $TIMES )) )) ] ; then
				echo -n "\e[38;5;001m"
				echo -n "█"
				echo -n "\e[38;5;255m"
			elif [ "$COUNT" -gt $(( $COLS - $((40 * $TIMES )) )) ] ; then
				echo -n "\e[38;5;003m"
				echo -n "█"
				echo -n "\e[38;5;255m"
			elif [ "$COUNT" -gt $(( $COLS - $((60 * $TIMES )) )) ] ; then
				echo -n "\e[38;5;002m"
				echo -n "█"
				echo -n "\e[38;5;255m"
			elif [ "$COUNT" -gt $(( $COLS - $((80 * $TIMES )) )) ] ; then
				echo -n "\e[38;5;004m"
				echo -n "█"
				echo -n "\e[38;5;255m"
			else
				echo -n "\e[38;5;006m"
				echo -n "█"
				echo -n "\e[38;5;255m"
			fi
		
		#draw blank space
		else
			echo -n " "
		fi
	done
}

check_state()
{
	#if screen has been resized
	if [ "$COLS" -ne `tput cols` ] ; then
			clear	#clear the screen
			COLS=`tput cols`
			TIMES=$(( COLS / 100 ))
	fi
			
	#check if screen is big enough
	if [ "$TIMES" -eq "0" ] ; then
		return "1"
	fi
	

	#if the device isn't in monitor mode
	if [ -z "`ifconfig | grep wlan0mon`" ] ; then
		return "2"
	else
		if [ -z "$MAC" ] || [ -z "$CHAN" ] ; then
			return "3"	
		fi
	fi

	

	return "0"

}

get_reading() 
{
	#run airodump in background for 1s
	(airodump-ng -a --bssid "$MAC" --channel "$CHAN" wlan0mon 2> APtracker.txt) &
	PID=$!
	sleep 1s
	kill -TERM $PID

	#parse signal
	SIGNAL=`grep "$MAC" APtracker.txt | tail -1 | cut -d- -f2 | cut -d' ' -f1`
	
	#check for reading
	if [ -n "$SIGNAL" ] ; then
		SIGNAL_P=$((100 - $SIGNAL))
		draw_signal
		return "0"
	else
		return "1"
	fi

}


#get argument
SSID=$1

COLS=0
DRAWN=0

#check for argument
if [ -n "$SSID" ] ; then

	while : ; do
		check_state
		case "$?" in
			3)
				get_AP_info
				check_state
				while [ "$?" -eq "3" ] ; do
					clear
					echo "Error getting MAC and Channel, trying again."
					get_AP_info
					check_state
				done
				clear
				DRAWN=0
				;;
			2)
				#attempt to put card into monitor mode
				echo "Starting monitor mode..."
				airmon-ng start wlan0 > /dev/null

				#if this fails, loop until the problem is resolved
				check_state
				while [ "$?" -eq "2" ] ; do
					clear
					echo "Error starting monitor mode, check interface."
					sleep 0.5s
					airmon-ng start wlan0 > /dev/null
					check_state
				done
				clear
				DRAWN=0
				;;
			1)
				check_state
				while [ "$?" -eq 1 ] ; do
					clear
					echo "Window must be at least 100 cols wide."
					sleep 0.5s
					check_state
				done
				clear
				DRAWN=0
				;;
			0)
				get_reading
				if [ "$?" -eq "1" ] ; then
					if [ "$DRAWN" -eq "0" ] ; then
						clear
					fi
					echo -n "Network unavailable or out of range."
				else
					DRAWN="1"
				fi
				;;
		esac
	done

			


else
	#error message for no argument
	echo "Please provide ESSID of AP as an argument."
fi
