#!/bin/bash

RESULTDIR=/home/txino/GTA/GTAscript/V4/RUN/BLOUT
JOBSDIR=/home/txino/GTA/GTAscript/V4/RUN/BLOUT/JOBS
TJID=0224104558_089
WJID=Sma00K279a..9..Smasm454-2lDM5j
JOBNAME=Sma00K279a
WORKDIR=/state/partition1/$USER/$WJID/$TJID/$JOBNAME

function endscript
{
    if ls ERR* 1>/dev/null 2>&1;then
        touch ERROR
        touch $JOBSDIR/$JOBNAME.ERROR
    else
        echo "NO ERROR FILE"
        touch DONE
        touch $JOBSDIR/$JOBNAME.DONE
    fi
    
    rm -f $JOBSDIR/$JOBNAME.RUNNING

    cd $RESULTDIR
    rsync --remove-source-files -av $WORKDIR $RESULTDIR/ && rm -rf $WORKDIR

    rmdir /state/partition1/$USER/$WJID/$TJID
    rmdir /state/partition1/$USER/$WJID
    rmdir /state/partition1/$USER

    date +%Y%m%d_%H%M%S
    exit
}

if hostname -A >/dev/null 2>&1;then
    hostname -A
else
    hostname
fi

date +%Y%m%d_%H%M%S
mkdir -p $WORKDIR
cd $WORKDIR || { touch ERR_cdworkdir; endscript; }
pwd
touch $JOBSDIR/$JOBNAME.RUNNING
####  END HEADER #######

#### JOB CODE ##########


/home/txino/GTA/GTAscript/V4/BLTOJSON/bltojson.pl /home/txino/GTA/GTAscript/V4/Sma00K279a..9..Smasm454-2lDM5j-rum_newblat-fst.fsgi.json Sma00K279a
 
rc=$?; if [[ $rc != 0 ]]
then
    echo "ERROR: /home/txino/GTA/GTAscript/V4/BLTOJSON/bltojson.pl "/home/txino/GTA/GTAscript/V4/Sma00K279a..9..Smasm454-2lDM5j-rum_newblat-fst.fsgi.json Sma00K279a" exec error \$rc=$rc"
    touch ERR_BLTJ
fi


#### END JOB CODE ######

####  START FOOTER #######

endscript

