#!/bin/bash
### BEGIN INIT INFO
# Provides:          invirt-dev
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Invirt development configuration
# Description:       
### END INIT INFO

# Author: Invirt project <invirt@mit.edu>

# Do NOT "set -e"

NAME=invirt-dev
GEN_FILES=/srv/repository/conf/distributions

. /lib/init/std-init.sh
. /lib/init/gen-files.sh

do_start()
{
    # Return
    #  0 if daemon has been started
    #  1 if daemon was already running
    #  2 if daemon could not be started
    ret=0

    gen_files || ret=2
    for d in db dists lists pool; do
        dir="/srv/repository/$d"
        if ! [ -e "$dir" ]; then
            mkdir -p "$dir"
        fi
        chown -R repository:nogroup "$dir"
    done
    reprepro-env export || ret=2

    invirt-build-conf || ret=2

    daemon -n invirt-dev-git-daemon -- git daemon \
        --user=git \
        --group=nogroup \
        --syslog \
        --verbose \
        --reuseaddr \
        --export-all \
        --base-path=/srv/git \
        /srv/git || ret=$?

    return $ret
}

do_stop()
{
    daemon -n invirt-dev-git-daemon --stop
    return $?
}

do_reload()
{
    do_start
}

std_init "$@"
