#! /bin/sh

. ${GLOBUS_LOCATION}/libexec/globus-script-initializer
globus_source ${libexecdir}/globus-sh-tools.sh

#  CVS Information:
#     $Source: /home/globdev/CVS/globus-packages/gass/cache_program/source/globus-gass-cache-destroy,v $
#     $Date: 2003/03/28 22:50:27 $
#     $Revision: 1.17 $
#     $Author: bester $
 
# defaults list of machine to clean
TARGETS=${HOME}/.globus/my-contacts

# command to list it, will be replaced with a simple echo if one machine specified
CAT_TARGETS="${GLOBUS_SH_CAT-cat} \${TARGETS}"

###############################################################################
# USAGE
Usage()
{
    echo "globus-gass-cache-destroy [ -t <machine> | -f <file> ]"
    echo "    Will destroy (clean without coherence check) the globus cache"
    echo "    on all the machine listed in ~/.globus/my-contacts, "
    echo "    or in <file> if -f option used, or only on the machine"
    echo "    specified with the -t option." 
    exit 1
}
 
###############################################################################
clean_one()
{

TARGET_MACH="${1}"

TMPSCRIPT="${local_tmpdir}/cdscript$$"
${GLOBUS_SH_RM-rm} -rf $TMPSCRIPT > /dev/null 2>&1
${GLOBUS_SH_CAT-cat} > $TMPSCRIPT  <<MYEND

(\${GLOBUS_SH_SLEEP} \$CACHE_SLEEP_TIME; \${GLOBUS_SH_RM}  -rf \$HOME/.globus/.gass_cache)> /dev/null 2>&1 < /dev/null&

if test "\$CACHE_SLEEP_TIME" = "0"; then
    wait
fi

echo successful
exit 0
 
MYEND

if test ${TARGET_MACH} = "localhost" -o ${TARGET_MACH} = "localhost.localdomain"; then
    RESULT=`CACHE_SLEEP_TIME=0 ${bindir}/globus-sh-exec $TMPSCRIPT`
else
    RESULT=`echo "" |${bindir}/globusrun -s -r "${TARGET_MACH}"  "&(environment=(CACHE_SLEEP_TIME 60))(executable=\\$(GLOBUS_LOCATION)/bin/globus-sh-exec)(library_path=\\$(GLOBUS_LOCATION)/lib)(arguments=\\$(GLOBUSRUN_GASS_URL)$TMPSCRIPT)" 2>&1`
fi

THIS_STATUS=$?
if [ $THIS_STATUS -ne 0 ]
then
    echo "Cleaning failed: " 1>&2
    echo "$RESULT" 1>&2
    ${GLOBUS_SH_RM-rm} -rf $TMPSCRIPT > /dev/null 2>&1
    exit $THIS_STATUS
fi

${GLOBUS_SH_RM-rm} -rf $TMPSCRIPT > /dev/null 2>&1

if [ "x$RESULT" != "xsuccessful" ]
then
    echo "globus-gass-cache-destroy: $RESULT" 1>&2
fi

}



###############################################################################
# main
###############################################################################
 
#save the arguments
allargs="$@"
 
while [ $# -ge 1 ]; do
    arg="$1"
    case "${arg}" in
        -f ) 
                shift
                if [ $# -ge 1 ]
                then
                        TARGETS="$1"
                        shift
                else
                        Usage
                fi
        ;;
	-t ) 
                shift
                if [ $# -ge 1 ]
                then
                        CAT_TARGETS="echo $1"
                        shift
                else
                        Usage
                fi
        ;;
        -h )
                Usage
	;;
	-help )
		Usage
        ;;
    esac
done


#eval $CAT_TARGETS  | ${GLOBUS_SH_EGREP-egrep} '(fork|poe)' | ${GLOBUS_SH_AWK-awk} -F: '{print $1}' | ${GLOBUS_SH_SORT-sort} | ${GLOBUS_SH_UNIQ-uniq} | while read TARGET; do
#eval $CAT_TARGETS  | ${GLOBUS_SH_SED-sed} '/fork/ p;/poe/ p; d' | ${GLOBUS_SH_AWK-awk} -F: '{print $1}' | ${GLOBUS_SH_SORT-sort} | ${GLOBUS_SH_UNIQ-uniq} | while read TARGET; do
eval $CAT_TARGETS  | ${GLOBUS_SH_AWK-awk} -F: '{print $1}' | ${GLOBUS_SH_SORT-sort} | ${GLOBUS_SH_UNIQ-uniq} | while read TARGET; do

clean_one "${TARGET}"

done


