#!/bin/sh
# dnscache/rc.main
# perp run script for dnscache service
# wcm, 2009.10.07 - 2011.02.02
# ===

exec 2>&1

TARGET=${1}
SVNAME=${2:-dnscache}

## create a seed file:
make_seed() {
  if test ! -f ./seed ; then
    dd if=/dev/urandom of=./seed bs=128 count=1
    chmod 0600 ./seed
    echo "*** >> random seed generated"
  fi
}


## make/update roots:
make_roots() {
  echo "*** ${SVNAME}: updating root servers..."
  dnsip `dnsqr ns . | awk '/answer:/ {print $5;}' | sort` \
      > ./root/servers/@.new
  mv ./root/servers/@.new ./root/servers/@
}


## start target:
start() {
  echo "*** ${SVNAME}: starting dnscache..."
  make_seed
  ## note: configure "datalimit" with rlimit d=NNN, eg:
  ##     -R 'd=3000000:o=250'
  exec \
    runtool \
        -e ./dnscache.env \
        -F '0<./seed' \
        -R 'o=250' \
        -U dnscache \
            dnscache
}


## reset target:
reset() {
  case $3 in
    'exit' )
        echo "*** ${SVNAME}: exited status $4" ;;
    'signal' )
        echo "*** ${SVNAME}: killed on signal $5" ;;
    * ) 
        echo "*** ${SVNAME}: stopped ($3)" ;;
  esac 
  exit 0
}


## branch to target:
eval ${TARGET} "$@"

### EOF
