#!/bin/bash
DEBUG=0
E_OPTERROR=65
LogFile=/var/log/****/purgedir.log
Help( )
{
         echo Example:
         echo     " `basename $0` -A ALL  purge cache";
         echo     " `basename $0` -h : show help message";
         echo     " `basename $0` -v : show version";
         exit $E_OPTERROR
}
Version( )
{
         echo     "     
                                               
}
purgeCache()
{
         local Dir=$1
         echo "$(date "+%m-%d %H:%M:%S") $Dir is purged">> $LogFile
         [ "$DEBUG" -ge "1" ] &&echo "In purgeDir purgeing $Dir"
         N=1
         Conf=/usr/local/squid/etc/squid.conf
         pkill -9 squid 2>&1 1>> $LogFile
         pkill -9 refreshd >/dev/null 2>&1
         for x in $(awk '$1 ~ /^cache_dir/ {if ($3 ~ /.*\/$/) {print $3 "swap.state"} \
                 else {print $3 "/swap.state"}}' $Conf|xargs -n 1 echo)
         do
                 echo '' > $x
         done
         [ -e "/data/refresh_db" ]&&rm -f /data/refresh_db/*
         /etc/init.d/squid start 1>/dev/null #2>&1
         Stat=$(/sbin/pidof '(squid)')
         while [ -z "$Stat" ]
         do
         /etc/init.d/squid start 1>/dev/null #2>&1
         Stat=$(/sbin/pidof '(squid)')
         done
         if [ -z "$Stat" ]
          then
           echo "HTTP/1.0 400 Bad Request"
          else
           echo "HTTP/1.0 200 OK"
           /usr/local/squid/bin/refreshd -k url_verify
         fi
         #exit 0
}
funA()
{
         #no delay,check the permission
         local Arg=$1
         [ "$DEBUG" -ge "1" ]&&echo "In funA: Arg=$Arg"
         if [ "$Arg" = "ALL" ];then
                 purgeCache $Arg
         else
                 echo "WARNING: Option ' -A $Arg' is invalidated!"
         fi
}
opt=0
while getopts "VvhHA:" Option
do
   case $Option in
     A )
         funA $OPTARG
         opt=1
         ;;
     v|V)
         Version
         opt=1
         ;;
     h|H)
         Help
         opt=1
         ;;
     * )
         Help
         opt=1
     ;;
   esac
done
if [ $opt -eq 0 ]
then
         Help
fi
exit 0