#!/bin/bash

#******************************************************************************
#				ARCHI-CLARIN
#		  		   ver 1.0
#			       INSTALL SKZ (QS)
#
# NOTE: use with: install-skz install
#******************************************************************************

function create_dir {
	if [[ -d "$1" && ! -L "$1" ]] ; then
    echo "$1 already exists!"
else 
	echo "$1 does not exists! Creating..."
    mkdir $1
fi
chmod 777 $1
}

function copy_file {
	if cp -r $1 $2; then
		echo "Copying $2"
	else
		echo -e "\033[0;31mCopy failed! Exiting...\033[0m" 1>&2
		exit 1
	fi
}

# START

if [[ $EUID -ne 0 ]]; then
   echo -e "\033[0;31mThis script must be run as root!\033[0m" 1>&2
   exit 1
fi

if [[ -d "/home/ARCHI00" && ! -L "/home/ARCHI00" ]]; then
	echo -e "\033[0;31mArchi has already been installed! Exiting...\033[0m"
	exit 1
fi

if [[ -d "/home/SYSQS" && ! -L "/home/SYSQS" ]]; then
	echo -e "\033[0;31mArchi has already been installed! Exiting...\033[0m"
	exit 1
fi


echo "Installing SKZ with ARCHI01..."

ret=false
getent passwd chn11 >/dev/null 2>&1 && ret=true

if $ret; then
    echo "User chn11 already exists!"
else
    echo "User chn11 does not exist! Creating..."
    useradd chn11
fi

echo "Creating directory tree..."

create_dir /home
create_dir /home/SYSQS
create_dir /home/SYSQS/ENGINE
create_dir /home/SYSQS/PRE_STOS
create_dir /home/SYSQS/EDITORS
create_dir /home/SYSQS/ADMIN_CONFIG
create_dir /mnt/tmp
create_dir /home/ARCHI00
create_dir /home/ARCHI00/INIT
create_dir /home/ARCHI00/DISASTER_RECOVERY
create_dir /home/ARCHI00/DOITMW
create_dir /home/ARCHI00/DOITMW/WAITW
create_dir /home/ARCHI00/REMOTE
create_dir /home/chn11
create_dir /home/chn11/CONFIG

echo "Copying executables and configs..."

# SERVER-A
# copy files to chn11 
copy_file SERVER-A/mserverA /home/chn11/mserverA
copy_file SERVER-A/m-srvAd /home/chn11/m-srvAd
copy_file SERVER-A/runserverA /home/chn11/runserverA
copy_file SERVER-A/DEFCONFIG.ex /home/chn11/CONFIG/ARCHI01.ex

# ADMIN SERVER
# copy files to /home/SYSQS
copy_file ADMIN/mserverAdmin /home/SYSQS/mserverAdmin 
copy_file ADMIN/m-srvAmd /home/SYSQS/m-srvAmd
copy_file ADMIN/runserver_admin /home/SYSQS/runserver_admin
copy_file ADMIN/DEFCONFIG.ex /home/SYSQS/ADMIN_CONFIG/ARCHI01.ex

# MAIN-LOOP
# copy files to /home/SYSQS/
copy_file MAIN-LOOP/mloopQS /home/SYSQS/mloopQS 
copy_file MAIN-LOOP/mountRAM /home/SYSQS/mountRAM 

# DEFAULT
copy_file DEFAULT/clock_alarm /home/SYSQS/clock_alarm
copy_file DEFAULT/default_stat /home/SYSQS/default_stat
copy_file DEFAULT/run_clock_alarm /home/SYSQS/run_clock_alarm
copy_file DEFAULT/unblock_servers /home/SYSQS/unblock_servers

# EDITORS
copy_file EDITORS/edit_mac /home/SYSQS/EDITORS/edit_mac
copy_file EDITORS/edit_reg /home/SYSQS/EDITORS/edit_reg
copy_file EDITORS/edit_trig /home/SYSQS/EDITORS/edit_trig
copy_file EDITORS/join_trigger /home/SYSQS/EDITORS/join_trigger
# edit-trig template file
copy_file EDITORS/TRIGGxxxx /home/SYSQS/EDITORS/TRIGGxxxx

echo
echo
sleep 1
read -p "Do you want to install producer? (Yy/Nn) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then

	./install-producer

fi

echo
echo
sleep 1
./install-archi ARCHI01

sleep 1
#DAEMON MAIN-LOOP
copy_file AT_BOOT/mloopQSd /etc/init.d/mloopQSd

echo "Registering startup daemon in chkconfig..."

#add main-loop as service to the list of services
chkconfig --add mloopQSd
chkconfig --level 35 mloopQSd on

echo -e "\033[0;32mInstallation Complete!\033[0m"

exit 0


