#! /bin/bash

echo "You must have a correct installation of OMNET++ v4 (OMNET++'s configure script runs without error)."
echo "Please be sure that OMNET++ paths are set correctly."
echo "Optionally, you may also have a correct installation of Castalia v3.2. It is RECOMMENDED to have a non-modified installation."
echo "This install.bash script assumes that you have the .tgz archive in your current directory and will create a vidmodel directory."
read -p "**** OK to proceed with the installation from your current directory? (Y/N)" yesno

if [ "$yesno" = "y" ] || [ "$yesno" = "Y" ]; then
{
	current_dir=`pwd`

	if test -f wvsnmodel-v4.tgz
	then
		echo "**** Creating vidmodel directory"
		mkdir vidmodel
		echo "**** cd into vidmodel"
		cd vidmodel
		echo "**** un-tar-ing the archive"
		tar xvfz ../wvsnmodel-v4.tgz

		read -p "**** Do you want to try compiling the model (recommended)? (Y/N)" yesno

		if [ "$yesno" = "y" ] || [ "$yesno" = "Y" ]; then
		{
			echo "**** cd into wvsn-model-omnetpp-v4"
			cd wvsn-model-omnetpp-v4
			echo "**** cd into geometry"
			cd geometry
			echo "**** Producing .o files for geometric library"
			make obj
			cd ..
			echo "**** Generating Makefile for OMNET++ simulation model"
			opp_makemake -f
			echo "**** Compiling the basic version of the OMNET++ simulation model"
			make

			read -p "**** Was it successful? (Y/N)" yesno

			if [ "$yesno" = "y" ] || [ "$yesno" = "Y" ]; then
			{
				echo "##############################################"
				echo "Great! You are now done with the OMNET++ step."
				echo "You can begin working with the OMNET++ version"
				echo "##############################################"
			}
			else
			{
				echo "################################################################"
				echo "Possible problems are:"
				echo "- OMNET++ paths are not set correctly: check and type make again"
				echo "- no SDL library: install SDL and type make again"
				echo "################################################################"
			}
			fi
		}
		fi

		read -p "**** Do you want to install the Castalia version (you need Castalia v3.2)? (Y/N)" yesno

		if [ "$yesno" = "y" ] || [ "$yesno" = "Y" ]; then
		{
			echo "**** cd into Castalia-files"
			cd Castalia-files

			castalia_dir=`echo ~/Castalia-3.2`
			echo "**** Enter the ABSOLUTE path of your Castalia root installation ($castalia_dir): "
			read input
			[ -n "$input" ] && castalia_dir=$input
			echo "**** Using $castalia_dir"
			
			echo "**** Backup of original Castalia's SensorNetwork.ned file if any"
			mv $castalia_dir/src/SensorNetwork.ned $castalia_dir/src/SensorNetwork.ned.orig

			echo "**** Copying the new SensorNetwork.ned file"
			cp input/SensorNetwork.ned.custom $castalia_dir/src/SensorNetwork.ned

			echo "**** Deleting previous videoSensorNode folder in Castalia's src if any"
			rm -r $castalia_dir/src/node/application/videoSensorNode
			
			echo "**** Copying videoSensorNode(src) folder into Castalia's src/node/application folder"
			cp -r src-videoSensorNode $castalia_dir/src/node/application/videoSensorNode

			echo "**** Copying routing contrib aodv(src), gpsr(src) and leach(src) folders into Castalia's src/node/communication/routing folder"
			cp -r input/src/node/communication/routing/* $castalia_dir/src/node/communication/routing

			echo "**** Creating symbolic links to OMNET++ .cc and .h files"
			pushd $castalia_dir/src/node/application/videoSensorNode
			chmod +x createLinks
			./createLinks $current_dir
			echo "**** Renaming VideoSensorNode.ned.custom into VideoSensorNode.ned"
			mv VideoSensorNode.ned.custom VideoSensorNode.ned
			popd

			echo "**** Renaming previous videoSensor folder in Castalia's Simulation if any"
			mv $castalia_dir/Simulations/videoSensor $castalia_dir/Simulations/videoSensor-`date +%F-%T`

			echo "**** Copying videoSensor(Simulations) folder into Castalia's Simulations folder"
			cp -r Simulations-videoSensor $castalia_dir/Simulations/videoSensor
			
			echo "**** Copying images folder into Castalia's Simulations/videoSensor folder"
			cp -r ../images $castalia_dir/Simulations/videoSensor

			echo "**** Copying routing contrib test simulation folders into Castalia's Simulations folder"
			cp -r input/Simulations/* $castalia_dir/Simulations
						
			echo "**** Copying the makefrag file into Castalia root folder"
			cp input/makefrag $castalia_dir/makefrag.in

			echo "**** Copying the setenv script to set path for Castalia"
			cp input/setenv $castalia_dir

			read -p "**** Do you want to install the modified throughputTest application for testing GPSR? (Y/N)" yesno

			if [ "$yesno" = "y" ] || [ "$yesno" = "Y" ]; then
			{
				echo "**** Renaming original ThroughputTest.cc in ThroughputTest.cc.orig"
				mv $castalia_dir/src/node/application/throughputTest/ThroughputTest.cc  $castalia_dir/src/node/application/throughputTest/ThroughputTest.cc.orig
				echo "**** Copying modified ThroughputTest.cc"
				cp input/src/node/application/throughputTest/ThroughputTest.cc $castalia_dir/src/node/application/throughputTest
			}
			else
			{
				echo "You can always do it later, just replace src/node/application/throughputTest by input/src/node/application/throughputTest" 			
			}
			fi
			
			read -p "**** Do you want to try compiling the model (recommended)? (Y/N)" yesno

			if [ "$yesno" = "y" ] || [ "$yesno" = "Y" ]; then
			{
				echo "**** cd into Castalia folder"
				cd $castalia_dir
				echo "**** updating paths in makefrag file"
				c_dir="${current_dir//\//\\/}"
				sed "s/~/$c_dir/g" makefrag.in > makefrag
				echo "**** run makemake to generate Makefile"
				./makemake
				echo "**** Compiling the basic version of the Castalia simulation model"
				make

				read -p "**** Was it successful? (Y/N)" yesno

				if [ "$yesno" = "y" ] || [ "$yesno" = "Y" ]; then
				{
					echo "###############################################"
					echo "Great! You are now done with the Castalia step."
					echo "You can begin working with the Castalia version"
					echo "###############################################"
				}
				else
				{
					echo "################################################################"
					echo "Possible problems are:"
					echo "- OMNET++ paths are not set correctly: check and type make again"
					echo "- no SDL library: install SDL and type make again"
					echo "################################################################"
				}
				fi
			}
			else			
			{
				echo "################################################################"
				echo "Installation of Castalia part is finished. STILL NEED TO USE"
				echo "./makemake and add additional lines into the generated Makefile."
				echo "Please READ carefully the instructions in README.txt"
				echo "################################################################"
			}
			fi
		}
		fi

		echo "####################################################################"
		echo "Installation is finished. Please READ carefully the instructions at:"
		echo "http://web.univ-pau.fr/~cpham/WSN-MODEL/wvsn.html"
		echo "####################################################################"
	else
		echo "**** Cannot find the wvsnmodel-v4.tgz archive. ABORT."
	fi
}
else
{
  echo "**** Installation aborted by user. ABORT."
}
fi
