#!/bin/sh
# Copyright (c) 2008 Alon Swartz <alon@turnkeylinux.org> - all rights reserved

### BEGIN INIT INFO
# Provides:          loggerhead
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      1
### END INIT INFO

# we are using serve-branches as start-loggerhead is buggy
DESC="Loggerhead Bazaar Browser"
NAME=loggerhead
DAEMON=/usr/bin/serve-branches
PIDFILE=/var/run/$NAME.pid
CONFFILE=/etc/default/serve-branches

set -e
. /lib/lsb/init-functions

# exit if the daemon or conffile is not available
[ -x "$DAEMON" ] || exit 0
[ -e "$CONFFILE" ] || exit 0

# source conf file - must include DAEMON_OPTIONS
. $CONFFILE
if [ ! -n "$DAEMON_OPTIONS" ]; then echo "DAEMON_OPTIONS not set"; exit 1; fi

case "$1" in
  start)
    log_begin_msg "Starting $DESC"
    start-stop-daemon --start --make-pidfile --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTIONS > /dev/null 2>&1 &
    log_action_end_msg $?
    ;;

  stop)
    log_begin_msg "Stopping $DESC"
    STOP="start-stop-daemon --stop --retry=TERM/30/KILL/5 --pidfile $PIDFILE"
    if $STOP --quiet; then
        rm -f $PIDFILE
    fi
    log_action_end_msg $?
    ;;

  restart|reload|force-reload)
    echo "Error: argument '$1' not supported" >&2
    exit 3
    ;;

  *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop}" >&2
    exit 1
    ;;
esac

exit 0

