#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#
#
# htcacheclean2    Startup script for the Apache cache cleaner
#
# chkconfig: - 85 15
# description: The Apache htcacheclean daemon maintains and prunes the
#              size of the mod_disk_cache cache directory.
# processname: htcacheclean2
# pidfile: /var/lock/subsys/htcacheclean2.pid
# config: /etc/sysconfig/htcacheclean2
#
### BEGIN INIT INFO
# Provides: htcacheclean2
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Should-Start: httpd2
# Short-Description: start and stop Apache htcacheclean
# Description: The Apache htcacheclean daemon maintains a mod_disk_cache
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

# What were we called? Multiple instances of the same daemon can be
# created by creating suitably named symlinks to this startup script
prog=$(basename $0 | sed -e 's/^[SK][0-9][0-9]//')

# edit /etc/sysconfig/${prog} to change this
HTCACHECLEAN_SIZE=300M
HTCACHECLEAN_DAEMON_INTERVAL=120
HTCACHECLEAN_PATH=/var/cache/httpd2/mod_cache_disk
HTCACHECLEAN_OPTIONS=""

if [ -f /etc/sysconfig/${prog} ]; then
        . /etc/sysconfig/${prog}
fi

# Path to htcacheclean, server binary, and short-form for messages.
htcacheclean=${HTCACHECLEAN-/usr/sbin/htcacheclean2}
htcacheclean_start=${htcacheclean}-daemon-start
lockfile=/var/lock/subsys/${prog}
pidfile=/var/run/httpd2/${prog}.pid
interval=$HTCACHECLEAN_DAEMON_INTERVAL
cachepath=$HTCACHECLEAN_PATH
limit=$HTCACHECLEAN_SIZE
httpduser=${HTTPDUSER-apache2}
OPTIONS=$HTCACHECLEAN_OPTIONS
RETVAL=0

start() {
	start_daemon --pidfile "$pidfile" --lockfile "$lockfile" \
 		--user $httpduser --name $prog -- $htcacheclean_start \
		-d "$interval" -p "$cachepath" -l "$limit" -P "$pidfile" $OPTIONS
        RETVAL=$?
        return $RETVAL
}
stop() {
	stop_daemon --pidfile "$pidfile" --lockfile "$lockfile" \
 		--name $prog -- $htcacheclean
	RETVAL=$?
        return $RETVAL
}
restart()
{
	stop
	start
}
brieftatus() {
	status --pidfile "$pidfile" --lockfile "$lockfile" \
 		--name $prog -- $htcacheclean
	RETVAL=$?
        return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
        brieftatus
	;;
  restart)
	restart
	;;
  condstop)
	if [ -e "$lockfile" ]; then
		stop
	fi
	;;
  update|condrestart)
	if [ -e "$lockfile" ]; then
		restart
	fi
	;;
  *)
	echo $"Usage: $prog {start|stop|restart|condstop|condrestart|status|help}"
	exit 1
esac

exit $RETVAL
