#!/bin/bash


origuser=`logname`

installDist=0

if [ -f "install_mpmas.log" ]; then
	mv install_mpmas.log  install_mpmas.log.bak
fi

exec &> >( tee -a install_mpmas.log ) #log to screen and to log file

tar -xzf mpmasqlf.tar.gz
if [ "$1" = "-n" ] 
then
	echo "Skipping installation of dependencies (libraries and perl modules)"
	
else
	echo "Installation of dependencies (libraries and perl modules)"

	apt-get install cpanminus build-essential r-base libxml2-dev libperlio-gzip-perl  || exit 1
	apt install most gnumeric
	
	for m in  Hash::Merge Storable Data::Dumper Carp IO::Handle Term::ReadLine::Perl Term::ReadKey  Marpa::R2 Spreadsheet::Read Spreadsheet::ReadSXC Text::CSV_XS File::Util::Tempdir Number::Format Test::More
	do
		cpanm install $m || exit 1
	done

#	./mpmasqlf/install_perl_modules_for_mpmasql || exit 1

	for c in "$@"
	do
		if [ "$c" = "--sql" ] || [ "$c" = "-a" ] || [ "$c" = "--allDb" ] 
		then
			echo "Installing perl modules for use of SQL files"
			apt-get install mysql-client libmysqlclient-dev libdbd-mysql-perl
			for m in DBI DBD::mysql
			do
				cpanm install $m || exit 1
			done
		fi

		if [ "$c" = "--excel" ] || [ "$c" = "-a" ] || [ "$c" = "--allDb" ] 
		then
			echo "Installing perl modules for use of Excel files"

			for m in Spreadsheet::ParseExcel Spreadsheet::ParseXLSX
			do
				cpanm install $m || exit 1
			done
		fi
#		if [ "$c" = "--csv" ] || [ "$c" = "-a"]  || [ "$c" = "--allDb" ] 
#		then
#			echo "Installing perl modules for use of CSV files"
#			for m in Text::CSV_XS
#			do
#				cpanm install $m || exit 1
#			done
#		fi
		if [ "$c" = "--sparql" ] || [ "$c" = "-a" ]  || [ "$c" = "--allDb" ] 
		then
			echo "Installing perl modules for use of SPARQL"
			for m in  JSON::MaybeXS JSON::Any RDF::Query::Client
			do
				cpanm install $m || exit 1
			done
		fi
		if [ "$c" = "--parallel"  ] 
		then
			echo "Installing perl modules for parallization of mpmasql"
			for m in  Parallel::Iterator
			do
				cpanm install $m || exit 1
			done
		fi
	   
		if [ "$c" = "-x" ] 
		then
			echo "Extended installation of dependencies (libraries and perl modules) for mpmasdist"
			installDist=1
			apt-get install gfortran pgplot5 libx11-dev libxext-dev || exit 1
			for m in ExtUtils::MakeMaker  Math::CDF Math::Random::MT::Auto ExtUtils::F77 PGPLOT PDL PDL::Graphics::PGPLOT PDL::Slatec Algorithm::Munkres
			do
						cpanm install $m ||  exit 1
			done
			#	./mpmasqlf/install_perl_modules_for_mpmasql -x || exit 1

		fi
	done
fi

echo "Installing MPMAS/mpmasql"

exec 1>> install_mpmas.log 2> >(tee -a install_mpmas.log >&2)


rm -r /usr/local/lib/mpmasql > /dev/null 2>&1
#rm -r /usr/local/lib/mpmasqlX
rm -r /usr/local/lib/mpmasql4 > /dev/null 2>&1
rm -r /usr/local/lib/mpmasql/modules > /dev/null 2>&1
rm -r /etc/mpmasql > /dev/null 2>&1

rm -r /usr/local/bin/mqlsens > /dev/null 2>&1
rm -r /usr/local/bin/mqlresult > /dev/null 2>&1

rm -r /usr/local/bin/mqlsam > /dev/null 2>&1
rm -r /usr/local/bin/mpmasdist > /dev/null 2>&1
rm -r /usr/local/bin/mpmasdist2 > /dev/null 2>&1
rm -r /usr/local/bin/runcompact > /dev/null 2>&1
rm -r /usr/local/bin/mqltabtest > /dev/null 2>&1
rm -r /usr/local/bin/test_mpmas > /dev/null 2>&1



