#! /bin/sh

# init script for starting and stopping cvsd
# Copyright (C) 2001, 2002, 2003, 2004, 2006 Arthur de Jong
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

### BEGIN INIT INFO
# Provides:          cvsd
# Required-Start:    $local_fs $syslog
# Required-Stop:     $local_fs $syslog
# Should-Start:      $remote_fs $network 
# Should-Stop:       $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      S 0 1 6
# Short-Description: cvs pserver chroot wrapper
# Description:       cvsd is a wrapper program for cvs in pserver mode.
#                    It will run 'cvs pserver' under a special uid/gid in
#                    a chroot jail.
### END INIT INFO

CVSD_BIN=@CVSD_BIN@
DESC="cvs pserver chroot wrapper"
CVSD_CFG=@CONFIGFILE@

[ -x "$CVSD_BIN" ] || exit 5
[ -f "$CVSD_CFG" ] || exit 6

PIDFILE=`sed -n 's/^ *PidFile *\([^ ]*\) *$/\1/p' < $CVSD_CFG`

# find the flavor of the echo command (stolen from configure)
case `echo "testing\c"; echo 1,2,3`,`echo -n testing; echo 1,2,3` in
  *c*,-n*) ECHO_N= ECHO_C='
' ;;
  *c*,*  ) ECHO_N=-n ECHO_C= ;;
  *)       ECHO_N= ECHO_C='\c' ;;
esac

case "$1" in
start)
  echo $ECHO_N "Starting $DESC: cvsd$ECHO_C"
  if [ -n "$PIDFILE" ] && [ -f "$PIDFILE" ] && \
     kill -s 0 `cat $PIDFILE` > /dev/null 2>&1
  then
    echo " already running."
    exit 0
  fi
  RET=0
  $CVSD_BIN -f $CVSD_CFG || RET=7
  echo "."
  exit $RET
  ;;
stop)
  echo $ECHO_N "Stopping $DESC: cvsd$ECHO_C"
  if [ -n "$PIDFILE" ]
  then
    if [ -f "$PIDFILE" ]
    then
      :
    else
      echo " not running."
      exit 0
    fi
    kill `cat $PIDFILE 2> /dev/null` 2> /dev/null
    echo "."
    rm -f $PIDFILE
  else
    echo " unable to stop, no PidFile!"
    exit 1
  fi
  ;;
restart|force-reload)
  $0 stop
  sleep 2
  $0 start || exit 7
  ;;
status)
  echo $ECHO_N "Status of $DESC: $ECHO_C"
  if [ -n "$PIDFILE" ]
  then
    if [ -f "$PIDFILE" ]
    then
      if kill -s 0 `cat $PIDFILE` > /dev/null 2>&1
      then
        echo "running."
        exit 0
      else
        echo "stopped."
        exit 1
      fi
    else
      echo "stopped."
      exit 3
    fi
  else
    if ps -e | grep cvsd | grep -v grep > /dev/null 2>&1
    then
      echo "probably running. (no PidFile in cvsd.conf)"
      exit 0
    else
      echo "probably not running. (no PidFile in cvsd.conf)"
      exit 3
    fi
  fi
  ;;
*)
  echo "Usage: $0 {start|stop|restart|force-reload|status}" >&2
  exit 3
  ;;
esac

exit 0
