#!/bin/bash

. /etc/sysconfig/subversion

PIDFILE=/var/run/subversion/svnserve.pid
PROGRAM=/usr/bin/svnserve
RUNLEVEL=3
NEEDS="+network +remote_fs"

OPTS="-d --pid-file $PIDFILE -r $REPOSITORIES"
[[ -n "$LISTEN_HOST" ]] && OPTS="$OPTS --listen-host=$LISTEN_HOST"
[[ -n "$LISTEN_PORT" ]] && OPTS="$OPTS --listen-port=$LISTEN_PORT"
[[ "$READ_ONLY" =~ "^[Yy].*$" ]] && OPTS="$OPTS -R"
[[ "$THREADS" =~ "^[Yy].*$" ]] && OPTS="$OPTS -T"

. /etc/init.d/smgl_init

start()
{
  mkdir -p $(dirname $PIDFILE)
  if [[ -z "$DAEMON_USER" ]]; then
    loadproc "${PROGRAM}" $OPTS
  else
    SOPTS="-u $DAEMON_USER"
    if [[ -n "$DAEMON_GROUP" ]]; then
      SOPTS="$SOPTS -g $DAEMON_GROUP"
    fi
    # This can succeed to start svnserve,
    # but that could quickly fail without us noticing.
    chown "$DAEMON_USER" $(dirname $PIDFILE) &&
    # Sudo does not need a working shell.
    sudo $SOPTS "$PROGRAM" $OPTS
    # Kludge to detect failure (because of user permissions, normally).
    sleep 1
    test -e /proc/$(cat $PIDFILE)/exe
    evaluate_retval
  fi
}
