
#!/bin/sh

#check SystemC installation

PWD=$(pwd)

INITIAL_PATH=$PWD

SYSTEMC_LIB=$SYSTEMC/lib-linux/libsystemc.a
SYSTEMC_H=$SYSTEMC/include/systemc.h

clear
echo ''
echo '  Wellcome to the installation script of the MPEG4 exploration example of the Multicube project!!'
echo ''
echo '  Several environment variables will be exported and added to your .bashrc '. 
echo '  To load them properly you should execute this script as ". install_example.sh" or '
echo '  "source install_example.sh" or you can type "bash" at the end of the installation'
echo '  Press enter to continue. Otherwise, press CTRL+C'. 
read OPTI

if [ "$SYSTEMC" = "" ]; then
	echo SYSTEMC variable not found. Installing SystemC
	SRC_FOUND=false
	LIB_FOUND=false
else
	if [ -f $SYSTEMC_LIB ]; then
		LIB_FOUND=true
		echo SystemC compiled library found	
	else
		LIB_FOUND=false
		echo SystemC compiled library not found	
	fi
	if [ -f $SYSTEMC_H ]; then
		echo SystemC headers found
		SRC_FOUND=true		
	else
		SRC_FOUND=false	
		LIB_FOUND=false
		echo SystemC headers not found	
	fi
	
fi

if [ $SRC_FOUND = true ]; then
	NUM=$(grep "SystemC Class Library (Rel. 2.2.0)"  $SYSTEMC/README | wc -l)
	if [  $NUM -gt 0 ]; then
		echo 'SystemC: Correct version'
	else
		echo 'SystemC: Invalid version '
		SRC_FOUND=false
		LIB_FOUND=false
	fi
fi
# Install SYSTEMC
if [ $SRC_FOUND = false ]; then

	# Download tgz file

	echo 'Do you prefer automatic or manual SystemC download?'
	OPTIONS="automatic manual"
	select SYSTEMC_DOWN in $OPTIONS; do

		if [ "$SYSTEMC_DOWN" = "manual" ]; then 
			echo 'Introduce the absolute path where SystemC tar file is:'
			read SYSTEMC_PATH
			break
		elif [ "$SYSTEMC_DOWN" = "automatic" ]; then
			echo 'Introduce the absolute path for SystemC installation:'
			read SYSTEMC_PATH
			if [ -d $SYSTEMC_PATH ]; then
				rm -rf systemc-2.2.0.tgz*
				wget http://www.teisa.unican.es/gim/en/scope/systemc-2.2.0.tgz
				mv systemc-2.2.0.tgz $SYSTEMC_PATH
				break
			else
				echo Wrong path
			fi
		else
			echo Wrong option $SYSTEMC_DOWN. Select 1 or 2.
		fi
	done

	if [ -f $SYSTEMC_PATH/systemc-2.2.0.tgz ]; then	
		export SYSTEMC=$SYSTEMC_PATH/systemc-2.2.0
		echo 'SystemC tgz file found.'
		cd $SYSTEMC_PATH
		tar xzvf systemc-2.2.0.tgz
		wget http://www.multicube.eu/other/systemc-gcc43.patch
		patch -p0 < systemc-gcc43.patch
		cd $INITIAL_PATH
	else
		echo 'Error: SystemC tgz file not found'
		return -1
	fi
fi

if [ $LIB_FOUND = true ]; then
	echo SystemC installation OK
else

	cd $SYSTEMC
	./configure
	make all install
	rm ../systemc-2.2.0.tgz*
	cd $INITIAL_PATH
	

	echo 'Checking SystemC installation'
	if [ "$SYSTEMC" = "" ]; then
		echo SYSTEMC variable not found. Installation failed
		return -1
	else
		if [ -f $SYSTEMC/lib-linux/libsystemc.a ]; then
			echo SystemC compiled library found. 
			echo SystemC Installation OK	
		else
			echo Fatal error: SystemC compiled library not found	
			return -1
		fi
	fi
fi



# Detect SCoPE

SCOPE_LIB=$SCOPE_HOME/scope/libscope.a
SCOPE_H=$SCOPE_HOME/scope/scope.h

if [ "$SCOPE_HOME" = "" ]; then
	echo SCOPE_HOME variable not found. Installing SCoPE
	SRC_FOUND=false
	LIB_FOUND=false
else
	if [ -f $SCOPE_LIB ]; then
		LIB_FOUND=true
		echo SCoPE compiled library found	
	else
		LIB_FOUND=false
		echo SCoPE compiled library not found	
	fi
	if [ -f $SCOPE_H ]; then
		SRC_FOUND=true
		echo SCoPE headers found	
	else
		SRC_FOUND=false	
		LIB_FOUND=false
		echo SCoPE headers not found	
	fi
