#!/bin/bash
# 
# Dedicated scheduling script

# Looks at the two files DATS_available and DATS_schedule
# to determine if this machine should be made available
# to the DATS grid.  
#
# 'available' 	signals that the machine is available to the grid 
# 'blocked' 	signals that the machine is in its scheduled
#		time, but it is not available to the grid  
# 'busy' 	signals that the machine is not in its scheduled
#		time and is thus not available to the grid
#
# The schedule is set as two times hh:mm:ss-hh:mm:ss (start_time_end-time)
# where the available time exists within the range.  If the end_time is
# less than the start_time, it is assumed that the end_time is the next day 

. /etc/autobench.conf || . functions

# Some constants
AVAILABLE=available
BUSY=busy
BLOCKED=blocked
SCHEDULED=scheduled:

SCHED_FILE=$AUTODIR/var/DATS_schedule
AVAIL_FILE=$AUTODIR/var/DATS_available
AVAIL_FILE_OLD=$AUTODIR/var/DATS_available.old

# Check if our state should be static
state=`head -n1 $SCHED_FILE 2>/dev/null` 

if [ $? -ne 0 ]; then
	echo 12:00:00-12:00:00 > $SCHED_FILE
	exit

elif [ $state = $BLOCKED ]; then
	echo -e $BLOCKED '\n' $SCHEDULED  `date '+%D'` > $AVAIL_FILE
	exit

elif [ $state = $AVAILABLE ]; then
	echo -e $AVAILABLE '\n' $SCHEDULED  `date '+%D'` > $AVAIL_FILE
	exit
fi


# Get start, end and now times
var=`cat $SCHED_FILE | tr -d " "`
start=`echo $var | cut -f1 -d "-"`
end=`echo $var | cut -f2 -d "-"`
let now=`date +%s`
let start_time=`date --date=$start '+%s'`
let end_time=`date --date=$end '+%s'`


if ! test -f $AVAIL_FILE
	then
		echo -e $BUSY > $AVAIL_FILE
		exit
	fi 

# Get last scheduled date
last_sched=`grep $SCHEDULED $AVAIL_FILE | tr -d $SCHEDULED`

# We might need to make the end_time tomorrow
if [ -n $last_sched ] && [ $end_time -lt $start_time ]; then
	end_time=`date "--date=$last_sched $end next day" '+%s'`
fi

#Get the current state
state=`head -n1 $AVAIL_FILE | tr -d " "`

# Find out if we should make the machine available
if [ \( $now -gt $start_time \) -a \( $now -lt $end_time \) ] && [ $state = $BUSY ]; then
	wall "This machine will become available to DATS in 5 minutes.  If you would like to block this for this period run: DATS_block"
	sleep 300 
	state=`head -n1 $AVAIL_FILE | tr -d " "`

	#If still busy, make available
	if [ $state=$BUSY ]; then
		mv $AVAIL_FILE $AVAIL_FILE_OLD
		echo -e $AVAILABLE '\n' $SCHEDULED  `date '+%D'` > $AVAIL_FILE

	# We've been blocked, so set the block stamp
	elif [ $state=$BLOCKED ]; then
		mv $AVAIL_FILE $AVAIL_FILE_OLD
		echo -e $BLOCKED '\n' $SCHEDULED  `date '+%D'` > $AVAIL_FILE
	
	fi

# If we're blocked make sure the stamp is set
elif [ \( $now -gt $start_time \) -a \( $now -lt $end_time \) ] && [ $state = $BLOCKED ]; then
	

		if [ -z $last_sched ]; then
		mv $AVAIL_FILE $AVAIL_FILE_OLD
		echo -e $BLOCKED '\n' $SCHEDULED  `date '+%D'` > $AVAIL_FILE
		fi

# Set us back to busy
elif [ $now -gt $end_time ] && [ \( $state = $AVAILABLE \) -o \( $state = $BLOCKED \) ]; then


		mv $AVAIL_FILE $AVAIL_FILE_OLD 
		echo $BUSY > $AVAIL_FILE

fi

