#!/bin/bash

SERVICE_NAME="o365_mu"
EXECSCRIPT="./o365_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 instance there)" >&2
	exit 258
else
	if echo "$DESTINATION" | grep "[^.A-Za-z_0-9-]"; then
		echo "Bad fromat of destination!" >&2
		exit 257
	fi
fi

#Test if destination type is not empty and it is defined as service-specific (mandatory settings)
if [ -z "$DESTINATION_TYPE" ]; then
	echo "Destination type can't be empty!" >&2
	exit 245
else
	if [ "$DESTINATION_TYPE" != "service-specific" ]; then
		echo "Destination type need to be defined as service specific!" >&2
		exit 244
	fi
fi

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

#Destination is name of instance (name of file with other information like authorization etc.)
INSTANCE_NAME=$DESTINATION

#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}-$INSTANCE_NAME.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 "Concuret 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/$SERVICE_NAME-users $TMP_HOSTNAME_DIR
cp $SERVICE_FILES_DIR/$SERVICE_NAME-groups $TMP_HOSTNAME_DIR
### We are not using resource mailboxes any more, this code will be preserved for further use
#cp $SERVICE_FILES_DIR/$SERVICE_NAME-resource-mails $TMP_HOSTNAME_DIR;
cp $SERVICE_FILES_DIR/$SERVICE_NAME-facilityId $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 process script!" >&2
	exit 253
fi

#call perl script with mandatory options
$EXECSCRIPT -i $INSTANCE_NAME -p $TMP_HOSTNAME_DIR -s $SERVICE_NAME

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

exit $ERRORCODE
