#!/bin/bash

checkExitCode() {
EC=$?

if [ ${EC} -ne 0 ]; then
	echo "Error"
    exit ${EC};
fi
}

BASEDIR=`dirname $0`

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

NINPUTS=3
NPARAMS=5
NOUTPUTS=7

. $BASEDIR/io.sh ${NINPUTS} ${NPARAMS} ${NOUTPUTS} "$@"
#
# Call Wings Labkey Integration Script
#
if [ ${PARAMS2} == 'true' ]; then
    ARGS='--watch'
fi

if [ ${PARAMS4} == 'false' ]; then
    ARGS='${ARGS} --no-upload'
fi

# Unzip files into a directory:
# Take a zipped input file containing a collection of MS2 (mzXML mostly) files and get a space-sep list -> very hacky.
/usr/bin/unzip $INPUTS1 -d $PWD
checkExitCode

#INPUTS1_BASENAME=`basename $INPUTS1`
#INPUTS1_BASENAME_NOEXT=`echo $INPUTS1_BASENAME | cut -d\. -f1`
#SSV_INPUTS1=`echo ${PWD}/${INPUTS1_BASENAME_NOEXT}/* | tr "\n" " "`
#echo ${PWD}/${INPUTS1_BASENAME_NOEXT}/* | tr "\n" " "
#echo $SSV_INPUTS1

## Take a zipped input file containing a collection of mzXML files and get a space-sep list -> very hacky.
## Accomodating only paired end FASTQ reads
SSV_INPUTS1=`/usr/bin/unzip -Z1 $INPUTS1 | tr "\n" " "`
echo $SSV_INPUTS1

## Extracting the file-collection from zipped input file
#/usr/bin/unzip $INPUTS1
#checkExitCode

#
# Search and replace the protocol file name in the template protocol file with the input FASTA filename
#
DB=`basename ${INPUTS2}` 
PROTOCOL=${DB}_`basename ${INPUTS3}`
sed -e "s/\${DB}/${DB}/g" ${INPUTS3} > ${PROTOCOL}

#
# Search and replace the input file location in the config file with the input FASTA filename
INPUT_PREFIX=`basename ${INPUTS1} $| cut -f 1 -d '.'`
MS2_CONFIG=${INPUT_PREFIX}.config

sed -e "s/input-location    = .*$/input-location    = ${INPUT_PREFIX}/g" ${BASEDIR}/.labkey-config.txt > $MS2_CONFIG

#/usr/bin/python $BASEDIR/wings-labkey --config ${BASEDIR}/.labkey-config.txt ms2 \
/usr/bin/python $BASEDIR/wings-labkey --config ${MS2_CONFIG} ms2 \
                                   --input-file ${SSV_INPUTS1} \
                                   --fasta-file ${INPUTS2} \
                                   --expanded \
                                   --protocol-file ${PROTOCOL} \
                                   --grouping ${PARAMS5} \
                                   --search-engine ${PARAMS1} \
                                   --poll-interval ${PARAMS3} \
                                   ${ARGS}

checkExitCode

#
# Rename Output files
#
read -r -a ARR_INPUTS1 <<< ${SSV_INPUTS1}
if [ ${#ARR_INPUTS1[@]} -le 1 ]; then
	OUTPUT_PREFIX=`basename ${ARR_INPUTS1} | cut -f 1 -d '.'`
	mv ${OUTPUT_PREFIX}.input.xml ${OUTPUTS5}
else 
	OUTPUT_PREFIX=all
	echo "Not generating the input.xml file for fractionated samples" > ${OUTPUTS5}
fi
checkExitCode

# Copy all the labkey output files to the filenames generated by WINGS
mv ${OUTPUT_PREFIX}.prot.xml  ${OUTPUTS1}
mv ${OUTPUT_PREFIX}.pep.xml   ${OUTPUTS2}
mv ${OUTPUT_PREFIX}.png       ${OUTPUTS3}
mv ${OUTPUT_PREFIX}Detail.png ${OUTPUTS4}
mv ${OUTPUT_PREFIX}.log       ${OUTPUTS6}
mv ${OUTPUT_PREFIX}.tsv       ${OUTPUTS7}
checkExitCode
exit ${EC}

