#!/bin/bash

SERVICE_NAME="atlassian_mu"
EXECSCRIPT="./atlassian_mu_process.pl"

FACILITY_NAME=$1
DESTINATION=$2
DESTINATION_TYPE=$3

	#Test if destination is in not empty and in the correct format
if [ -z "$DESTINATION" ]; then
	echo "Missing Destination argument (Name of configuration file to retrieve endpoint URL and key from)" >&2
	exit 252
else
	if echo "$DESTINATION" | grep "[^.A-Za-z_0-9-]"; then
		echo "Bad format of destination!" >&2
		exit 251
	fi
fi

#Test if name of facility is not empty
if [ -z "$FACILITY_NAME" ]; then
	echo "Missing FacilityName argument" >&2
	exit 245
fi

#Basic path to find data from gen service
SERVICE_FILES_BASE_DIR="`pwd`/../gen/spool"
SERVICE_FILES_DIR="$SERVICE_FILES_BASE_DIR/$FACILITY_NAME/$SERVICE_NAME"
#Just safety check. This should not happen
if [ ! -d "$SERVICE_FILES_DIR" ]; then echo '$SERVICE_FILES_DIR: '$SERVICE_FILES_DIR' is not a directory' >&2 ; exit 1; fi

#Create lock to disallow calling more than once at time (method similar to locks in slave scripts)
LOCK_DIR=${LOCK_DIR:=/var/lock}
LOCK_FILE="${LOCK_DIR}/perun-${SERVICE_NAME}-$DESTINATION.lock"
LOCK_PIDFILE="$LOCK_FILE/pid"
function create_lock {
	if mkdir "${LOCK_FILE}"; then
		trap 'rm -r -f "${LOCK_FILE}"' EXIT
		echo $$ > "$LOCK_PIDFILE";
		if [ $? -ne 0 ]; then
			echo "Can't create lock file." >&2
			exit 250
		fi
	else
		# lock file exists, check for existence of concurrent process
		if ps ax | grep "$SERVICE_NAME" | sed 's/^\([0-9]\+\).*/\1/' | grep "\(^\| \)`cat $LOCK_PIDFILE`\( \|$\)"; then
		# concurrent process is running - this skript must terminate
			echo "Concurrent process $SERVICE_NAME is running" >&2
			exit 249
		else
		# lock is not valid; it should be deleted
			rm -r "$LOCK_FILE"
			if [ $? -ne 0 ]; then
				echo "Can't remove not valid lock file." >&2
				exit 248
			fi
			echo "Invalid lock file found and deleted: $LOCK_FILE" >&2
			mkdir "${LOCK_FILE}"
			if [ $? -ne 0 ]; then
				echo "Can't create lock after removing invalid lock." >&2
				exit 247
			fi
			trap 'rm -r -f "${LOCK_FILE}"' EXIT
			echo $$ > "$LOCK_PIDFILE"
			if [ $? -ne 0 ]; then
				echo "Can't create lock file after removing invalid lock file." >&2
				exit 246
			fi
		fi
	fi
}

#start script by creating new lock
create_lock

#prepare temporary working directory
TMP_HOSTNAME_DIR="`mktemp -d /tmp/perun-send.XXXXXXXXXX`"
if [ $? -ne 0 ]; then
	echo "Can't create temporary dir" >&2
	exit 255
fi

#prepare after exit removing of temporary files and directories
trap 'rm -r -f "${LOCK_FILE}" "${TMP_HOSTNAME_DIR}"' EXIT

#copy all needed files to the temporary directory
cp $SERVICE_FILES_DIR/*.scim $TMP_HOSTNAME_DIR
if [ $? -ne 0 ]; then
	echo "Can't copy service file to temporary dir" >&2
	exit 254
fi

#test if exists perl script to call from this one
if [ ! -f "$EXECSCRIPT" ]; then
	echo "Can't locate connector file!" >&2
	exit 253
fi

#call perl script with mandatory options
$EXECSCRIPT -c "/etc/perun/services/$SERVICE_NAME/$DESTINATION" -d "$TMP_HOSTNAME_DIR/"

#catch return statement and process it
ERRORCODE=$?
if [ $ERRORCODE -ne 0 ]; then
	echo "Process exit with error" >&2
fi

exit $ERRORCODE