fi

if [ $SRC_FOUND = true ]; then
	NUM=$(grep 'SCOPE_VERSION "1.1.5"'  $SCOPE_HOME/scope/scope_version.h | wc -l)
	if [  $NUM -gt 0 ]; then
		echo 'SCoPE: Correct version'
	else
		echo 'SCoPE: Invalid version'
		SRC_FOUND=false
		LIB_FOUND=false
	fi
fi

# Install SCoPE
if [ $SRC_FOUND = false ]; then


	# Download tgz file
	SCOPE_PATH=$PWD

	echo 'Do you prefer automatic or manual SCoPE download?'
	OPTIONS="automatic manual"
	select SCOPE_DOWN in $OPTIONS; do

		if [ "$SCOPE_DOWN" = "manual" ]; then 
			echo 'Introduce the absolute path where SCoPE tgz file is:'
			read SCOPE_PATH
			break
		elif [ "$SCOPE_DOWN" = "automatic" ]; then
			echo 'Introduce the absolute path for SCoPE installation:'
			read SCOPE_PATH
			if [ -d $SCOPE_PATH ]; then
				rm -rf SCoPE-v1.1.5.tgz*
				wget http://www.teisa.unican.es/gim/en/scope/SCoPE-v1.1.5.tgz
				mv SCoPE-v1.1.5.tgz $SCOPE_PATH
				break
			else
				echo Wrong path
			fi
		else
			echo Wrong option $SCOPE_DOWN. Select 1 or 2.
		fi
	done

	if [ -f $SCOPE_PATH/SCoPE-v1.1.5.tgz ]; then	
		export SCOPE_HOME=$SCOPE_PATH/SCoPE-v1.1.5
		export PATH=$PATH:'$SCOPE_PATH'/SCoPE-v1.1.5/bin
		echo 'SCoPE tgz file found.'
		cd $SCOPE_PATH
		tar xzvf SCoPE-v1.1.5.tgz
		cd $INITIAL_PATH
	else
		echo 'Error: SCoPE tgz file not found'
		exit -1
	fi
fi

if [ $LIB_FOUND = true ]; then
	echo SCoPE installation OK
else
		cd $SCOPE_HOME
		make all
		rm ../SCoPE-v1.1.5.tgz*
		cd $INITIAL_PATH
	
fi

# Check XML Plug-in

XML_LIB=$SCOPE_XML_PLUGIN/build/libloadxml.a
SRC_FOUND=true

if [ "$SCOPE_XML_PLUGIN" = "" ]; then
	echo SCOPE_XML_PLUGIN variable not found. Installing M3P
	SRC_FOUND=false
	LIB_FOUND=false
else
	if [ -f $XML_LIB ]; then
		LIB_FOUND=true
		echo XML Plug-in compiled library found	
	else
		LIB_FOUND=false
		echo XML Plug-in compiled library not found	
	fi
fi

if [ $SRC_FOUND = true ]; then
	NUM=$(grep "SCoPE XML PLUG-IN README (v1.0.5)"  $SCOPE_XML_PLUGIN/README | wc -l)
	if [  $NUM -gt 0 ]; then
		echo 'XML Plug-in: Correct version'
	else
		echo 'XML Plug-in: Invalid version '
		SRC_FOUND=false
		LIB_FOUND=false
	fi
fi

# Install XML Plug-in
if [ $SRC_FOUND = false ]; then

	# Download tgz file
	XML_PATH=$PWD

	echo 'Do you prefer automatic or manual M3P download?'
	OPTIONS="automatic manual"
	select M3P_DOWN in $OPTIONS; do

		if [ "$M3P_DOWN" = "manual" ]; then 
			echo 'Introduce the absolute path where M3P tgz file is:'
			read XML_PATH
			break
		elif [ "$M3P_DOWN" = "automatic" ]; then
			echo 'Introduce the absolute path for M3P installation:'
			read XML_PATH
			if [ -d $XML_PATH ]; then
				rm -rf m3p-v1.0.5.tgz*
				wget http://www.teisa.unican.es/gim/en/scope/m3p-v1.0.5.tgz
				mv m3p-v1.0.5.tgz $XML_PATH
				break
			else
				echo Wrong path
			fi
		else
			echo Wrong option $M3P_DOWN. Select 1 or 2.
		fi
	done

	if [ -f $XML_PATH/m3p-v1.0.5.tgz ]; then	
		export SCOPE_XML_PLUGIN=$XML_PATH/m3p-v1.0.5
		export ARP_HOME=$SCOPE_XML_PLUGIN/src/arp_lib/
		echo 'XML Plug-in tgz file found.'
		cd $XML_PATH
		tar xzvf m3p-v1.0.5.tgz
		cd $INITIAL_PATH
	else
		echo 'Error: XML Plug-in tgz file not found'
		return -1
	fi
