#!/usr/bin/ksh
#
# usage:
#	rerun dirname
#
# rerun autoprocessing with existing locs directory and pfiles
# -- AND --
# remove conflicting reconstruction files from a previous run
#
#
# If *.tmp.tar or *.tmp.tar.gz is present, it will be sent to
# autolocsort_excite_paradigm; otherwise a temp file name
# indicating none will be sent as the first argument.
#
# Note: when files are moved across the network from the scanner,
# the order has P-files and their reconstruction followed by
# extract and the DICOM conversion.  With this rerun, the order
# is reversed so that the slower DICOM conversion process will
# have a better chance of being assigned to a faster Linux
# computer on the grid.  If all the reconstruction jobs are
# distributed on the grid first, that may leave no fast Linux
# system available for the slower DICOM conversion process.
#
if (test $# -lt 1) then
	echo usage:
	echo rerun dirname
	exit
fi
if (test -d $1) then
    echo rerun for directory $1
else
    echo no such directory: $1
    exit
fi
dir=$1
#
#####################################################
# remove extract flags and kill
#####################################################
#
echo ... removing extract flags
rm -f $dir/log/extract.running
rm -f $dir/log/extract.done
rm -f $dir/log/extract*.waiting
rm -f $dir/log/wait*
rm -f $dir/log/kill
#
#####################################################
# remove entire analyze directory
#####################################################
echo ... removing entire analyze/nifti directories and log kill
rm -rf $dir/analyze $dir/nifti
#
#####################################################
##### remove existing recon-files from a previous run
#####################################################
echo ... removing all recon 4D.hdr
rm -f  $dir/*_4D.hdr
echo ... removing all recon 4D.mat 
rm -f  $dir/*_4D.mat
echo ... removing all recon 4D.img
rm -f  $dir/*_4D.img*
echo ... removing all recon 4D.info
rm -f  $dir/*_4D.info
echo ... removing all recon 4D.dicom
rm -f  $dir/*_4D.dicom
echo ... removing all recon P info
rm -f $dir/*.P*.info
echo ... removing all recon P subdirs
rm -rf $dir/fmri_P*
rm -rf $dir/psf_P*
rm -rf $dir/t1map_P*
rm -rf $dir/pasl_P*
echo ... removing all recon S subdirs
rm -rf $dir/fmri_S*
rm -rf $dir/psf_S*
rm -rf $dir/t1map_S*
rm -rf $dir/pasl_S*
#####################################################
startdir=`pwd`
echo ... create a new run script in directory $startdir
script=$startdir/sge_rerun$$
rm -rf $script
cd $dir
fulldir=`pwd`
targzlist=`ls -1 *tmp.tar *tmp.tar.gz 2>/dev/null`
clear_analyze=1
if (test ${#targzlist} -eq 0) then
    option=1 # skip tar extract step
    cmd=" /fmri2/fmri/launch_sge /fmri2/fmri/autolocsort_excite_paradigm $fulldir/tempname $option $clear_analyze"
    echo ... setting command: $cmd
    echo $cmd >> $script
else
    option=0 # do everything
    nlist=`echo $targzlist | wc -w`
    if (test $nlist -eq 1) then
        cmd=" /fmri2/fmri/launch_sge /fmri2/fmri/autolocsort_excite_paradigm $fulldir/$targzlist $option $clear_analyze"
        echo ... setting command: $cmd
        echo $cmd >> $script
    else
        study=`basename $fulldir`
        listfile=$fulldir"/"$study"_autolocsort_"$$.flist
        rm -rf $listfile
        for filename in $targzlist; do
            echo $fulldir/$filename >> $listfile
        done
        cmd="/fmri2/fmri/launch_sge /fmri2/fmri/autolocsort_excite_paradigm $listfile $option $clear_analyze"
        echo ... setting command: $cmd
        echo ... showing flist contents:
        cat $listfile
        echo ...
        echo $cmd>> $script
    fi
fi
pfilelist=`ls -1 *.P????? 2>/dev/null`
for filename in $pfilelist
	do
	echo /fmri2/fmri/launch_sge /fmri2/fmri/autorecon_excite $fulldir/$filename >> $script
	done
cd $startdir
chmod +x $script
echo ... execute the new run script $script
#
# run the created script
$script
#
# sleep a few seconds and then show the queue
#
echo ... wait a few seconds
sleep 10
echo ... check autoprocessing queue status
qstat
