#!/bin/sh
# 
SQ_DIR=/usr/local/squid
#
case $1 in

'start')
        echo "Starting SQUID ..."
        nohup /sbin/init.d/squid_start
        ;;

'start_minimal')
        echo "Starting SQUID ..."
        #  Minimalsystem ohen Berechtigungspruefung, ACLs ...
        nohup /sbin/init.d/squid_start_minimal
        ;;


'status')
        ps -ef | grep -i squid | grep -v grep | grep -v "init.d" \
               | grep -v status
        ;;


'start_terminal')
        $SQ_DIR/bin/squid -D -NCd1
        ;;


'debug_on')
        $SQ_DIR/bin/squid -k debug
        ;;


'show_cache')
        # die 25 groessten Objekte im cache anzeigen
        sort -r -n +4 -5 $SQ_DIR/logs/access.log | awk '{print $5, $7}' | head -25
        ;;


'show_cache_log')
        # Cache-Statistik anzeigen
        cat $SQ_DIR/logs/cache.log | grep -v empty
        ;;

'show_cache_hits')
        # Cache-Statistik anzeigen
        $SQ_DIR/bin/show_cache_hits
        ;;


'show_active_users')
        # aktive User anzeigen
        tail -800 $SQ_DIR/logs/access.log | \
             gawk '{print $8}' | sort | uniq
        ;;


'rotate')
        # Logfiles freigeben
        echo $0  ich rotiere jetzt...
        $SQ_DIR/bin/squid -k rotate
        ;;


'show_all_users')
        # alle jemals angemeldeten User anzeigen
        cat $SQ_DIR/logs/access.log | \
              gawk '{print $8}' | sort | uniq
        ;;


'test')
        $SQ_DIR/bin/client -vp 8080 http://squid.nlanr.net/
        ;;

'test1')
        $SQ_DIR/bin/client -vp 8080 http://de.yahoo.com/
        ;;

'test2')
        $SQ_DIR/bin/client -vp 8080 http://www.web.de/
        ;;

'test3')
        $SQ_DIR/bin/client -vp 8080 http://web.de/
        ;;


'test4')
        $SQ_DIR/bin/client -vp 8080 http://www.webshit.dk
        ;;

'test5')
        $SQ_DIR/bin/client -vp 8080 http://fw.mannesmann.de/
        ;;


'stop')
         echo "Shutting down Squid HTTP proxy/cache."
         $SQ_DIR/bin/squid -k shutdown

         countdown=60
         while $SQ_DIR/bin/squid -k check 2> /dev/null && [ $countdown  > 0 ]
         do
                 echo "Waiting $countdown seconds for squid process to terminate...\r"
                 sleep 1
                 countdown=`expr $countdown - 1`
         done
         if [ $countdown -gt 0 ]
         then
                 echo "\nSquid terminated."
         else
                 echo -e "\nGiving up."
         fi
        ;;


'reload')
         echo "Reconfiguring Squid HTTP proxy/cache."
         $SQ_DIR/bin/squid -k reconfigure
         ;;


'client_mgr')
         echo "  "
         echo "Kommando:   /usr/local/squid/bin/client -vp 8080 mgr: | more"
         echo "  "
         ;;

'calamaris')
         cd  $SQ_DIR/logs
         cat access.log | $SQ_DIR/bin/calamaris -a >calamaris.out
         ;;

'tagesreport')
         cd  $SQ_DIR/logs
         cat access.log.0 | $SQ_DIR/bin/calamaris -amH 'Daily' >tagesreport
         ;;


'stat1')
        # Seiten im Cache anzeigen, die nicht in der Ausnahmeliste stehen
        $SQ_DIR/bin/client -vp 8080 mgr:ipcache \
            | grep -f $SQ_DIR/etc/porno.txt -v
        ;;


'stat2')
        # Aufgerufene Seiten die nicht in der Ausnahmeliste stehen
        grep -f $SQ_DIR/etc/porno.txt -v $SQ_DIR/logs/access.log \
           | grep -v TCP_DENIED | $SQ_DIR/bin/conv_date
        ;;


*)
        echo "usage: $0  Bitte Parameter eingeben:"
        echo "           start              - SQUID starten"
        echo "           stop               - SQUID beenden"
        echo "           start_minimal      - SQUID Minisystem starten"
        echo "           status             - SQUID-Status (ps -ef)"
        echo "           reload             - SQUID-Konfiguration neu einlesen"
        echo "           rotate             - SQUID-Logfiles freigeben"
        echo "           show_cache         - SQUID-Cache anzeigen (25 groesste Objekte)"
        echo "           show_cache_log     - SQUID-Logfile anzeigen"
        echo "           show_cache_hits    - SQUID-Cache-Statistik anzeigen"
        echo "           show_active_users  - SQUID aktive User anzeigen"
        echo "           show_all_users     - SQUID alle jemals aktiven User anzeigen"
        echo "           calamaris          - SQUID-Auswertung mit calamaris"
        echo "           tagesreport        - SQUID-Auswertung mit calamaris"
        echo "           start_terminal     - SQUID starten mit Terminalausgabe (Test)"
        echo "           debug_on           - SQUID Debugging anschalten"
        echo "           client_mgr         - SQUID-Client: Aufruf anzeigen"
        echo "           stat1              - SQUID-Client: Seiten im Cache die"
        echo "                                nicht in der Pornoliste stehen"
        echo "           stat2              - SQUID-Client: Aufgerufene Seiten die"
        echo "                                nicht in der Pornoliste stehen"
        echo "           test...            - SQUID Testaufrufe"
	exit 1
        ;;
esac
exit 0

