#!/bin/bash
#
# Tool to deploy QCG application scripts from installation directory to cluster node's shared directory
#

umask 022

CONFIG_FILE=/etc/qcg/qcg-comp/app-scripts/config

# source subdirectories path
SRC_PATH="/usr/share/qcg-appscripts"

# subdirectories to deploy into cluster shared destination
SUBDIRS="core app-scripts tools"
UPDATE_SUBDIRS="apps"
CONFIG_SUBDIRS="config"

# tools that needs recompilation
TOOLS_RECOMPILE="qcg-gaussian qcg-interactive"

LOG_FILE=/var/log/qcg/qcg-comp/appscripts/update-`date "+%F_%T"`

function get_config_var {
	local file=$1
	local var=$2

	if [ ! -f "$file" ]; then
		return
	fi

	value=`cat $file | awk "/^[[:blank:]]*${var}[[:blank:]]*=[[:blank:]]*.+$/ { sub(\"[^=]+=[[:blank:]]*\", \"\"); sub(\"[[:blank:]]+$\", \"\"); print \\\$0 }" | sed "s/^['\"]//" | sed "s/['\"]$//"`
	echo "$value"
}

[ ! -f "$CONFIG_FILE" ] && { echo "error: configuration file '$CONFIG_FILE' doesn't exist"; exit 1; }

###
# read configuration file
###
dst_path=$(get_config_var "$CONFIG_FILE" "cluster_shared_path")
[ -z "${dst_path}" ] && { echo "error: variable 'cluster_shared_path' not defined in $CONFIG_FILE configuration file"; exit 1; }

echo "cluster shared path [${dst_path}] ..."
#echo "found path [${dst_path}]"

###
# check destination subdirectories
###
subdirs_exists=0

for subdir in $SUBDIRS; do
	dst_subdir="${dst_path}/${subdir}"

	[ -d "${dst_subdir}" ] && { subdirs_exists=1; }
done

if [ "${subdirs_exists}" -ne 0 ]; then
	while true; do
		read -p "WARNING: Some destination directories '${SUBDIRS}' in ${dst_path} already exists - all content will be removed. Do you want to continue ?" yn
		case $yn in
			[Yy]* ) break;;
			[Nn]* ) echo "canceling"; exit;;
			* ) echo "Please answer yes or no.";;
		esac
	done
fi
#echo "continuing"

if [ ! -d "${dst_path}" ]; then
	res=`mkdir -p "${dst_path}"`
	[ $? -ne 0 ] && { echo "error: failed to create directory ${dst_path}: ${res}"; exit 2; }
fi

###
# deployment
###
for subdir in ${SUBDIRS}; do
	src_subdir="${SRC_PATH}/${subdir}"
	dst_subdir="${dst_path}/${subdir}"

	res=`rm -fR "${dst_subdir}"`
	[ $? -ne 0 ] && { echo "error: failed to remove directory ${dst_subdir}: ${res}"; exit 2; }

	res=`cp -R "${src_subdir}" "${dst_subdir}"`
	[ $? -ne 0 ] && { echo "error: failed to deploy directory ${src_subdir} -> ${dst_subdir}: ${res}"; exit 2; }
done

date=$(date "+%F_%T")
for subdir in ${UPDATE_SUBDIRS}; do
	src_subdir="${SRC_PATH}/${subdir}"
	dst_subdir="${dst_path}/${subdir}"

	mkdir -p "${dst_subdir}"
	mkdir -p "${dst_subdir}/backup"

	for file in ${src_subdir}/*; do
		file_name=`basename $file`

		echo "upgrading file ${dst_subdir}/${file_name} ..." >> "${LOG_FILE}" 2>&1

		if [ -f "${dst_subdir}/${file_name}" ]; then
			res=`cp "${dst_subdir}/${file_name}" "${dst_subdir}/backup/${file_name}.${date}"`
			[ $? -ne 0 ] && { echo "error: failed to create backup file ${dst_subdir}/backup/${file_name}.${date}: ${res}"; exit 2; }
		fi

		res=`cp "${src_subdir}/${file_name}" "${dst_subdir}/${file_name}"`
		[ $? -ne 0 ] && { echo "error: failed to deploy file ${dst_subdir}/${file_name}: ${res}"; exit 2; }
	done
done

date=$(date "+%F_%T")
for subdir in ${CONFIG_SUBDIRS}; do
	src_subdir="${SRC_PATH}/${subdir}"
	dst_subdir="${dst_path}/${subdir}"

	mkdir -p "${dst_subdir}"

	for file in ${src_subdir}/*; do
		file_name=`basename $file`

		if [ ! -f "${dst_subdir}/${file_name}" ]; then
			echo "deploying file ${dst_subdir}/${file_name} ..." >> "${LOG_FILE}" 2>&1

			res=`cp "${src_subdir}/${file_name}" "${dst_subdir}/${file_name}"`
			[ $? -ne 0 ] && { echo "error: failed to deploy file ${dst_subdir}/${file_name}: ${res}"; exit 2; }
		fi
	done
done


###
# tools compilation
###
for tool in ${TOOLS_RECOMPILE}; do
	subdir="${dst_path}/tools/${tool}"

	echo "compiling $tool tool" >> "${LOG_FILE}" 2>&1
	(cd "$subdir" && make >> "${LOG_FILE}" 2>&1)
done


echo "QCG application scripts deployed successfully"
