#!/bin/sh
#
#    INSTALL
#    Enterprise Messaging Gateway Installation script
#
#    Copyright (C) Braxo AB, Sweden.
#    Nordic Messaging Technologies is a trademark of Braxo AB.
#    https://nordicmessaging.se

# Default variables
VERSION=`cat VERSION`
EMGUSER="root"
EMGGROUP="root"
ROOTFILES=$(file * | egrep -v 'directory|INSTALL|VERSION' | awk 'BEGIN{FS=":"} {print $1}')
ETCFILES=$(ls etc)
EMGDIR="/etc/emg"
BINDIR="/usr/bin"
PROGRAMS=$(ls bin)
SCRIPTS=$(ls scripts)
BITS=8
prod=emg

# Get (y)es or (n)o from user
read_yn() {
	DEF=""
	[ "$1" != "" ] && {
		DEF="$1"
	}
	while read YN
	do
		[ "$YN" = "" ] && YN=$DEF
		case $YN in
		[jJyY])	return 0 ;;
		[nN])	return 1 ;;
		ja)	return 0 ;;
		yes)	return 0 ;;
		nej)	return 1 ;;
		no)	return 1 ;;
		*)		echo "Answer y or n." ;;
		esac
	done
}

ps -e|grep emgd >/dev/null && {
	echo 
	echo "The EMG server, emgd, is already running."
	echo
	exit 1
}

# Get input from user
read_ans() {
	read ANS
}

# Create a directory and its parent directories if necessary
# Set user to EMGUSER
create_dir() {
	OLDDIR=`pwd`
	DIR=$1

	[ -d "$DIR" ] && {
		return
	}

	echo $DIR | grep "^/" >/dev/null && cd /
	DIRS=`echo $DIR | tr '/' ' '`

	for dir in $DIRS
	do
		if [ ! -d "$dir" ]
		then
			if mkdir $dir 2>/dev/null 
			then
				chown $EMGUSER:$EMGGROUP $dir
			else
				echo "Could not create directory $dir."
				echo "Installation aborted!"
				exit 1
			fi
		fi
		cd $dir
	done

	cd $OLDDIR
}

# MAIN

echo
echo "* * *  Enterprise Messaging Gateway $VERSION - INSTALLATION  * * *"
echo

if grep "^emg:" /etc/passwd >/dev/null; then
	EMGUSER=emg
fi

echo "EMG can be owned by the user root or by another user."
echo "What user should be the owner of the EMG files? [$EMGUSER] "
read_ans
[ "$ANS" != "" ] && EMGUSER=$ANS

grep "^$EMGUSER:" /etc/passwd >/dev/null || {
	echo "Specified user must exist."
	echo "Please add user and run INSTALL again."
	exit 1
}

groupid=$(grep "^$EMGUSER:" /etc/passwd | awk 'BEGIN{FS=":"} {print $4}')
if [ ! -z $groupid ]; then
	EMGGROUP=$(grep :$groupid: /etc/group | awk 'BEGIN{FS=":"} {print $1}')
fi

echo "What group should be the group for the EMG files? [$EMGGROUP] "
read_ans
[ "$ANS" != "" ] && EMGGROUP=$ANS

grep "^$EMGGROUP:" /etc/group >/dev/null || {
	echo "Specified group must exist."
	echo "Please add group and run INSTALL again."
	exit 1
}

USERHOME=$(grep "^$EMGUSER:" /etc/passwd | awk 'BEGIN{FS=":"} {print $6}')
EMGDIR=$USERHOME/etc
BINDIR=$USERHOME/bin

echo "Where should Enterprise Messaging Gateway configuration"
printf "configuration files go? [$EMGDIR] "
read_ans
[ "$ANS" != "" ] && EMGDIR=$ANS

[ "$EMGDIR" = "/etc/emg" ] || {
	echo
	echo "Since the directory is not /etc/emg, the"
	echo "environment variable EMGDIR must be set."
	echo
}

printf "Where should EMG executables go? [$BINDIR] "
read_ans
[ "$ANS" != "" ] && BINDIR=$ANS

