#!/bin/sh

# This script allows the user to start Pidgin
# with the XtreemFS functionality

# TODO:
# + check if the config folder's path contains the local mount point's path
# - check user permissions before mounting XFS volume
# - check if the XFS volume is already mounted
# + log file
# + react on missing parameters
# + do not create log file with -h option
# + do not mount volume if config dir is not a subdirectory of the lmp
# + remove old log files

#
# Variables
#

# Global variables
PIDGIN_EXEC="pidgin"
XOS_PREFIX="xos:"
XOS_DELIMITER=","
XOS_PREFIX_SERVER="server="
XOS_PREFIX_VOL="vol="
XOS_PREFIX_LMP="lmp="
XOS_PREFIX_MOUNT="mount="

number_of_params=0
log_file="/tmp/pidgin_xos.log"
max_log_files=10
xos_error_message="Some errors occurred in XtreemOS module: please check $log_file for more information"

# XtreemFS parameters
conf_dir=""
server=""
vol=""
lmp=""
mount="NO"

#
# Functions
#

# Check log file
create_log_file()
{
	if [ -f $log_file ] ; then
		timestamp=`date +%Y%m%d%H%M%S`
		mv $log_file $log_file.$timestamp &> /dev/null
	fi
	touch $log_file &> /dev/null

	# remove old log files
	n_log_files=`ls $log_file.* 2> /dev/null | wc -l`
	if [ $n_log_files -gt $max_log_files ] ; then
		echo "Removing old log files ..." >> $log_file
		rm -f $log_file.* 2>> $log_file
	fi
}

# Display help
display_help()
{
	program_name=`basename $0`
	echo "
Start Pidgin reading configuration information from a remote
directory using XtreemFS capabilities.

Usage: $program_name [OPTION]...

  -c	XFS_CONFIG_STRING
  -h	display this help and exit

Format of XFS_CONFIG_STRING:
  xos:CONFIG_DIR,server=XFS_SERVER,vol=XFS_VOLUME,lmp=LOCAL_MOUNT_POINT[,mount=MOUNT_FLAG]

Format considerations:
  - Parameter labels must be literal: '$XOS_PREFIX', '$XOS_PREFIX_SERVER',
	'$XOS_PREFIX_VOL', '$XOS_PREFIX_LMP' and '$XOS_PREFIX_MOUNT'
  - It must not contain blank spaces
  - CONFIG_DIR is the desired configuration directory
  - XFS_SERVER is the URL of the XtreemFS server
  - XFS_VOLUME is name of the volume to be mounted
	from XtreemFS
  - LOCAL_MOUNT_POINT is the local directory where
	the remote volume will be mounted
  - MOUNT_FLAG if 'TRUE' (or 'true', 'YES' or 'yes')
	indicates if the remote volume must be mounted
	on the local mount point. Otherwise, no mount
	operation will take place and Pidgin will be
	passed the configuration directory as usually
"
}

# Print parameters values
print_parameters()
{
	echo "Parameters values ($number_of_params present):" >> $log_file
	echo "	Configuration directory: $conf_dir" >> $log_file
	echo "	XtreemFS server URL: $server" >> $log_file
	echo "	XtreemFS volume: $vol" >> $log_file
	echo "	Local mount point: $lmp" >> $log_file
	echo "	Force mount: $mount" >> $log_file
}

