#!/bin/sh

checkExitCode() {
if [ $? -ne 0 ]; then
	echo "Error"
	exit 1;
fi
}

BASEDIR=`dirname $0`
#LIBDIR=$BASEDIR/../../../../../lib
#ANNDIR=$LIBDIR/customProteinFastaSeq/ensemblAnnotations/

if [ $# -lt 1 ]; then echo "Error No inputs provided" && exit 1; fi

. $BASEDIR/io.sh 4 4 4 "$@"

# Making parameter 4 redundant
# For each FASTA file version, The customProDB annotations should be stored on the server under <path_to_fastafile>/cpdbAnnot/
# Get annotations for the input bam file using the input fasta file as reference to maintain consistent genomics fasta files
ANNDIR=`dirname $INPUTS4`/cpdbAnnot/

echo $R_HOME/bin/Rscript --no-save --no-restore $BASEDIR/customProDBWrapper.R $INPUTS1 $INPUTS2 $OUTPUTS1 $ANNDIR $PARAMS1 $PARAMS2 $PARAMS3 $INPUTS3
$R_HOME/bin/Rscript --no-save --no-restore $BASEDIR/customProDBWrapper.R $INPUTS1 $INPUTS2 $OUTPUTS1 $ANNDIR $PARAMS1 $PARAMS2 $PARAMS3 $INPUTS3

checkExitCode

echo mv ${OUTPUTS1}_rpkm.fasta $OUTPUTS1
mv ${OUTPUTS1}_rpkm.fasta $OUTPUTS1
echo mv ${OUTPUTS1}_snv.fasta $OUTPUTS2
mv ${OUTPUTS1}_snv.fasta $OUTPUTS2
echo mv ${OUTPUTS1}_snv.tab $OUTPUTS3
mv ${OUTPUTS1}_snv.tab $OUTPUTS3

if [ ${PARAMS1} == 'true' ] && [ -e ${OUTPUTS1}_indel.fasta ] ; then
	echo mv ${OUTPUTS1}_indel.fasta $OUTPUTS4
	mv ${OUTPUTS1}_indel.fasta $OUTPUTS4
else
	echo "No indel fasta produced. Output file will be blank"
	echo "" > $OUTPUTS4
fi

checkExitCode
