#!/bin/bash

DEFMODE=main

MODE=${2:-$DEFMODE}

IAPPASS=rotekucn
LOGIN=admin
PASS=12345

DB_FW=""
#MAIN_FW=./output/RTPS_05502xx.bin
MAIN_FW=./output/IBP.bin
SERVICE_FW=./output/SERVICE_05502xx.bin

function usage {
    echo -e "Usage: upload.sh {ip|mac} {full|main|db|service}\n\
    \tmain - update main firmware\n\
    \tdb - update doughter board firmware\n\
    \service - upload service firmware\n\
    \tfull - update both main and doughter boards firmware"
}

function wait_ip {
	while ! ping -c1 -W1 $IP > /dev/null
	do
		echo "Waiting IP.."	
		sleep 1
		((i++))
		if [ $i == 10 ]
		then
			echo -e "\r\nError. IP not found!"
			exit 1
		fi
	done
}

function need_login {
	curl -s --compressed -b /tmp/cookies.txt -c /tmp/cookies.txt http://$IP/index.html | grep -q "Авторизация"
}

function need_iap {
	curl -s --compressed -b /tmp/cookies.txt -c /tmp/cookies.txt http://$IP/index.html | grep -q "Параметры"
}

function log_out {
	curl -s -b /tmp/cookies.txt -c /tmp/cookies.txt http://$IP/logout.cgi > /dev/null
}

function log_in {
	curl -s -b /tmp/cookies.txt -c /tmp/cookies.txt -d "login=$LOGIN&password=$PASS" http://$IP/login.cgi > /dev/null
}

function boot_iap {
	curl -s -X GET -b /tmp/cookies.txt -c /tmp/cookies.txt http://$IP/fw_update.cgi > /dev/null
}

function upload_db_fw {
	echo $(curl --compressed http://$IP/upload.cgi -H 'Expect:' -F filedata=@$DB_FW --progress-bar)
}

function upload_main_fw {
	echo $(curl --compressed http://$IP/upload.cgi -H 'Expect:' -F filedata=@$MAIN_FW --progress-bar)
}

function upload_service_fw {
	TMP_FILE=$(mktemp -d /tmp/upload.XXXXX)/$(basename $MAIN_FW)
	cp $SERVICE_FW $TMP_FILE
	echo $(curl --compressed http://$IP/upload.cgi -H 'Expect:' -F filedata=@$TMP_FILE --progress-bar)
}

function goback {
	curl -s http://$IP/goback.cgi 
}


if [ $# -lt 1 ]
then
    echo "Error: Not enough parameters" 
    usage
    exit 1
fi

#Check passed parameter
if [[ "$1" =~ ^([a-fA-F0-9]{2}[:-]){5}[a-fA-F0-9]{2}$ ]]
then
    #Passed MAC
    MAC=$(echo $1 | tr '[:upper:]' '[:lower:]' | tr '-' ':')
    IP=$(sudo arp-scan -l | grep $MAC | cut -d$'\t' -f1)
elif [[ "$1" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]
then
    #Passed IP
    IP=$1
else
    echo "Error: Invalid parameter"
    usage
    exit 1
fi

ping -c1 -W1 $IP 2>/dev/null 1>/dev/null
if [ "$?" = 0 ]
then
  echo "Host found ($IP)"
else
  echo "Error: Host not found ($IP)"
  exit 1
fi


if need_login 
then
	echo "Logging in.."
	log_in
fi	
if need_iap
then
	echo "Reboot to IAP.."
	boot_iap
	sleep 10
	wait_ip
fi	

if [[ $MODE = "full" || $MODE = "db" ]]
then
	echo "Prog db firmware.."
	db_res=$(upload_db_fw)
	if [[ $db_res == "1" ]]
	then 
		echo "OK"
	else
		echo "Error"
		echo "Update failed!"
		exit 1
	fi
	sleep 1
fi

if [[ $MODE = "db" ]]
then
	goback
	echo "Successfully updated"
	exit 0
fi

if [[ $MODE = "full" || $MODE = "main" ]]
then
	echo "Prog main firmware.."
	main_res=$(upload_main_fw)
	if [[ $main_res == "1" ]]
	then 
		echo "OK"
	else 
		echo "Error"
		echo "Update failed!"
		exit 1
	fi
fi

if [[ $MODE = "service" ]]
then
	echo "Prog service firmware.."
	res=$(upload_service_fw)
	if [[ $res == "1" ]]
	then 
		echo "OK"
	else 
		echo "Error"
		echo "Update failed!"
		exit 1
	fi
	rm -rf $TMP_FILE
fi

echo "Successfully updated"
exit 0