#!/bin/sh

usage() {
  echo ""
  echo "Usage: $0 type [conf_dir]"
  echo ""
}

for arg in "$@" 
do
  case "$arg" in
    -h | -help | --help | -\?)
      usage
      exit 0;;
    *)
      ;;
  esac
done

if [ $# -ne 1 ] && [ $# -ne 2 ]
then
  usage
  exit 1
fi

type=$1

shift

#
# detect TSI installation Directory
#
TSI_BIN=`dirname $0`

if [ "${TSI_BIN#/}" = "$TSI_BIN" ]
then
  if [ "$TSI_BIN" != "." ]
  then
    TSI_DIR="$(pwd)/$TSI_BIN"
  else
    TSI_DIR="$(pwd)"
  fi
else
  TSI_DIR=$TSI_BIN
fi

TSI_DIR="${TSI_DIR%/bin}"


# >>>>>>>>>> Detect configuration directory:
#                first look for command line argument
#                then in environment ($UNICORE_TSI)
#                then from the directory of this file

if [ $# -eq 1 ]
then
	TSI_CONF=$1
elif [ -n "$UNICORE_TSI" ]
then
	TSI_CONF=$UNICORE_TSI
else
	TSI_CONF="${TSI_DIR}/conf"
fi


# >>>>>>>>>> Sanity check. TSI_CONF must exist, be a directory and be wx.

if [ ! -d $TSI_CONF ] 
then
	echo "TSI configuration directory $TSI_CONF is not a directory (or cannot be found)" 1>&2
	exit 1
fi

if [ ! -w  $TSI_CONF ] 
then 
	echo "Cannot write to TSI configuration directory $TSI_CONF" 1>&2
	exit 1
fi

if [ ! -x  $TSI_CONF ] 
then 
	echo "Cannot execute TSI configuration directory $TSI_CONF" 1>&2
	exit 1
fi


# >>>>>>>>>> get the log dir from the tsi.properties file (and check that it is rx)

LOGDIR="`perl -ne 'if(/^\s*tsi.logdir\s*=\s*(\S+)/) {print $1}' $TSI_CONF/tsi.properties`"

if [ "$LOGDIR" == "syslog" ]
then
    echo "TSI logs messages via Syslog" 1>&2
    exit 1
fi

if [ ! -r $LOGDIR ]  || [ ! -x $LOGDIR ]
then
	echo "The TSI logging directory $TSI_CONF/logs does not exist (or is not readable and executable)." 1>&2
	exit 1
fi

listing=`ls $LOGDIR/ wTSILog_*[0-9] 2> /dev/null` 



# >>>>>>>>>> Write all log file names - in reverse time order

if [ "$type" = "a" ]
then
	echo $listing | tr " " "\012"
	exit 0
fi

echo "${LOGDIR}:"

# >>>>>>>>>> Write log file names since last start (most recent file with first line containing "starting up")

# Put into time order
rev_listing=`echo $listing | tr " " "\012" | sort -r`

if [ "$type" = "l" ]
then
	collected=""
	for logs in $rev_listing
  	do
          if [ -e $LOGDIR/$logs ]
          then
		collected="$logs $collected"
		head -n 5 $LOGDIR/$logs | fgrep -s -e "starting up"
		if [ $? -eq 0 ]
		then
			# Found, quit
			break
		fi
          fi
   	done
        
	echo $collected | tr " " "\012"
	exit 0
fi

if [ "$type" = "L" ]
then
	collected=""
	gathering=0
	for logs in $rev_listing
        do        
          if [ -e  $LOGDIR/$logs ]
          then
	
		if [ $gathering -eq 1 ]
		then
			collected="$logs $collected"
		else
			head -n 5 $LOGDIR/$logs | fgrep -s -e "starting up"
			if [ $? -eq 0 ]
			then
				# Found, start gathering after next
				gathering=1
			fi
		fi
            fi
	  done
        
	echo $collected | tr " " "\012"
	exit 0
fi



# Everything else must be a number, check

echo $type | egrep -s -e "[a-zA-Z]"
if [ $? -eq 0 ]
then
	# The type does not seem to be valid (contains letters!)
	echo "The type must be \"a\" (for all files), \"l\" (since latest start), \"L\" (all before latest start)" 1>&2
	echo "A number (latest n), negative number (all except latest n)" 1>&2
	exit 1
fi 

# >>>>>>>>>> Write the latest n
my_type=$type
if [ $my_type -lt 0 ]
then
	my_type=`expr $my_type \* -1`
fi

# Collect the latest n
collected=""
not_collected=""
count=0
for logs in $rev_listing
do
	if [ "$count" -lt "$my_type" ]
	then
		# Collect in reverse time order
		collected="$logs $collected"
	else
		not_collected="$logs $not_collected"
	fi
	count=`expr $count + 1`
done

if [ $type -ne $my_type ]
then
	collected=$not_collected
fi

echo $collected | tr " " "\012"

exit 0




# 08-08-06 Updated, Bernd Schuller, JSC
# 06-02-02 Created, Sven van den Berghe, fecit
#
#
#           Copyright (c) Fujitsu Ltd 2002
#
# Use and distribution is subject to the Community Source Licence.
#                                                       
# A copy of the Community Source Licence came with this distribution.
# Copies are also available at http://www.fecit.co.uk/arcon or by 
# email from arcon@fecit.co.uk. 
