#!/bin/sh
################################################################################
# This is a script that takes a series of Phase 1 Proposals and
# generates Spike files as though they were Phase 2 proposals.
# These files are used to make a mock Long Range Plan. This plan
# is then used to inform the TAC about resource shortages in the
# upcoming cycle. They will use this information to help select
# the best proposals.
################################################################################

# Check usage

if [ ! "$1" ]; then echo "Usage: expandAll <directory>"; exit 1; fi
if [ ! -d "$1" ]; then echo "Error: Can't read directory ${1}"; exit 1; fi

# Figure out where this script is located

scriptPath="$0"
link=`readlink "${scriptPath}"`
while [ "${link}" != "" ]
do
   case "${link}" in
      /*) scriptPath="${link}";;
      *)  scriptPath=`dirname "${scriptPath}"`/"${link}";;
   esac
   link=`readlink "${scriptPath}"`
done

scriptDir=`dirname "${scriptPath}"`

# Run apt on batches of proposals

for hundred in 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
do
	echo "Running on ${hundred}xx"
	files=`ls ${1}/${hundred}*.xml ${1}/${hundred}*.apt 2> /dev/null`
	if [ "${files}" == "" ]; then
		echo "No ${hundred}xx files found"
	else
		${scriptDir}/apt -nogui -runall -P vp.expandobs="true" -Metrics -nostatus ${files};
		mv Metrics.xml ${1}/Metrics-${hundred}.xml
	fi
done
