#!/bin/sh
#
# (c)2009 BRUNIE-Software GmbH
#
# Installations Skript fr BRUNIE
# GT.M Version 5.3-003
#

if [ ! -f "brunie.gtm.installation" ] ; then
	echo ""
	echo -e "\aBitte zuerst in den Installationsordner wechseln!"
	echo ""
	echo "  cd <Installationsordner>"
	echo "  sh configure"
	echo ""
	exit
fi

ucis="LAG"               # Mgliche UCIs
ucilist="SYS"            # Standard-UCI

instdir=`pwd`

if [ `whoami` != "root" ] ; then
	echo -e "\aBitte als 'root' anmelden!"
	echo ""
	exit
fi

echo ""
echo "  Hiermit wird BRUNIE installiert, sowie notwendige"
echo "  Konfigurationen vorgenommen."
echo ""
echo "  Standardverzeichnis fr GT.M:    /usr/gtm"
echo "  Standardverzeichnis fr brunie:  /brunie"
echo ""
echo "  Konventionen der Datenbanken:    /brunie/<UCI>/*.*   -> Programme des UCIs"
echo "                                   /brunie/<UCI>.dat   -> Globals des UCIs"
echo "                                   /brunie/<UCI>.tmp   -> QTEMP*"
echo "                                                          TEMP*"
echo "                                                          QTP*"
echo "                                                          SA ...des UCIs"
echo "                                   /brunie/global.db   -> %* Globals"
echo ""
echo "  Standardbenutzer:                brunie"
echo "  Standardgruppe:                  brunie"
echo ""

echo -e "  Starten? (J/N) <J> \c"
read jn
if [ -z "$jn" ] ; then jn="J" ; fi
if [ "$jn" = "N" -o "$jn" = "n" ] ; then exit 0 ; fi

if [ -d "/brunie/system" ] ; then
	echo    "  ***ACHTUNG!***"
	echo    "  BRUNIE ist bereits installiert! Soll die aktuelle"
	echo -e "  Installation gelscht werden? (J/N) <N> \c"
	read jn
	    if [ -z "$jn" ] ; then jn="N" ; fi
	echo ""
	
	    if [ "$jn" = "J" -o "$jn" = "j" ] ; then 

		if [ -f /etc/init.d/brunie ]; then
		    echo "  ...stoppe BRUNIE-API"
		    /etc/init.d/brunie stop &>/dev/null
		    chkconfig --del brunie
		fi

		echo    "  Entfernung der aktuellen Installation..."
		echo -e "  - Sicherung des Verzeichnis /brunie in /tmp/backup/brunie... \c"
	        mkdir -p /tmp/backup &>/dev/null
	        rm -d -r /tmp/backup/brunie &>/dev/null
	        mv /brunie /tmp/backup/brunie  &>/dev/null
	        echo "Okay!"
	        echo -e "  - Sicherung des Verzeichnis /usr/gtm in /tmp/backup/gtm... \c"
	        rm -d -r /tmp/backup/gtm  &>/dev/null
	        mv /usr/gtm /tmp/backup/gtm  &>/dev/null
	        echo "Okay!"
	        echo "  - Benutzer/Gruppe brunie wurde gelöscht"
	        userdel brunie &>/dev/null
	        groupdel brunie &>/dev/null
	        echo "  Deinstallation erfolgreich!"
		exit 0
	    fi
fi

echo
echo

echo -e "  - BRUNIE-API installieren und aktivieren? (J/N) <J> \c"
read api
if [ -z "$api" ] ; then api="J" ; fi

for uci in $ucis
do
	echo -e "  - UCI '$uci' anlegen? (J/N) <J> \c"
	read jn
	if [ -z "$jn" ] ; then jn="J" ; fi

	if [ "$jn" = "J" -o "$jn" = "j" ] ; then
		ucilist="$ucilist $uci"
	fi
done

echo    "  - Verzeichnis der Updatedateien (*.rs/*.gs):"
echo -e "          <$instdir/update> \c"
read update
if [ -z "$update" ] ; then
	update=$instdir/update
fi

echo ""

echo -e "  Anlegen der Gruppe 'brunie'... \c"
groupadd -r brunie &>/dev/null
echo "Okay!"

