#!/bin/sh
########################################################################
# Begin /etc/rc.d/init.d/git-daemon
#
# Description : Start/Stop git-daemon
#
# Authors     : Thomas Trepl - thomas@linuxfromscratch.org
#
# Version     : LFS x.x
#
# Notes       :
#
########################################################################

### BEGIN INIT INFO
# Provides:            git-daemon
# Required-Start:      $network
# Should-Start:
# Required-Stop:       $network
# Should-Stop:
# Default-Start:       2 3 4 5
# Default-Stop:        0 1 6
# Short-Description:   Start git-daemon
# Description:         Start git-daemon to publish repositories
# X-LFS-Provided-By:
### END INIT INFO

. /lib/lsb/init-functions

GIT_DAEMON_OPTS=""
GIT_BASE_DIR="/srv/git/"
DFT_REPO_DIR="$GIT_BASE_DIR"
if [ -f "/etc/sysconfig/git-daemon" ]; then
   . /etc/sysconfig/git-daemon
fi
PID_FILE="/run/git-daemon.pid"
GIT_BIN="/usr/bin/git"
GIT_USR="git"
GIT_GRP="git"
GIT_HOME=$(eval echo ~$GIT_USR)

case "${1}" in
   start)
      log_info_msg "Starting git-daemon ... "
      export HOME=$GIT_HOME
      $GIT_BIN daemon \
               --detach --pid-file=$PID_FILE \
               --user=$GIT_USR --group=$GIT_GRP \
               --reuseaddr \
               --base-path=$GIT_BASE_DIR \
               $GIT_DAEMON_OPTS \
               $DFT_REPO_DIR
      evaluate_retval
      ;;

   stop)
      log_info_msg "Stopping git-daemon ... "
      killproc -p $PID_FILE $GIT_BIN
      evaluate_retval
      ;;

   restart)
      ${0} stop
      sleep 1
      ${0} start
      ;;

   *)
      echo "Usage: ${0} {start|stop|restart}"
      exit 1
      ;;
esac

exit 0

# End /etc/rc.d/init.d/git-daemon
