#! /bin/sh

# 
# Copyright 1999-2006 University of Chicago
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# 

prefix="${GLOBUS_LOCATION-prefix}"
exec_prefix="${prefix}"
sbindir="${exec_prefix}/sbin"
bindir="${exec_prefix}/bin"
includedir="${prefix}/include/globus"
datarootdir="${prefix}/share"
datadir="${datarootdir}"
libexecdir="${datadir}/globus"
sysconfdir="/etc"
sharedstatedir="/var/lib"
localstatedir="/var"

PATH="${bindir}:${sbindir}:${PATH}"

# 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="[ -r \${TARGETS} ] && 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$$"
rm -rf $TMPSCRIPT > /dev/null 2>&1
cat > $TMPSCRIPT  <<MYEND

(\sleep \$CACHE_SLEEP_TIME; \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 command -v globus-hostname > /dev/null ; then
    SELF_MACH="$(globus-hostname)"
elif command -v hostname > /dev/null; then
    SELF_MACH="$(hostname)"
elif command -v uname > /dev/null; then
    SELF_MACH="$(uname -n)"
fi
case "${TARGET_MACH}" in
    localhost|localhost.localdomain|${SELF_MACH})
        RESULT=`CACHE_SLEEP_TIME=0 sh $TMPSCRIPT`
        ;;
    *)
        RESULT=`echo "" | globusrun -s -r "${TARGET_MACH}"  "&(environment=(CACHE_SLEEP_TIME 60))(executable=/bin/sh)(library_path=\\$(GLOBUS_LOCATION)/lib)(arguments=\\$(GLOBUSRUN_GASS_URL)$TMPSCRIPT)" 2>&1`
        ;;
esac

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

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|-help|--help )
                Usage
        ;;
        *)
                echo "Unknown parameter ${arg}" 1>&2
                exit 1
        ;;
    esac
done


eval $CAT_TARGETS  | awk -F: '{print $1}' | sort | uniq | while read TARGET; do

clean_one "${TARGET}"

done
