#!/bin/sh

set -e

# source debconf library.
. /usr/share/debconf/confmodule
db_version 2.0
db_capb backup

# set title
db_title "Configuring cvsd"

configfile="/etc/cvsd/cvsd.conf"

# read values from the current configuration file
if [ -r "$configfile" ]
then

  # Location of Chroot jail
  rootjail=`sed -n 's/^[[:space:]]*RootJail[[:space:]][[:space:]]*\([^[:space:]]*\)[[:space:]]*$/\1/p' < "$configfile"`
  [ "x$rootjail" = "x" ] && rootjail="none"
  db_set cvsd/rootjail "$rootjail"

  # Maximum number of connections
  maxconnections=`sed -n 's/^[[:space:]]*MaxConnections[[:space:]][[:space:]]*\([^[:space:]]*\)[[:space:]]*$/\1/p' < "$configfile"`
  [ "x$maxconnections" = "x" ] && maxconnections=0
  db_set cvsd/maxconnections "$maxconnections"

  # Nice value to run at
  nice=`sed -n 's/^[[:space:]]*Nice[[:space:]][[:space:]]*\([^[:space:]]*\)[[:space:]]*$/\1/p' < "$configfile"`
  [ "x$nice" = "x" ] && nice=0
  db_set cvsd/nice "$nice"

  # Umask to use
  umask=`sed -n 's/^[[:space:]]*Umask[[:space:]][[:space:]]*\([^[:space:]]*\)[[:space:]]*$/\1/p' < "$configfile"`
  [ "x$umask" = "x" ] && umask=027
  db_set cvsd/umask "$umask"

  # Address-Port combinations to listen on
  listen=`sed -n 's/^[[:space:]]*Listen[[:space:]][[:space:]]*\([^[:space:]]*\)[[:space:]][[:space:]]*\([^[:space:]]*\)[[:space:]]*$/\1 \2/p' < "$configfile"`
  listen=`echo "$listen" | tr '\n' ' ' | sed 's/ *$//'`
  [ "x$listen" = "x" ] && listen="* 2401"
  db_set cvsd/listen "$listen"

  # get current repositories from configfile
  reposs=`sed -n 's/^[[:space:]]*Repos[[:space:]][[:space:]]*\([^[:space:]]*\)[[:space:]]*$/\1/p' < "$configfile"`
  reposs=`echo "$reposs" | tr '\n' ':' | sed 's/:*$//'`
  db_set cvsd/repositories "$reposs"

  # read current limits
  limits=""
  for i in `sed -n 's/^[[:space:]]*Limit[[:space:]][[:space:]]*\([^[:space:]]*\)[[:space:]][[:space:]]*[^[:space:]]*[[:space:]]*$/\1/p' < "$configfile"`
  do
    limit=`sed -n 's/^[[:space:]]*Limit[[:space:]][[:space:]]*'"$i"'[[:space:]][[:space:]]*\([^[:space:]]*\)[[:space:]]*$/\1/p' < "$configfile"`
    if db_set "cvsd/limit_$i" "$limit"
    then
      if [ -n "$limits" ]
      then
        limits="$limits, $i"
      else
        limits="$i"
      fi
    fi
  done
  db_set cvsd/limits "$limits"

fi

state="general"
while [ "$state" != "done" ]
do
  case "$state" in
  general)

    # Location of Chroot jail
    db_input medium cvsd/rootjail || true

    # Maximum number of connections
    db_input low cvsd/maxconnections || true

    # Nice value to run at
    db_input low cvsd/nice || true

    # Umask to use
    db_input low cvsd/umask || true

    # Address-Port combinations to listen on
    db_input low cvsd/listen || true

    state="repositories"
    db_go || exit 1

    # TODO: add error checking on options (especially listen)

    ;;
  repositories)

    # Repositories to have
    db_get cvsd/rootjail
    [ "$RET" = "/" ] || [ "$RET" = "none" ] && RET=""
    db_subst cvsd/repositories rootjail "$RET"
    db_input high cvsd/repositories || true

    state="whichlimits"
    db_go || state="general"

    ;;
  whichlimits)

    # Limits on cvs command
    db_capb multiselect
    db_input low cvsd/limits || true

    state="limitlist"
    db_go || state="repositories"

    ;;
  limitlist)

    # ask specific limits questions
    db_get cvsd/limits
    for i in `echo $RET | sed 's/,//g'`
    do
      db_input low "cvsd/limit_$i" || true
    done

    state="done"
    db_go || state="whichlimits"

    ;;
  esac
done

exit 0


syntax highlighted by Code2HTML, v. 0.9.1