#!/bin/sh
#
# Slackware Startup Script for Squid
# ----------------------------------
#
# Written by:  Marco A. Frias B. <marcofrias@linuxpackages.net>
#
# Changelog:
#
# 0.1: 14/Sep/2008 by Marco A. Frias B. <marcofrias@linuxpackages.net>
#      * Initial write Script.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Based on Startup Script written by:
#   Frank Bodammer, Peter Poeml, Klaus Singvogel <feedback@suse.de>
#   David Somero <xgizzmo@slackbuilds.org>
#
# NOTE:
#      This script can be run directly from a root command shell, using:
#
# # /etc/rc.d/rc.squid start
# or
# # /etc/rc.d/rc.squid stop
#
# For automatic startup, it should be added to /etc/rc.d/rc.local. A sample
# startup line:
#
# if [ -x /etc/rc.d/rc.squid ]; then
#   /etc/rc.d/rc.squid start
# fi
#
# For automatic shutdown, it should be added to /etc/rc.d/rc.local_shutdown. A
# sample startup line:
#
# if [ -x /etc/rc.d/rc.squid ]; then
#   /etc/rc.d/rc.squid stop
# fi
#

DAEMON=/usr/sbin/squid
CFGFILE=/etc/squid/squid.conf
PIDFILE=/var/run/squid.pid
PID=-1

# Only start if we can find the daemon.
if [ ! -x $DAEMON ] ; then
  echo "Squid is not installed correctly." >&2
  exit 1
fi

check_run() {
  if [ -f "$PIDFILE" ]; then
    read LINE < "$PIDFILE"
    [ -z "${LINE//[0-9]/}" -a -d "/proc/$LINE" ] && PID="$LINE"
    if [ -n "$PID" ]; then
      return 0
    fi
    return 1 # Squid is dead and /var/run pid file exists
  fi
  return 3 # Squid is not running
}

start() {
  # See if it's already running. Look *only* at the pid file.
  check_run
  case $? in
   0)
    echo "Squid (pid $PID) is running..."
    exit 0
    ;;
   1)
    echo "Squid is dead but pid file exists..."
    exit 1
    ;;
  esac

  # determine which one is the cache_swap directory
  CACHE_SWAP=$(awk '/^cache_dir/ {print $3}' $CFGFILE)
  [ -z "$CACHE_SWAP" ] && CACHE_SWAP=/var/cache/squid

  for cachedir in $CACHE_SWAP; do
    if [ ! -d $cachedir/00 ]; then # create missing cache directories
      echo "Creating swap directories in $cachedir"
      $DAEMON -z -F > /dev/null 2>&1
    fi
  done

  sleep 2
  echo -n "Starting Squid: "
  $DAEMON -DF
  if [ $? -eq 0 ]; then
    echo "ok"
  else
    echo "failed"
  fi
}

stop() {
  # See if it's already running. Look *only* at the pid file.
  check_run
  case $? in
   1)
    echo "Squid is dead but pid file exists..."
    exit 1
    ;;
   3)
    echo "Squid is not running..."
    exit 0
    ;;
  esac

  COUNTDOWN=$(awk '/^shutdown_lifetime/ {print $2}' $CFGFILE)
  [ -z "$COUNTDOWN" ] && COUNTDOWN=30

  echo -n "Shutting down Squid in $COUNTDOWN seconds: "
  $DAEMON -k shutdown

  while [ $COUNTDOWN -gt 0 ]; do

    $DAEMON -k check 2> /dev/null
    if [ $? -ne 0 ]; then
      break
    fi

    sleep 1
    echo -n "."
    COUNTDOWN=`expr $COUNTDOWN - 1`
  done
  echo ""
}

restart() {
  stop
  sleep 1
  start
}

reload() {
  # See if it's already running. Look *only* at the pid file.
  check_run
  case $? in
   1)
    echo "Squid is dead but pid file exists..."
    exit 1
    ;;
   3)
    echo "Squid is not running..."
    exit 0
    ;;
  esac

  echo -n "Reloading Squid: "
  $DAEMON -k reconfigure
  if [ $? -eq 0 ]; then
    echo "ok"
  else
    echo "failed"
  fi
}

status() {
  # See if it's already running. Look *only* at the pid file.
  check_run
  case "$?" in
   0)
    echo "Squid (pid $PID) is running..."
    exit 0
    ;;
   1)
    echo "Squid is dead but pid file exists..."
    exit 1
    ;;
   3)
    echo "Squid is not running..."
    exit 0
    ;;
  esac
}

# Parse command line parameters.
case $1 in
start)
  start
  ;;
stop)
  stop
  ;;
restart)
  restart
  ;;
reload)
  reload
  ;;
status)
  status
  ;;
*)
  # Print help
  echo "Usage: $0 {start|stop|restart|reload|status}" 1>&2
  exit 1
  ;;
esac

exit 0
