#!/bin/sh

# For each name in INFILE, call a SeqHound method, and return
# the result (a single line). Results are concatenated to
# a single output file OUTFILE.

# In a future release of seqhound (after 2.5)
#  SeqHound looks for a .shoundremrc
# file in $HOME, and in the current directory
# Until SeqHound comes up with a better way of finding
# the .shoundremrc file, this script must be run by 
# any program using SeqHound


# Make sure that the user has a ~.shoundremrc file
#if [ ! -e ~/.shoundremrc ] \
#   ln -s $SEQHOUND/shoundremrc ~/.shoundremrc


#------------ SeqHound 2.5 ------------------------
# For now, we use 2.5, but we need to get around the
# requirement that .shoundremrc must be present in every directory
# in which seqhound will be run. The solution is to create a lock file
# for every instance of leash. When a leash job terminates,
# the lock file is removed. If no more lock files remain,
# leash removes .shoundremrc.

# RM_CMD - command to be used for removing files and directories
if [ -f /usr/bin/rm ] 
   then
     RM_CMD=/usr/bin/rm
   else
     if [ -f /bin/rm ] 
        then
           RM_CMD=/bin/rm
        else
           RM_CMD=rm
     fi
fi


LOCK=leash.lock.$$
echo  "This file is created when an instance of leash is running." > $LOCK
echo  "It should automatically be removed when leash terminates. " >> $LOCK

if [ ! -f .shoundremrc ] 
   then 
     cp $birch/java/Leash/shoundremrc ./.shoundremrc
fi     

INFILE=$1
shift
OUTFILE=$1
shift

cat $INFILE |
while read name
  do
    echo $name
    java -jar $birch/java/Leash/Leash.jar -mpi $name $* -of leash.$$
    cat leash.$$ >> $OUTFILE
  done

$RM_CMD $LOCK leash.$$
COUNT=`ls -1 | grep -c leash.lock`
if [ $COUNT -eq "0" ]
   then
     $RM_CMD .shoundremrc
fi