#!/bin/sh
CURDIR=`pwd`
echo "Installation: BIOSIM"
echo "--------------------"
echo ""
echo "Biologisch orientierter neuronaler Netzwerk-Simulator"
echo ""
echo -n "<Deutsch/English>  : (D/E)  <Default = D> : "
read LANGUAGE
if [ "$LANGUAGE" = "" ] ; then
	LANGUAGE=D
fi
if [ "$LANGUAGE" != "D" -a "$LANGUAGE" != "d" ] ; then
	LANGUAGE=E
fi
echo ""
if [ "$LANGUAGE" = "D" ] ; then
	echo -n "Installationsverzeichnis fr BIOSIM: "
else
	echo -n "Installation directory for BIOSIM: "
fi
read INSTDIR
if [ "$INSTDIR" = "" ] ; then
	INSTDIR=$CURDIR
fi
if [ "$LANGUAGE" = "D" ] ; then
	echo "Installationsverzeichnis und Datenverzeichnis werden angelegt"
else
	echo "Installation directory and data directory will be created"
fi
if [ ! -d "$INSTDIR" ] ; then
	mkdir "$INSTDIR"
fi
if [ ! -d "$INSTDIR/DATA" ] ; then
	mkdir "$INSTDIR/DATA"
fi
if [ "$LANGUAGE" = "D" ] ; then
	echo "Kopiere Hilfsdateien"
	cp ./biosim.hlp.de $INSTDIR/biosim.hlp
	cp ./HELP/helptext.de $INSTDIR/helptext
	cp ./SOURCES/text.de ./SOURCES/text.h
else
	echo "Copy help files"
	cp ./biosim.hlp.us $INSTDIR/biosim.hlp
	cp ./HELP/helptext.us $INSTDIR/helptext
	cp ./SOURCES/text.us ./SOURCES/text.h
fi
if [ "$LANGUAGE" = "D" ] ; then
	echo "Kopiere Kanaldateien in das Installationsverzeichnis ..."
else
	echo "Copying channel files into installation directory ..."
