#!/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;31mMain archi system has to be installed! Exiting...\033[0m"
	exit 1
fi

if [[ ! ( -d "/home/SYSQS" && ! -L "/home/SYSQS" ) ]]; then
	echo -e "\033[0;31mMain archi system has to be installed! Exiting...\033[0m"
	exit 1
fi

if [ "$#" -ne 1 ]; then
    echo -e "\033[0;31mArchi name has to be provided as single parameter!\033[0m"
    exit 1
fi

if [[ $1 =~ ^ARCHI([0-9]{2})?[0-9]{2}$ && !($1 =~ ^ARCHI00[0-9]{2}$) ]]; then

	if [[ -d "/home/$1" && ! -L "/home/$1" ]] ; then
		echo -e "\033[0;31m$1 already exists! Exiting...\033[0m"
		exit 1
	else
		echo "Installing $1..."
	fi

else
	echo -e "\033[0;31mIncorrect archi name! Exiting...\033[0m" 1>&2
	exit 1
fi

echo "Choose $1 admin ID"
echo "This will be starting ID for client list"

admin_id="NOID"

until [[ $admin_id =~ ^[0-9]{7}$ ]]; do
read -p "Admin id (7 digits): " admin_id
done

echo "#define ARCHINAME \"$1\"" > ../CSCAF/archi_name.inl
echo "#define ADMIN_NUM \"$admin_id\"" >> ../CSCAF/archi_name.inl

cd ..
make communicator
cd INSTALL-SKZ

echo "Creating directory tree..."

create_dir /home/$1
create_dir /home/$1/001A
create_dir /home/$1/001A/SYSTEM
create_dir /home/$1/001A/SYSTEM
create_dir /home/$1/001A/SYSTEM/MAN
create_dir /home/$1/001A/SYSTEM/BIN
create_dir /home/$1/001A/SYSTEM/CONFIG
create_dir /home/$1/001A/MET
create_dir /home/$1/001B
create_dir /home/$1/001B/DOIT
create_dir /home/$1/001B/VAR
create_dir /home/$1/001B/VAR/ADMIN
create_dir /home/$1/001B/VAR/ADMIN/CLIENTS
create_dir /home/$1/001B/VAR/ADMIN/CLIENTS/TMP
create_dir /home/$1/001B/VAR/FILES
create_dir /home/$1/001B/VAR/FILES/CON
create_dir /home/$1/001B/VAR/FILES/GROUP
create_dir /home/$1/001B/VAR/FILES/SERVICE
create_dir /home/$1/001B/VAR/ARCHI

echo "Copying executables and configs..."

#SERVER-CONFIGS
copy_file SERVER-A/DEFCONFIG.ex /home/chn11/CONFIG/$1.ex
copy_file ADMIN/DEFCONFIG.ex /home/SYSQS/ADMIN_CONFIG/$1.ex

#BASIC
#copy files to /home/$1/001A/SYSTEM/BIN/
copy_file APPLICATIONS/BASIC/passwdARCHI /home/$1/001A/SYSTEM/BIN/passwdARCHI
copy_file APPLICATIONS/BASIC/unblock /home/$1/001A/SYSTEM/BIN/unblock
copy_file APPLICATIONS/BASIC/softblock /home/$1/001A/SYSTEM/BIN/softblock

#CHECK-KEY
#copy files to /home/$1/001A/SYSTEM/BIN/
copy_file APPLICATIONS/CHECK-KEY/check-key /home/$1/001A/SYSTEM/BIN/check-key
copy_file APPLICATIONS/CHECK-KEY/check-key-file /home/$1/001A/SYSTEM/BIN/check-key-file

#GROUPS 
#copy files to /home/$1/001A/SYSTEM/BIN/
copy_file APPLICATIONS/GROUPS/agrp-admin /home/$1/001A/SYSTEM/BIN/agrp-admin
copy_file APPLICATIONS/GROUPS/agrp-client /home/$1/001A/SYSTEM/BIN/agrp-client

#PRIORITY 
#copy files to /home/$1/001A/SYSTEM/BIN/
copy_file APPLICATIONS/PRIORITY/adm-priority /home/$1/001A/SYSTEM/BIN/adm-priority

#READ 
#copy files to /home/$1/001A/SYSTEM/BIN/
copy_file APPLICATIONS/READ/READ /home/$1/001A/SYSTEM/BIN/READ

#WRITE 
#copy files to /home/$1/001A/SYSTEM/BIN/
copy_file APPLICATIONS/WRITE/WRITE /home/$1/001A/SYSTEM/BIN/WRITE

#CONFIG-READ
#copy files to /home/$1/001A/SYSTEM/BIN/
copy_file APPLICATIONS/CONFIG-READ/config-read /home/$1/001A/SYSTEM/BIN/config-read
copy_file APPLICATIONS/CONFIG-READ/config_read.conf /home/$1/001A/SYSTEM/CONFIG/config_read.conf

trigg='NOTRIGG'

read -p "Do you want to register archi? (Yy/Nn) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
	until [[ $trigg =~ ^TRIGG[0-9]{4}$ ]]; do
	read -p "Trigger-name: (TRIGGxxxx) " trigg
	done
	./register-archi $1 $trigg
fi

echo "Initializing user database..."

/home/$1/001A/SYSTEM/BIN/passwdARCHI --add

echo -e "\033[0;32mGenerating $1 admin credentials...\033[0m"
echo -e "\033[0;32m\n\n*****************************************\n\n\033[0m"

/home/$1/001A/SYSTEM/BIN/passwdARCHI --chg $admin_id 000000

echo -e "\033[0;32m\n\n*****************************************\n\n\033[0m"

rm /mnt/tmp/exec_tmp

echo -e "\033[0;32m$1 installation Complete!\033[0m"

exit 0