fi

if [ $LIB_FOUND = true ]; then
	echo XML Plug-in installation OK
else
		cd $SCOPE_XML_PLUGIN
		make
		rm ../m3p-v1.0.5.tgz*
		cd $INITIAL_PATH
	
fi

# Check M3Explorer

M3Ex_LIB=m3explorer
SRC_FOUND=false
LIB_FOUND=false

if which m3explorer; then
	SRC_FOUND=true
 	LIB_FOUND=true
 	echo m3explorer found	
else
	LIB_FOUND=false
	echo m3explorer not found	
fi

# Install M3Explorer
if [ $SRC_FOUND = false ]; then

	# Download tgz file
	M3Ex_PATH=$PWD

	echo 'Do you prefer automatic or manual M3Explorer download?'
	OPTIONS="automatic manual"
	select M3Ex_DOWN in $OPTIONS; do

		if [ "$M3Ex_DOWN" = "manual" ]; then 
			echo 'Introduce the absolute path where M3Explorer gz file is:'
			read M3Ex_PATH
			break
		elif [ "$M3Ex_DOWN" = "automatic" ]; then
			echo 'Introduce the absolute path for M3Explorer installation:'
			read M3Ex_PATH
			if [ -d $M3Ex_PATH ]; then
				rm -rf m3explorer_release_1_0.tar.gz*
				wget http://home.dei.polimi.it/zaccaria/data/release_1_0/m3explorer_release_1_0.tar.gz
				mv m3explorer_release_1_0.tar.gz $M3Ex_PATH
				break
			else
				echo Wrong path
			fi
		else
			echo Wrong option $M3Ex_DOWN. Select 1 or 2.
		fi
	done

	if [ -f $M3Ex_PATH/m3explorer_release_1_0.tar.gz ]; then	
		echo 'XML Plug-in tgz file found.'
		cd $M3Ex_PATH
		tar xzvf m3explorer_release_1_0.tar.gz
		cd $INITIAL_PATH
	else
		echo 'Error: XML Plug-in tgz file not found'
		return -1
	fi
fi

if [ $LIB_FOUND = true ]; then
	echo M3Explorer installation OK
else
		cd $M3Ex_PATH/m3explorer
		mkdir build
		cd build
		../configure
		make all install
		cd $M3Ex_PATH/m3explorer
		mkdir bin
		cd bin
		ln -s ../build/image/bin/m3explorer
		export PATH=$PATH:$M3Ex_PATH/m3explorer/bin
		cd $M3Ex_PATH
		rm m3explorer_release_1_0.tar.gz*
		cd $INITIAL_PATH
fi

# Check MPEG example

# MPEG_LIB=m3explorer
SRC_FOUND=false
LIB_FOUND=false
# 
# if which m3explorer; then
# 	SRC_FOUND=true
#  	LIB_FOUND=true
#  	echo m3explorer found	
# else
# 	LIB_FOUND=false
# 	echo m3explorer not found	
# fi

# Install MPEG example
if [ $SRC_FOUND = false ]; then

	# Download tgz file
	MPEG_PATH=$PWD

	echo 'Do you prefer automatic or manual MPEG example download?'
	OPTIONS="automatic manual"
	select MPEG_DOWN in $OPTIONS; do

		if [ "$MPEG_DOWN" = "manual" ]; then 
			echo 'Introduce the absolute path where MPEG example tgz file is:'
			read MPEG_PATH
			break
		elif [ "$MPEG_DOWN" = "automatic" ]; then
			echo 'Introduce the absolute path for MPEG example installation:'
			read MPEG_PATH
			if [ -d $MPEG_PATH ]; then
				rm -rf Multimedia_exploration.tgz*
				wget http://www.teisa.unican.es/gim/en/scope/Multimedia_exploration.tgz
				mv Multimedia_exploration.tgz $MPEG_PATH
				break
			else
				echo Wrong path
			fi
		else
			echo Wrong option $MPEG_DOWN. Select 1 or 2.
		fi
	done

	if [ -f $MPEG_PATH/Multimedia_exploration.tgz ]; then	
		export IMEC_EXAMPLE_PATH=$MPEG_PATH/Multimedia_exploration/libs
		echo 'MPEG example tgz file found.'
		cd $MPEG_PATH
		tar xzvf Multimedia_exploration.tgz
		cd $INITIAL_PATH
	else
		echo 'Error: MPEG example tgz file not found'
		return -1
	fi