echo -e "  Anlegen des Benutzers 'brunie'... \c"
useradd -d /home/brunie -g brunie -r -s /bin/bash brunie &>/dev/null
echo "Okay!"



echo "--------------------------------------------------"
echo -e "GT.M 64bit oder 32bit installieren? (64/32) <64> \c"
read bit
    if [ -z "$bit" ] ; then bit="64" ; fi
    
    if [ "$bit" = "64" ] ; then
	cd gtm64
	sh configure
    fi
    
    if [ "$bit" = "32" ] ; then
	cd gtm32
	sh configure
    fi



#echo -e "  Installation von GT.M... \c"
#cd gtm
#sh configure 


echo "Okay!"


echo -e "  Installation BRUNIE-API Dienst... \c"

cd $instdir
cp init.d/brunie /etc/init.d/brunie
chgrp root /etc/init.d/brunie
chown root /etc/init.d/brunie
chmod u+rwx,g+rwx /etc/init.d/brunie

echo "Okay!"

echo -e "  Kopieren der Systemskripte... \c"

cd $instdir
mkdir -p /brunie
mkdir -p /brunie/system
mkdir -p /brunie/log

cp system/* /brunie/system/ &>/dev/null
ln -f -s /brunie/system/gtm /usr/local/bin

echo "Okay!"

echo -e "  Kopieren der Systemroutinen... \c"

mkdir -p /brunie/SYS

cd $instdir
cp rout/SYS/* /brunie/SYS/ &>/dev/null
cp rout/global/* /usr/gtm &>/dev/null
cp rout/log/* /brunie/log/ &>/dev/null

echo "Okay!"

echo "  Vorbereitung der Datenbanken, bitte warten..."
cd /brunie

for uci in $ucilist
do
	sh $instdir/system/makeuci $uci force
done

cd /brunie
echo ""
echo -e "  Zuweisen der Rechte... \c"

chgrp -R brunie /brunie &>/dev/null
chown -R brunie /brunie &>/dev/null
chmod -R u+rwx,g+rwx /brunie &>/dev/null
chmod -R 755 /usr/gtm/_Q* &>/dev/null
chmod -R 755 /usr/gtm/_B* &>/dev/null
chown brunie:brunie /usr/gtm/_Q* &>/dev/null
chown brunie:brunie /usr/gtm/_B* &>/dev/null

echo "Okay!"

echo ""
echo -e "  Installation der Updatedateien, beginnen? (J/N) <J> \c"
read jn
if [ -z "$jn" ] ; then jn="J" ; fi

if [ "$jn" = "J" -o "$jn" = "j" ] ; then
	echo "  - Installation der Updates..."

	cd $update
	update=`pwd`
	rout=`ls *.rs *.rS *.Rs *.RS 2>/dev/null`
	glob=`ls *.gs *.gS *.Gs *.GS 2>/dev/null`

	cd /brunie
	export gtm_dist="/usr/gtm"
	export gtmgbldir="SYS"
	export gtmroutines="/usr/gtm"
	export gtm_ztrap_form="adaptive"
	export PATH="$PATH:/usr/gtm"

	for rs in $rout; do
		pos=`expr index $rs .`
		pos=`expr $pos - 1`
		uci=`expr substr $rs 1 $pos`
		uci=`tr a-z A-Z <<<$uci`

		if [ -f "$uci.dat" ] ; then
			echo -e "    - Routineninstallation in $uci... \c"
			su -c "$gtm_dist/mumps -r RR^%BRUNIE $update/$rs $uci &>/dev/null" brunie
			echo "Okay!"
		fi
	done

	for gs in $glob; do
		pos=`expr index $gs .`
		pos=`expr $pos - 1`
		uci=`expr substr $gs 1 $pos`
		uci=`tr a-z A-Z <<<$uci`

		if [ -f "$uci.dat" ] ; then
			echo -e "    - Globalinstallation in $uci... \c"
			su -c "$gtm_dist/mumps -r GR^%BRUNIE $update/$gs $uci &>/dev/null" brunie
			echo "Okay!"
		fi
	done
fi

echo

if [ "$api" = "J" -o "$api" = "j" ] ; then
	#insserv brunie
	chkconfig brunie on
	/etc/init.d/brunie stop &>/dev/null
	/etc/init.d/brunie start &>/dev/null
	/etc/init.d/brunie restart

fi

cd $instdir