mkdir -p /usr/local/lib/mpmasql
mkdir -p /usr/local/lib/mpmasql/modules
mkdir -p /etc/mpmasql

cp -r -v mpmasqlf/scripts/* /usr/local/lib/mpmasql/
cp -v mpmasqlf/defaults/default.cfg /etc/mpmasql/
cp -v mpmasqlf/defaults/default.cll /etc/mpmasql/
cp -v mpmasqlf/defaults/default_po.cll /etc/mpmasql/
cp -v mpmasqlf/bin/mpmasMipSolver /usr/local/bin/
if [ -e  mpmasqlf/bin/mpmas ] ; then cp -v mpmasqlf/bin/mpmas /usr/local/bin/ ; fi
cp -v mpmasqlf/bin/mpmasMP /usr/local/bin
if [ -e  mpmasqlf/bin/mpmasCplexMP ] ; then cp -v mpmasqlf/bin/mpmasCplexMP /usr/local/bin ; fi
#cp -v mpmasqlf/bin/mpmasMipSolverOsl /usr/local/bin/
if [ -e  mpmasqlf/bin/mpmasMipSolverCplex ] ; then cp -v mpmasqlf/bin/mpmasMipSolverCplex /usr/local/bin/ ; fi
#cp -v mpmasqlf/bin/mpmasOsl /usr/local/bin/
if [ -e  mpmasqlf/bin/mpmasCplex ] ; then cp -v mpmasqlf/bin/mpmasCplex /usr/local/bin/ ; fi
#cp -v mpmasqlf/bin/mpmasMipSolver32 /usr/local/bin/
#cp -v mpmasqlf/bin/mpmas32 /usr/local/bin/
#cp -v mpmasqlf/bin/mpmas32MP /usr/local/bin/

chmod 755 /usr/local/bin/mpmasMipSolver*
chmod 755 /usr/local/bin/mpmas*
chmod 755 /usr/local/lib/mpmasql/*
chmod 755 /usr/local/lib/mpmasql/modules/*
chmod 755 /etc/mpmasql/default.cfg
chmod 755 /etc/mpmasql/default.cll


#mkdir -p /usr/share/gtksourceview-3.0/language-specs/
#for Ubuntu 18.04
cp -v lang/mpmasql_cll.lang /usr/share/gtksourceview-3.0/language-specs/
chmod -v 755 /usr/share/gtksourceview-3.0/language-specs/mpmasql_cll.lang
cp -v lang/mpmasql_var.lang /usr/share/gtksourceview-3.0/language-specs/
chmod -v 755 /usr/share/gtksourceview-3.0/language-specs/mpmasql_var.lang
cp -v lang/mpmasql_dat.lang /usr/share/gtksourceview-3.0/language-specs/
chmod -v 755 /usr/share/gtksourceview-3.0/language-specs/mpmasql_dat.lang
cp -v lang/mpmasql_prm.lang /usr/share/gtksourceview-3.0/language-specs/
chmod -v 755 /usr/share/gtksourceview-3.0/language-specs/mpmasql_prm.lang
cp -v lang/mpmasql_mpm.lang /usr/share/gtksourceview-3.0/language-specs/
chmod -v 755 /usr/share/gtksourceview-3.0/language-specs/mpmasql_mpm.lang

#for Ubuntu 22.04
cp -v lang/mpmasql_cll.lang /usr/share/gtksourceview-4/language-specs/
chmod -v 755 /usr/share/gtksourceview-4/language-specs/mpmasql_cll.lang
cp -v lang/mpmasql_var.lang /usr/share/gtksourceview-4/language-specs/
chmod -v 755 /usr/share/gtksourceview-4/language-specs/mpmasql_var.lang
cp -v lang/mpmasql_dat.lang /usr/share/gtksourceview-4/language-specs/
chmod -v 755 /usr/share/gtksourceview-4/language-specs/mpmasql_dat.lang
cp -v lang/mpmasql_prm.lang /usr/share/gtksourceview-4/language-specs/
chmod -v 755 /usr/share/gtksourceview-4/language-specs/mpmasql_prm.lang
cp -v lang/mpmasql_mpm.lang /usr/share/gtksourceview-4/language-specs/
chmod -v 755 /usr/share/gtksourceview-4/language-specs/mpmasql_mpm.lang



#if [ $origuser ] ; then
	#echo $origuser
	#chown -v $origuser $HOME/.local/share/gtksourceview-3.0/language-specs/mpmasql_cll.lang
	#chown -v $origuser $HOME/.local/share/gtksourceview-3.0/language-specs/mpmasql_var.lang
	#chown -v $origuser $HOME/.local/share/gtksourceview-3.0/language-specs/mpmasql_dat.lang
	#chown -v $origuser $HOME/.local/share/gtksourceview-3.0/language-specs/mpmasql_prm.lang
	#chown -v $origuser $HOME/.local/share/gtksourceview-3.0/language-specs/mpmasql_mpm.lang
#fi 




echo -e "#!/bin/sh\n/usr/local/lib/mpmasql/BuildMASInput.pl \"\$@\"\n" > /usr/local/bin/mpmasql3
chmod 755 /usr/local/bin/mpmasql3
#echo -e "#!/bin/sh\n/usr/local/lib/mpmasql/mpmasql3.pl \"\$@\"\n" > /usr/local/bin/mpmasqlX
#chmod 755 /usr/local/bin/mpmasqlX
echo -e "#!/bin/sh\n/usr/local/lib/mpmasql/mpmasql4.pl \"\$@\"\n" > /usr/local/bin/mpmasql4
chmod 755 /usr/local/bin/mpmasql4
echo -e "#!/bin/sh\n/usr/local/lib/mpmasql/mpmasql4.pl \"\$@\"\n" > /usr/local/bin/mpmasql
chmod 755 /usr/local/bin/mpmasql

echo -e "#!/bin/sh\n/usr/local/lib/mpmasql/RecheckMatrix.pl \"\$@\"\n" > /usr/local/bin/mqlmatrix 
chmod 755 /usr/local/bin/mqlmatrix
#echo -e "#!/bin/sh\n/usr/local/lib/mpmasql/SingleAgentsBatch.pl \"\$@\"\n" > /usr/local/bin/mpmasqlBatchSolve
#chmod 755 /usr/local/bin/mpmasqlBatchSolve

if [ $installDist = 0 ]
then
	echo -e "#!/bin/sh\n/usr/local/lib/mpmasql/DistributeAssets_v2.pl \"\$@\"\n" > /usr/local/bin/mpmasdist
	chmod 755 /usr/local/bin/mpmasdist
fi

echo -e "#!/bin/sh\n/usr/local/lib/mpmasql/testmql.pl \"\$@\"\n" > /usr/local/bin/mqltabtest
chmod 755 /usr/local/bin/mqltabtest

echo -e "#!/bin/sh\n/usr/local/lib/mpmasql/test_mpmas.pl \"\$@\"\n" > /usr/local/bin/test_mpmas
chmod 755 /usr/local/bin/test_mpmas

echo -e "#!/bin/sh\n/usr/local/lib/mpmasql/maptoxy \"\$@\"\n" > /usr/local/bin/maptoxy
echo -e "#!/bin/sh\n/usr/local/lib/mpmasql/mapbymap \"\$@\"\n" > /usr/local/bin/mapbymap
echo -e "#!/bin/sh\n/usr/local/lib/mpmasql/mapcount \"\$@\"\n" > /usr/local/bin/mapcount
echo -e "#!/bin/sh\n/usr/local/lib/mpmasql/mapdiff \"\$@\"\n" > /usr/local/bin/mapdiff


chmod 755 /usr/local/bin/maptoxy
chmod 755 /usr/local/bin/mapbymap
chmod 755 /usr/local/bin/mapcount
chmod 755 /usr/local/bin/mapdiff


rm -r mpmasqlf

exec &>/dev/tty

echo "Testing MPMAS call"
(mpmasql4 -v && echo "Looks good" ) || echo "Looks like it FAILED"

