#!/bin/sh -x

# Determines the queue a job is submitted to
#PBS -q %PBS_QUEUE%

# Name of the job
#PBS -N %PBS_JOB_NAME%-cat

# Combine PBS error and output files.
#PBS -j oe

# Memory resource requirements
#PBS -l mem=%PBS_MEMORY%

# Mail job status at completion
#PBS -m ae
%PBS_MAILTO_DIRECTIVE%

#
# Output some useful PBS job information
#
echo ------------------------------------------------------
echo Job is running on the following nodes:
cat ${PBS_NODEFILE}
echo ------------------------------------------------------
echo PBS: qsub is running on ${PBS_O_HOST}
echo PBS: originating queue is ${PBS_O_QUEUE}
echo PBS: executing queue is ${PBS_QUEUE}
echo PBS: working directory is ${PBS_O_WORKDIR}
echo PBS: execution mode is ${PBS_ENVIRONMENT}
echo PBS: job identifier is ${PBS_JOBID}
echo PBS: job name is ${PBS_JOBNAME}
echo PBS: task number is ${PBS_TASKNUM}
echo PBS: node number is ${PBS_NODENUM}
echo PBS: node file is ${PBS_NODEFILE}
echo PBS: array index is ${PBS_ARRAY_INDEX}
echo PBS: current home directory is ${PBS_O_HOME}
echo PBS: scratch directory is ${TMPDIR}
echo PBS: PATH = ${PBS_O_PATH}
echo ------------------------------------------------------

# Copy the goby and support tools to the local node
# TODO: copy bwa and log4j, etc.
/bin/cp ${PBS_O_WORKDIR}/goby.jar ${TMPDIR}

INTERIM_RESULT_DIR=${PBS_O_WORKDIR}/split-results/%BASENAME%
RESULT_DIR=${PBS_O_WORKDIR}/results/%BASENAME%

# Run the concatenate
cd ${TMPDIR}

java %PBS_JVM_FLAGS% -Dlog4j.debug=true -Dlog4j.configuration=file:log4j.properties -jar goby.jar \
    --mode concatenate-alignments --output %BASENAME% ${INTERIM_RESULT_DIR}*/*.entries

/bin/mkdir -p ${RESULT_DIR}
/bin/cp *.entries *.header *.stats *.tmh ${RESULT_DIR}
