#!/bin/bash
# -*- coding: utf-8 -*-
#
datadir=$1
outdir=$7
export PYTHONPATH=$PYTHONPATH:$datadir/MODEL/lib/x86_64-intel8/python2.5:$datadir/MODEL/modlib
export LD_LIBRARY_PATH=$datadir/MODEL/lib/x86_64-intel8
Sphinx $1 $2 $3 $4 $5 $6 $7
#Check if the status file was created
if [ -f $outdir/status.txt ]; then
    	if grep "Crashed" $outdir/status.txt;
	then
		echo "Program Crashed"
		
	else
	    	if grep "Done" $outdir/status.txt;
		then
			echo "Program Finished Successfully"
            # Run annotate_ss script for PV
            file1=$(ls $outdir/CompleteModels/*pdb | head -n 1);
            sslines=$(./annotate_ss $file1);
             
            for f in $(ls $outdir/CompleteModels/*pdb);
            do
                sed -i '/END/d' $f; # Delete line with END in it
                echo "$sslines" >> $f;
            done

		else
			#This bit is for the cases when the program died and did not manage to write to status.txt that it crashed
		    	echo "Program Crashed"
			echo "Crashed" > $outdir/status.txt

		fi
	fi

else
	#If the status file wasn't even created -- assume that the job crashed
	echo "Crashed" > $outdir/status.txt

fi
#Check if the results are in at this point. If not, consider we failed.

