#! /bin/sh
# Installation script for linux
# Does not check the dependants, just configures the compiler and server and generates makefile
thepath=$PWD
echo "------ Mesham Linux Installation Script ------"
echo "Mesham will be installed in directory $thepath"
echo ""
echo -n "Port number (default: 3000, press enter to accept this) >"
read portnum
if [ "$portnum" == "" ]; then
portnum=3000
fi
echo -n "Server hostname/IP (default: localhost, press enter to accept this) >"
read hostname
if [ "$hostname" == "" ]; then
hostname="localhost"
fi
if [ "$USER" == "root" ] ; then
rootaccess="y"
else
LOOPROOT=1
while [ $LOOPROOT == 1 ] ; do
echo -n "Do you have root access [y/n] (default: y, press enter to accept this) >"
read rootaccess
if [ "$rootaccess" == "" ] ; then 
rootaccess="y"
fi
if [ "$rootaccess" == "y" ]; then
LOOPROOT=0
fi 
if [ "$rootaccess" == "n" ]; then
LOOPROOT=0
fi 
done
fi
echo -n "What invokes the C compiler (default: gcc, press enter to accept this) >"
read cc
if [ "$cc" == "" ] ; then 
cc="gcc"
fi
echo -n "What invokes the MPI C compiler (default: mpicc, press enter to accept this) >"
read mpicc
if [ "$mpicc" == "" ] ; then 
mpicc="mpicc"
fi