echo
echo "* * *  Make sure this directory is in your PATH!  * * *"


printf "Creating directories..."

create_dir $EMGDIR
chmod 755 $EMGDIR

create_dir $EMGDIR/log
chmod 755 $EMGDIR/log

create_dir $EMGDIR/sql

create_dir $BINDIR

echo OK

printf "Moving programs..."
for file in $PROGRAMS
do
	mv bin/$file $BINDIR
	chown $EMGUSER:$EMGGROUP $BINDIR/$file
	chmod 555 $BINDIR/$file
done
echo OK

printf "Moving scripts..."
for file in $SCRIPTS
do
	mv scripts/$file $BINDIR
	chown $EMGUSER:$EMGGROUP $BINDIR/$file
	chmod 555 $BINDIR/$file
done
echo OK

save_to_old ()
{
	FILE=$1
	[ -f "$FILE" ] && {
		echo "Saving $FILE as $FILE.old."
		mv $FILE $FILE.old
	}
}

printf "Moving data files..."
for file in $ETCFILES
do
	save_to_old $EMGDIR/$file
	mv etc/$file $EMGDIR
	chown $EMGUSER:$EMGGROUP $EMGDIR/$file
	chmod 644 $EMGDIR/$file
done

# Scripts should be executable
chmod 755 $EMGDIR/*.sh

for file in `ls sql`
do
	mv sql/$file $EMGDIR/sql/
	chown $EMGUSER:$EMGGROUP $EMGDIR/sql/$file
	chmod 644 $EMGDIR/sql/$file
done

chmod 444 $EMGDIR/sql/*.sql

for file in $ROOTFILES
do
	mv $file $EMGDIR
	chown $EMGUSER:$EMGGROUP $EMGDIR/$file
	chmod 444 $EMGDIR/$file
done

[ -f license ] && {
	mv license $EMGDIR
	chmod 444 $EMGDIR/license
	chown $EMGUSER:$EMGGROUP $EMGDIR/license
}

echo OK

export EMGDIR

enterlic=YES

[ -f "$EMGDIR/license" ] && {
	echo "You already have a license file in $EMGDIR/license."
	printf "Do you want to use the existing license? [y] "
	read_yn y && enterlic=NO
}

[ "$enterlic" = "YES" ] && {
	echo "LICENSE INFORMATION"
	echo "Enter your license information EXACTLY as received."
	echo "You can also just press Enter for all fields here, and copy the license data"
	echo "to $EMGDIR/license manually when this script has exited."
	printf "HOSTID     : "
	read_ans ; HOSTID=$ANS
	printf "COMPANY    : "
	read_ans ; COMPANY=$ANS
	printf "TELNO      : "
	read_ans ; TELNO=$ANS
	printf "SERIAL     : "
	read_ans ; SERIAL=$ANS
	printf "LICENSEDATA: "
	read_ans ; LICDATA=$ANS
	printf "ACTIVATION : "
	read_ans ; ACTIVATION=$ANS

	echo "# Enterprise Messaging Gateway $VERSION - License information" >>$EMGDIR/license
	echo "HOSTID=$HOSTID" >>$EMGDIR/license
	echo "COMPANY=$COMPANY" >>$EMGDIR/license
	echo "TELNO=$TELNO" >>$EMGDIR/license
	echo "SERIAL=$SERIAL" >>$EMGDIR/license
	echo "LICENSEDATA=$LICDATA" >>$EMGDIR/license
	echo "ACTIVATION=$ACTIVATION" >>$EMGDIR/license

	chmod 444 $EMGDIR/license
}

chown -R $EMGUSER:$EMGGROUP $EMGDIR

echo
echo "INSTALLATION FINISHED!"
echo "======================"
echo
echo "First, please review the configuration files in $EMGDIR."
echo "The EMG server, emgd, can be started with \"emgd\""
echo "after setting the variable EMGDIR to $EMGDIR."
echo
echo "Check the file $EMGDIR/README for more information."
echo

exit 0