fi

if [ $LIB_FOUND = true ]; then
	echo MPEG example installation OK
else
		export IMEC_EXAMPLE_PATH=$MPEG_PATH/Multimedia_exploration/libs
		cd $MPEG_PATH/Multimedia_exploration
		make
		cd $MPEG_PATH
		rm Multimedia_exploration.tgz*
		cd $INITIAL_PATH
fi

echo 'Installation complete'

echo 'Do you want to start the exploration process?'
OPTIONS="yes no"
select EXPL in $OPTIONS; do

	if [ "$EXPL" = "yes" ]; then 
		cd $MPEG_PATH/Multimedia_exploration/exploration
		make clean
		make run_integration
		make clean
		make run_report
		cd $INITIAL_PATH
		break
	elif [ "$EXPL" = "no" ]; then
		break
	else
		echo Wrong option $EXPL. Select 1 or 2.
	fi
done

echo 'Do you want to modify the $HOME/.bashrc file for future use?'
OPTIONS="yes no"
select EXPL2 in $OPTIONS; do


	if [ "$EXPL2" = "yes" ]; then
		if [ -f $HOME/.bashrc ]; then
			echo ' ' >> $HOME/.bashrc
		
			if [ ! "$SYSTEMC_PATH" = "" ]; then
				echo ' exporting SYSTEMC '
				echo ' export SYSTEMC='$SYSTEMC_PATH'/systemc-2.2.0 ' >> $HOME/.bashrc
			fi
			if [ ! "$SCOPE_PATH" = "" ]; then	
				echo ' exporting SCOPE_HOME '
				echo ' export SCOPE_HOME='$SCOPE_PATH'/SCoPE-v1.1.5 ' >> $HOME/.bashrc
				echo ' adding SCOPE_HOME/bin to the bash '
				echo ' export PATH=$PATH:'$SCOPE_PATH'/SCoPE-v1.1.5/bin ' >> $HOME/.bashrc
			fi
			if [ ! "$XML_PATH" = "" ]; then
				echo ' exporting SCOPE_XML_PLUGIN '
				echo ' export SCOPE_XML_PLUGIN='$XML_PATH'/m3p-v1.0.5 ' >> $HOME/.bashrc
				echo ' adding SCOPE_XML_PLUGIN/build to the LD_LIBRARY_PATH '
				echo ' export LD_LIBRARY_PATH='$XML_PATH'/m3p-v1.0.5/build ' >> $HOME/.bashrc
				echo ' export ARP_HOME='$SCOPE_XML_PLUGIN'/src/arp_lib/ '  >> $HOME/.bashrc
			fi

			if [ ! "$M3Ex_PATH" = "" ]; then
				echo ' export PATH=$PATH:'$M3Ex_PATH'/m3explorer/bin ' >> $HOME/.bashrc 
			fi
			echo 'Added required environment variables to bashrc file'
		else
			echo 'Plase, add required environment variables to your starting shell file:'
			echo ' export SYSTEMC='$SYSTEMC_PATH'/systemc-2.2.0 '
			echo ' export SCOPE_HOME='$SCOPE_PATH'/SCoPE-v1.1.5 '
			echo ' export SCOPE_XML_PLUGIN='$XML_PATH'/m3p-v1.0.5 ' 
			echo ' export ARP_HOME='$SCOPE_XML_PLUGIN'/src/arp_lib/ ' 
			echo ' export PATH=$PATH:'$SCOPE_PATH'/SCoPE-v1.1.5/bin ' 
			echo ' export PATH=$PATH:'$M3Ex_PATH'/m3explorer/bin ' 
			echo ' export LD_LIBRARY_PATH='$XML_PATH'/m3p-v1.0.5/build ' 
		fi
	else
		echo 'Plase, add required environment variables to your starting shell file:'
		echo ' export SYSTEMC='$SYSTEMC_PATH'/systemc-2.2.0 '
		echo ' export SCOPE_HOME='$SCOPE_PATH'/SCoPE-v1.1.5 '
		echo ' export SCOPE_XML_PLUGIN='$XML_PATH'/m3p-v1.0.5 ' 
		echo ' export ARP_HOME='$SCOPE_XML_PLUGIN'/src/arp_lib/ ' 
		echo ' export PATH=$PATH:'$SCOPE_PATH'/SCoPE-v1.1.5/bin ' 
		echo ' export PATH=$PATH:'$M3Ex_PATH'/m3explorer/bin ' 
		echo ' export LD_LIBRARY_PATH='$XML_PATH'/m3p-v1.0.5/build ' 
		return 0
	fi

	break
done
return 0