echo "------ Please wait, generating configuration and make files ------"
# first generate the compiler configure header file
compileconfig=$thepath"/compiler/src/include/config.h"
touch $compileconfig
echo -n "#define classpathtopreprocessorclient \"" > $compileconfig
echo -n $thepath >> $compileconfig
echo "/compiler/java\0\"" >> $compileconfig
if [ "$rootaccess" == "y" ]; then
echo  "#define librarylocation \"\0\"" >> $compileconfig
echo  "#define headerlocation \"\0\"" >> $compileconfig
else
echo -n "#define librarylocation \"-L" >> $compileconfig
echo -n $thepath >> $compileconfig
echo "/libraries\0\"" >> $compileconfig
echo -n "#define headerlocation \"-I" >> $compileconfig
echo -n $thepath >> $compileconfig
echo "/libraries\0\"" >> $compileconfig
fi
echo -n "#define PORTNUM \"" >> $compileconfig
echo -n $portnum >> $compileconfig
echo "\0\"" >> $compileconfig
echo -n "#define serveraddress \"" >> $compileconfig
echo -n $hostname >> $compileconfig
echo "\0\"" >> $compileconfig
echo -n "#define mpicccmd \"" >> $compileconfig
echo -n $mpicc >> $compileconfig
echo "\0\"" >> $compileconfig
echo "#define anyothercompile \"\\0\"" >> $compileconfig
# now generate the server execution script
rm $thepath/server/linuxrunserver.sh
rm $thepath/server/winrunserver.bat
serverscript=$thepath"/server/runserver"
touch $serverscript
echo "#!/bin/sh" >  $serverscript
echo "portnum=\$1" >>  $serverscript
echo "if [ \"\$portnum\" == \"\" ]; then" >>  $serverscript
echo -n "portnum=" >>  $serverscript
echo  $portnum >>  $serverscript
echo "fi" >>  $serverscript
echo "cd translator" >>  $serverscript
echo "rm LReentry.dat" >>  $serverscript
echo "rm *.bak" >>  $serverscript
echo "java MeshamServer \$portnum" >>  $serverscript
# now generate the makefile
chmod +x $serverscript
makefile=$thepath"/makefile"
touch $makefile
echo -n "COMPILER=" > $makefile
echo $cc >> $makefile
echo "COMPOUT=compiler/bin/mc" >> $makefile
echo "COMPSRC=compiler/src" >> $makefile
echo "LIBLOC=libraries" >> $makefile
echo -n "MPICC=" >> $makefile
echo $mpicc >> $makefile
echo "ARG=-I\${LIBLOC}/source/headers -I\${LIBLOC} -I\${LIBLOC}/source/platform/linux/include" >> $makefile 
echo "LIBOUT=libmesham.a" >> $makefile
echo "SLIBOUT=libmesham.so" >> $makefile
echo "CP=cp" >> $makefile
echo "AR=ar" >> $makefile
echo "RANLIB=ranlib" >> $makefile
echo "all: comp library" >> $makefile
echo "comp:" >> $makefile
echo "	\${COMPILER} -o \${COMPOUT} \${COMPSRC}/main.c -I \${COMPSRC}/include/linux" >> $makefile
echo "library: static shared cleany" >> $makefile
echo "shared: compiledyn" >> $makefile
echo "	\${CP} \${LIBLOC}/source/\${SLIBOUT} \${LIBLOC}/\${SLIBOUT}" >> $makefile
echo "compiledyn:" >> $makefile
echo "	\${COMPILER} -shared -o \${LIBLOC}/source/\${SLIBOUT} *.o -lm" >> $makefile 
echo "static: cpit" >> $makefile
echo "cpit: ran" >> $makefile
echo "	\${CP} \${LIBLOC}/source/\${LIBOUT} \${LIBLOC}/\${LIBOUT}" >> $makefile
echo "ran: out.o" >> $makefile
echo "	\${RANLIB} \${LIBLOC}/source/\${LIBOUT}" >> $makefile
echo "out.o: maths other io errors string system" >> $makefile
echo "	\${AR} rc \${LIBLOC}/source/\${LIBOUT} *.o" >> $makefile
echo "errors: " >> $makefile
echo "	\${MPICC} -fPIC -c \${LIBLOC}/source/Errors/*.c \${ARG}" >> $makefile
echo "maths: " >> $makefile
echo "	\${MPICC} -fPIC -c \${LIBLOC}/source/Maths/*.c \${ARG}" >> $makefile
echo "other: " >> $makefile
echo "	\${MPICC} -fPIC -c \${LIBLOC}/source/Other/*.c \${ARG}" >> $makefile
echo "io: " >> $makefile
echo "	\${MPICC} -fPIC -c \${LIBLOC}/source/io/*.c \${ARG}" >> $makefile
echo "string: " >> $makefile
echo "	\${MPICC} -fPIC -c \${LIBLOC}/source/String/*.c \${ARG}" >> $makefile
echo "system: " >> $makefile
echo "	\${MPICC} -fPIC -c \${LIBLOC}/source/System/*.c \${ARG}" >> $makefile
echo "cleany: " >> $makefile
echo "	rm -rf *.o \${LIBLOC}/source/*.a \${LIBLOC}/source/*.so" >> $makefile
echo "clean: " >> $makefile
echo "	rm makefile" >> $makefile
if [ "$rootaccess" == "y" ] ; then
echo "install: ic lib lib64 slib slib64 header" >> $makefile
echo "ic: " >> $makefile
echo -n "	ln -s " >> $makefile
echo -n $thepath >> $makefile
echo  "/\${COMPOUT} /usr/bin/." >> $makefile
echo "lib: " >> $makefile
echo -n "	ln -s " >> $makefile
echo -n $thepath >> $makefile
echo "/\${LIBLOC}/\${LIBOUT} /lib/." >> $makefile
echo "lib64: " >> $makefile
echo -n "	ln -s " >> $makefile
echo -n $thepath >> $makefile
echo "/\${LIBLOC}/\${LIBOUT} /lib64/." >> $makefile
echo "slib: " >> $makefile
echo -n "	ln -s " >> $makefile
echo -n $thepath >> $makefile
echo "/\${LIBLOC}/\${SLIBOUT} /lib/." >> $makefile
echo "slib64: " >> $makefile
echo -n "	ln -s " >> $makefile
echo -n $thepath >> $makefile
echo "/\${LIBLOC}/\${SLIBOUT} /lib64/." >> $makefile
echo "header: " >> $makefile
echo -n "	ln -s " >> $makefile
echo -n $thepath >> $makefile
echo "/\${LIBLOC}/mesham.h /usr/include/." >> $makefile
fi
echo "------ Done ------"
echo "Compiler configuration can be found in $compileconfig"
echo "Server execution script  can be found in $serverscript"
echo "Make file is at $makefile"
echo ""
echo "Now type make all"
echo "(only if you have root access) Then login as root and type make install"
echo "Then type make clean"
exit 0