#!/bin/bash

# Created by Naresh Mehta (https://w...content-available-to-author-only...h.se/)
#
# This is a script which launches multiple liso devices based on the information
# given in the file lisoSimSetup.csv. There are option parameters that can be passed
# to the script to launch only a specific set of devices from specific rows

csvFile="lisoSimSetup.csv"
startRow=1
stopRow=
count=0
startForking=0
generatedPath=
firstExecute=0
resetDevices=0

function help {
	echo "Helper script to launch Liso Device Simulators"
	echo ""
	echo "Usage:"
	echo "	$0 [-f <file>] [-s startRow] [-q stopRow] [-r]"
	exit 1
}

function createWorkingDirectory {
	generatedPath=$workingDir"/"$room_name

	if [[ ( "$resetDevices" -eq 1 ) ]]
		then
		rm -rf $generatedPath
	fi

	mkdir -p $generatedPath
}

function createFSStructure {
	pushd .
	cd $generatedPath
	cat > settings.json << EOF
{
    "public": {
        "role": "DEVICE",
        "runMode": "DEVICE",
        "applicationPort": $port,
        "applicationVersion": "v1.31"
    },
    "platform": "linux",
    "macAddress": "$macAddress",
    "evokoHomeIpAddress": "$eh_ipaddr_port",
    "simulation": true,
    "lisoIpAddress": "127.0.0.1:$port",
    "activeInterface": "ethernet",
    "workingDirectory": "$generatedPath"
}	
EOF

	mkdir -p liso_files
	cd liso_files

	cat > configuration.conf << EOF
{"room_id":"$room_id","ip_address_number":"$eh_ipaddr_port","room_name":"$room_name"}
EOF
	popd
}

function launchProcesses {
	(( count = count + 1 ))
	if [[ ( "$count" -eq "$startRow" ) ]]
		then
		(( startForking = 1))
	fi
	if [[ ( "$startForking" -eq 1 ) ]]
		then
		createWorkingDirectory $workingDir $room_name
		createFSStructure $generatedPath $port $macAddress $room_name $room_id $eh_ipaddr_port

#		Now that everything is setup, we will simply launch the processes one at a time
		PWD=`pwd`
		pushd .
		cd ..

		if [[ ( "$firstExecute" -eq 0 ) ]]
			then
			if [[ ( "$resetDevices" -eq 1 ) ]]
				then
				meteor reset
			fi
			gnome-terminal -x meteor --settings $generatedPath/settings.json --port $port &
			(( firstExecute = 1 ))
			sleep 2m
		else
			export MONGO_URL=$mondoDBURL
			gnome-terminal -x meteor --settings $generatedPath/settings.json --port $port &
			sleep 40s
		fi
		popd
#		(
#			cd ..
#			meteor --settings $generatedPath/settings.json --port $port
#		)
	fi
	if [[ ( "$count" -eq "$stopRow" ) ]]
		then
		(( startForking = 0))
	fi
}

while getopts ":f:s:q:r" o; do
    case "${o}" in
    	f)
			csvFile=${OPTARG}
			;;
        s)
            startRow=${OPTARG}
            ;;
        q)
            stopRow=${OPTARG}
            ;;
        r)
            resetDevices=1
            ;;
        *)
            help
            ;;
    esac
done
shift $((OPTIND-1))

IFS=,
[ ! -f $csvFile ] && { echo "$csvFile file not found"; exit 1; }
while read port macAddress room_id room_name eh_ipaddr_port workingDir mondoDBURL
do
	if [[ ! ( "$port" =~ ^\# ) && ! ( -z "$port" ) ]]
		then
		launchProcesses $port $macAddress $room_id $room_name $eh_ipaddr_port $workingDir $startRow $stopRow $mondoDBURL
	fi
done < $csvFile