fi
cp ./*.channel "$INSTDIR"
cp ./ask* "$INSTDIR"
if [ "$LANGUAGE" = "D" ] ; then
	echo "Kopiere Beispiele in das Datenverzeichnis ..."
else
	echo "Copying example files into data directory ..."
fi
cp ./DATA/* "$INSTDIR/DATA"
if [ "$LANGUAGE" = "D" ] ; then
	echo "Kopiere Resourcedateien in das Heimatverzeichnis ..."
	cp ./HELP/HelpServer.de $HOME/HelpServer
else
	echo "Copying resource files into home directory ..."
	cp ./HELP/HelpServer.us $HOME/HelpServer
fi
if [ -f $HOME/Biosim ] ; then
	rm $HOME/Biosim
fi
if [ `expr $INSTDIR : '/.*'` -eq 0 ] ; then
	INSTDIR=${CURDIR}/${INSTDIR}
fi
sed s+/BIOSIMDIR+$INSTDIR+ ./Biosim.tmpl >$HOME/Biosim
if [ "$LANGUAGE" = "D" ] ; then
	echo "Welche X11-Version ist auf ihrem Rechner installiert ?"
	echo -n "Default-Wert ist Version 4: "
else
	echo "Which X11-Release is installed on your computer ?"
	echo -n "Default value is Release 4: "
fi
read X11VER
if [ "$X11VER" = "" ] ; then
	X11VER=4
fi
if [ "$LANGUAGE" = "D" ] ; then
	echo "In welchem Verzeichnis befinden sich die X11-Bibliotheken ?"
	echo -n "Default-Wert ist /usr/lib/X11 : "
else
	echo "In which directory reside the X11 libraries ?"
	echo -n "Default value is /usr/lib/X11 : "
fi
read X11LIB
if [ "$X11LIB" = "" ] ; then
	X11LIB=/usr/lib/X11
fi
if [ "$LANGUAGE" = "D" ] ; then
	echo "In welchem Verzeichnis befinden sich die Motif-Bibliotheken ?"
	echo -n "Default-Wert ist /usr/lib/X11 : "
else
	echo "In which directory reside the Motif libraries ?"
	echo -n "Default value is /usr/lib/X11 : "
fi
read MOTIFLIB
if [ "$MOTIFLIB" = "" ] ; then
	MOTIFLIB=/usr/lib/X11
fi
if [ "$LANGUAGE" = "D" ] ; then
	echo "In welchem Verzeichnis befinden sich die X11-Include-Dateien als Unterverzeichnis X11 ?"
	echo -n "Default-Wert ist /usr/include : "
else
	echo "In which directory reside the X11 include files as a subdirectory X11 ?"
	echo -n "Default value is /usr/include : "
fi
read X11INCL
if [ "$X11INCL" = "" ] ; then
	X11INCL=/usr/include
fi
if [ "$LANGUAGE" = "D" ] ; then
	echo "In welchem Verzeichnis befinden sich die Motif-Include-Dateien als Unterverzeichnis Xm ?"
	echo -n "Default-Wert ist /usr/include : "
else
	echo "In which directory reside the Motif include files as a subdirectory Xm ?"
	echo -n "Default value is /usr/include : "
fi
read MOTIFINCL
if [ "$MOTIFINCL" = "" ] ; then
	MOTIFINCL=/usr/include
fi
if [ "$LANGUAGE" = "D" ] ; then
	echo "Welchen Compiler wollen Sie benutzen ?"
	echo "Standard C-Compiler (cc) oder GNU-C-Compiler (gcc)"
	echo -n "Default-Wert ist gcc (cc/gcc) : "
else
	echo "Which compiler do you want to use ?"
	echo "Standard -C-Compiler (cc) or GNU-C-Compiler (gcc)"
	echo -n "Default value is cc (cc/gcc) : "
fi
read COMPILER
if [ "$COMPILER" = "" ] ; then
	COMPILER=gcc
fi
if [ "$LANGUAGE" = "D" ] ; then
	echo "Welche Compiler-Optionen wollen Sie benutzen ?"
	if [ "$COMPILER" = "cc" ] ; then
		echo -n "Default-Wert ist -O : "
	else
		echo -n "Default-Wert ist -O2 : "
	fi
else
	echo "Which compiler options do you want to use ?"
	if [ "$COMPILER" = "cc" ] ; then
		echo -n "Default value is -O : "
	else
		echo -n "Default value is -O2 : "
	fi
fi
read COPTIONS
if [ "$COPTIONS" = "" ] ; then
	if [ "$COMPILER" = "cc" ] ; then
		COPTIONS=-O
	else
		COPTIONS=-O2
	fi
fi
clear
if [ "$LANGUAGE" = "D" ] ; then
	echo "Konfigurationsparameter:"
	echo "------------------------"
	echo " "
	echo "Installationsverzeichnis: $INSTDIR"
	echo "X11-Version $X11VER"
	echo "X11-Bibiliotheksverzeichnis: $X11LIB"
	echo "X11-Include-Datei-Verzeichnis: $X11INCL"
	echo "Motif-Bibliotheksverzeichnis: $MOTIFLIB"
	echo "Motif-Include-Datei-Verzeichnis: $MOTIFINCL"
	echo "Compiler: $COMPILER"
	echo "Compileroptionen: $COPTIONS"
	echo " "
	echo -n "Sind alle Angaben korrekt ? <j/n> : "
	read answer
	if [ "$answer" != "j" ] ; then
		echo "Abbruch der Installation"
		exit
	else
		echo "Generierung der Make-Datei"
	fi
else
	echo "Configuration parameter:"
	echo "------------------------"
	echo " "
	echo "Installation directory: $INSTDIR"
	echo "X11-Release $X11VER"
	echo "X11-libraries directory: $X11LIB"
	echo "X11-Include files directory: $X11INCL"
	echo "Motif-libraries directory: $MOTIFLIB"
	echo "Motif-Include files directory: $MOTIFINCL"
	echo "Compiler: $COMPILER"
	echo "Compiler options: $COPTIONS"
	echo " "
	echo -n "Is this correct ? <y/n> : "
	read answer
	if [ "$answer" != "y" ] ; then
		echo "Abortion of installation"
		exit
	else
		echo "Makefile will be generated"
	fi
fi
if [ "$X11VER" = "5" ] ; then
	COPTIONS="$COPTIONS -DX11R5"
fi
if [ "$LANGUAGE" = "D" ] ; then
	COPTIONS="$COPTIONS -DDEUTSCH"
else
	COPTIONS="$COPTIONS -DENGLISH"
fi
echo "" | mail -s "BIOSIM-Installation: `uname`" bergdoll@zxa.basf-ag.de
cd ./HELP
(echo "# Makefile for HELPSERVER"
echo " "
echo "# -- HELPSERVER Installation Directories and Libraries --"
echo " "
echo "HELPSERVERDIR = $INSTDIR"
echo "XLIB = $X11LIB"
echo "MLIB = $MOTIFLIB"
echo "XH = $X11INCL"
echo "MH = $MOTIFINCL"
echo "CC = $COMPILER"
echo "CCFLAGS = $COPTIONS"
echo " ") >Makefile.header
cat Makefile.header Makefile.tmpl >Makefile
rm Makefile.header
echo "Erzeuge Hilfe-Server fuer BIOSIM ..."
make clean
make all && make install
if [ $? -ne 0 ] ; then
	echo " "
	echo " "
	if [ "$LANGUAGE" = "D" ] ; then
		echo "Der Hilfeserver konnte nicht erfolgreich installiert werden."
		echo "Ueberpruefen Sie, ob Ihre X11-Sourcen auch dort stehen"
		echo "wo Sie sie angegeben haben. Fragen Sie zuerst Ihren"
		echo "System-Administrator, ob er das Problem loesen kann."
		echo "Wenn nicht, so koennen Sie sich auch an mich wenden."
		echo "Meine E-Mail-Adresse ist: bergdoll@zxa.basf-ag.de"
	else
		echo "The Helpserver was not installed successfully !"
		echo "Look, if the X11-Sources are at the location you think it should be"
		echo "Address your questions primarily to your system administrator"
		echo "If he cannot help you, send me a mail, so I will try."
		echo "My E-Mail-Address is: bergdoll@zxa.basf-ag.de"
	fi
	exit
fi
cd ../SOURCES
$COMPILER $COPTIONS -DBATCH -o biosim2gnuplot biosim2gnuplot.c && strip biosim2gnuplot && mv biosim2gnuplot $INSTDIR
if [ "$LANGUAGE" = "D" ] ; then
	(echo "# Makefile fuer BIOSIM"
	echo " "
	echo "# -- BIOSIM Installationsverzeichnisse und Bibliotheken --"
	echo " "
	echo "BIOSIMDIR = $INSTDIR"
	echo "XLIB = $X11LIB"
	echo "MLIB = $MOTIFLIB"
	echo "XH = $X11INCL"
	echo "MH = $MOTIFINCL"
	echo "CC = $COMPILER"
	echo "CCFLAGS = $COPTIONS -DBATCH"
	echo " ") >Makefile.batch.header
	cat Makefile.batch.header Makefile.tmpl >Makefile
	rm Makefile.batch.header
	echo "Erzeuge Batch-Version von BIOSIM ..."
	/bin/sh -c "make batch" &
	pid=$!
	found=$pid
	while [ $pid -eq $found ] ; do
	found=`ps | expand | tr -s ' ' ' ' | cut -d' ' -f2 | grep $pid`
	if [ `echo $found | wc -c` -eq 1 ] ; then
		found=0
	fi
	sleep 10
	done
	(echo "# Makefile fuer BIOSIM"
	echo " "
	echo "# -- BIOSIM Installationsverzeichnisse und Bibliotheken --"
	echo " "
	echo "BIOSIMDIR = $INSTDIR"
	echo "XLIB = $X11LIB"
	echo "MLIB = $MOTIFLIB"
	echo "XH = $X11INCL"
	echo "MH = $MOTIFINCL"
	echo "CC = $COMPILER"
	echo "CCFLAGS = $COPTIONS"
	echo " ") >Makefile.header
	cat Makefile.header Makefile.tmpl >Makefile
	make clean
else
	(echo "# Makefile for BIOSIM"
	echo " "
	echo "# -- BIOSIM Installation Directories and Libraries --"
	echo " "
	echo "BIOSIMDIR = $INSTDIR"
	echo "XLIB = $X11LIB"
	echo "MLIB = $MOTIFLIB"
	echo "XH = $X11INCL"
	echo "MH = $MOTIFINCL"
	echo "CC = $COMPILER"
	echo "CCFLAGS = $COPTIONS -DBATCH"
	echo " ") >Makefile.batch.header
	cat Makefile.batch.header Makefile.tmpl >Makefile
	rm Makefile.batch.header
	echo "Creating Batch-Version of BIOSIM ..."
	/bin/sh -c "make batch" &
	pid=$!
	found=$pid
	while [ $pid -eq $found ] ; do
	found=`ps | expand | tr -s ' ' ' ' | cut -d' ' -f2 | grep $pid`
	if [ `echo $found | wc -c` -eq 1 ] ; then
		found=0
	fi
	sleep 10
	done
	(echo "# Makefile for BIOSIM"
	echo " "
	echo "# -- BIOSIM Installation Directories and Libraries --"
	echo " "
	echo "BIOSIMDIR = $INSTDIR"
	echo "XLIB = $X11LIB"
	echo "MLIB = $MOTIFLIB"
	echo "XH = $X11INCL"
	echo "MH = $MOTIFINCL"
	echo "CC = $COMPILER"
	echo "CCFLAGS = $COPTIONS"
	echo " ") >Makefile.header
	cat Makefile.header Makefile.tmpl >Makefile
	make clean
fi
rm Makefile.header
make all && make install
if [ $? -ne 0 ] ; then
	echo " "
	echo " "
	if [ "$LANGUAGE" = "D" ] ; then
		echo "BIOSIM konnte nicht erfolgreich installiert werden."
		echo "Ueberpruefen Sie, ob Ihre X11-Sourcen auch dort stehen"
		echo "wo Sie sie angegeben haben. Fragen Sie zuerst Ihren"
		echo "System-Administrator, ob er das Problem loesen kann."
		echo "Wenn nicht, so koennen Sie sich auch an mich wenden."
		echo "Meine E-Mail-Adresse ist: bergdoll@zxa.basf-ag.de"
	else
		echo "BIOSIM was not installed successfully !"
		echo "Look, if the X11-Sources are at the location you think it should be"
		echo "Address your questions primarily to your system administrator"
		echo "If he cannot help you, send me a mail, so I will try."
		echo "My E-Mail-Address is: bergdoll@zxa.basf-ag.de"
	fi
	exit
fi
echo " "
echo " "
if [ "$LANGUAGE" = "D" ] ; then
	echo "BIOSIM ist nun uebersetzt und installiert."
	echo "Der Aufruf erfolgt mit: cd $INSTDIR; biosim &"
	echo "Die Batch-Version rufen sie auf mit: cd $INSTDIR; biosim_batch <netzwerkdatei> <ausgabedatei>"
	echo "Online-Hilfe ist verfuegbar."
	echo "Die Dokumentation ist in der Datei biosim.ps enthalten"
	echo "und kann von einem postscriptfaehigen Drucker ausgedruckt werden."
	echo "Fragen und Anregungen sind willkommen"
	echo "Meine E-Mail-Adresse ist: bergdoll@zxa.basf-ag.de"
	echo "Viel Spass mit BIOSIM..."
else
	echo "BIOSIM is now ready for use."
	echo "To start BIOSIM type in: cd $INSTDIR; biosim &"
	echo "To start the Batch-Version of BIOSIM type in: cd $INSTDIR; biosim_batch <networkfile> <outputfile>"
	echo "Online-Help is available."
	echo "The documentation is contained in the postscript file biosim.ps"
	echo "and can be printed with every postscript printer"
	echo "Questions and stimulations are wellcome"
	echo "My E-Mail-Address is: bergdoll@zxa.basf-ag.de"
	echo "Happy computing with BIOSIM..."
fi
