#!/bin/sh
#
# choosehost
# prototype implementation of a scheduler backend for an XDM host chooser
#
# Copyright (c) 2007 by Christian Peter <christian.peter@itwm.fraunhofer.de>
#
# v1.1, 2008-10-10, CHP
# - new: read default server from config file
# v1.0, 2007-12-14, CHP
# - initial proof of concept
#

PREFIX=/opt/yass
CONFIG_FILE=$PREFIX/etc/scheduler.cfg
LOG_FILE=/var/tmp/yass-scheduler.log

DATE=`date +"%Y-%m-%d %H:%M:%S"`

if [ "$1" = "choose" ]; then

	# strip domain name and display variable from argument
	CLIENT=`echo $2 | sed s#[\.:].*##`

	if [ -f "$CONFIG_FILE" ]; then
#		echo "debug: searching for client \"$CLIENT\"..."
		SERVER=`grep ^$CLIENT, $CONFIG_FILE | cut -f 2 -d,`
		if [ $? -ne 0 -o -z "$SERVER" ]; then
			echo "warning: no client-specific server found. looking for default." 1>&2
			SERVER=`grep ^DEFAULT, $CONFIG_FILE | cut -f 2 -d,`
			[ $? -ne 0 ] && SERVER=""
		fi
	fi
	
	if [ -n "$SERVER" ]; then
		echo $SERVER
		echo "$DATE,choose,$2,$SERVER" >> $LOG_FILE
	else
		echo "error: could not find a suitable server. aborting." 1>&2
		exit 1
	fi
elif [ "$1" = "handover" ]; then
	echo "$DATE,handover,$2,$3" >> $LOG_FILE
elif [ "$1" = "idle" ]; then
	echo "$DATE,idle,$2" >> $LOG_FILE
fi