# Parse -c option
parse_option()
{
	str=$1
	number_of_params=`echo $str | awk -F $XOS_DELIMITER '{print NF}'`
	i=1
	while [ $i -le $number_of_params ]; do
		tmp=`echo $str | awk -F $XOS_DELIMITER -v i=$i '{print $i}'`

		# configuration directory
		value=`echo $tmp | grep -i $XOS_PREFIX`
		if [ $value ] ; then
			conf_dir=`echo $value | cut -c 5-`
			i=`expr $i + 1`
			continue
		fi

		# XtreemFS server
		value=`echo $tmp | grep -i $XOS_PREFIX_SERVER`
		if [ $value ] ; then
			server_bar=`echo $value | cut -c 8-`
			if [ $server_bar ] ; then
				last_char=`echo $server_bar | tail -c 2`
				cut_index=`expr ${#server_bar} - 1`
				if [ $last_char = "/" ] ; then
					server=`echo $server_bar | cut -c -$cut_index`
				else
					server=$server_bar
				fi
			fi
			i=`expr $i + 1`
			continue
		fi

		# XtreemFS volume to be mounted
		value=`echo $tmp | grep -i $XOS_PREFIX_VOL`
		if [ $value ] ; then
			vol=`echo $value | cut -c 5-`
			i=`expr $i + 1`
			continue
		fi

		# local mount point
		value=`echo $tmp | grep -i $XOS_PREFIX_LMP`
		if [ $value ] ; then
			lmp_bar=`echo $value | cut -c 5-`
			if [ $lmp_bar ] ; then
				last_char=`echo $lmp_bar | tail -c 2`
				cut_index=`expr ${#lmp_bar} - 1`
				if [ $last_char = "/" ] ; then
					lmp=`echo $lmp_bar | cut -c -$cut_index`
				else
					lmp=$lmp_bar
				fi
			fi
			i=`expr $i + 1`
			continue
		fi

		# flag to decide if mount volume or not
		value=`echo $tmp | grep -i $XOS_PREFIX_MOUNT`
		if [ $value ] ; then
			mount=`echo $value | cut -c 7-`
			i=`expr $i + 1`
			continue
		fi
	done
}

check_parameters()
{
	if [ -z $conf_dir ] ; then
		echo "Warning: Configuration directory not specified" >> $log_file
		echo $xos_error_message
		$PIDGIN_EXEC
		exit 1
	fi

	# check if the config dir is a subdirectory of the local mount point
	if [ $lmp ] ; then
		len_lmp=${#lmp}
		substr=`expr substr $conf_dir 1 $len_lmp`
		if ! [ $substr = $lmp ] ; then
			echo "Warning: the configuration directory is not a subdirectory of the local mount point" >> $log_file
			mount=NO
		fi
	fi

	if [ -z $server ] || [ -z $vol ] || [ -z $lmp ] ; then
		echo "Error: some parameters are NULL!!" >> $log_file
		echo $xos_error_message
		$PIDGIN_EXEC -c $conf_dir
		exit 1
	fi
}



#
# Main
#

# Command line processing
if [ $# -eq 0 ] ; then
	# less arguments than expected
	$PIDGIN_EXEC $*
	exit 0
fi

while getopts hc: opt
do
	case "$opt" in
		c) parse_option $OPTARG;;
		h) display_help; exit 0;;
		\?) $PIDGIN_EXEC $*; exit 0;
	esac
done

create_log_file

# print parameters
print_parameters

# check XFS parameters
check_parameters

# Use XtreemFS parameters

# NOTE: we don't have to care about the existance of the
# local mount point: Pidgin solves such situation
# on its own

# if the user doesn't want to mount the XFS volume,
# just get config dir and go on with execution
if ! [ $mount = "yes" -o $mount = "YES" -o $mount = "true" -o $mount = "TRUE" ] ; then
	$PIDGIN_EXEC -c $conf_dir
	exit 0
fi

# create local mount point
mkdir -p $lmp 2>> $log_file

# local mount point doesn't exist as a directory
# thus, we can't mount the XFS volume
if [ ! -d $lmp ] ; then
	$PIDGIN_EXEC -c $conf_dir
	exit 1	
fi

# mount XFS volume on lmp
echo "Mounting XtreemFS volume on local mount point ..." >> $log_file
xtreemfs -o volume_url=$server/$vol -o nonempty $lmp 2>> $log_file
if ! [ $? -eq 0 ] ; then
	echo $xos_error_message
fi

# Finally, start Pidgin
echo "Calling Pidgin ..." >> $log_file
$PIDGIN_EXEC -c $conf_dir

exit 0


