#!/bin/sh
# WARNING: This file was auto-generated. Do not edit!
echon() {
  echo -n "$*"
}
set -e
PATH="/bin:/usr/bin:/usr/local/bin:$PATH"
export PATH
program=`basename $0`
cd "${SVSCANDIR-/service}"

fatal() {
  echo "$program: Fatal error: $@" >&2
  exit 1
}

usage() {
  args=$1; min=$2; max=$3; shift 3
  if [ $args -lt $min -o $args -gt $max ]; then
    echo "$0: usage: $program $@" >&2
    exit 1
  fi
}

if [ "$1" = -q ]; then
  exec >/dev/null
  shift
fi

usage $# 1 9999 "[-q] service [service ...]"

start() {
  # Check the "run" file
  if [ ! -e "$1/run" ]; then
    fatal "'$1/run' file does not exist!"
  fi

  rm -f "$1/down"

  if ! svok "$1"; then
    echon "(supervise"
    count=1
    until svok "$1"; do
      echon .
      sleep 1
      count=$(($count+1))
      if [ $count -gt ${SVCTIMEOUT-15} ]; then
        echon ") "
	fatal "supervise for '$1' did not start!  Is svscan running?"
      fi
    done
    echon ") "
  fi

  svc -u "$1"
  svc-waitup "$1"

  echon "$1 "
}

for svc in "$@"; do

  if ! [ -e "$svc" ]; then
    fatal "Service '$svc' does not exist."
  fi

  echon "Starting $svc: "

  # Control the log process first, so that all messages are logged ...
  if [ -k "$svc" ]; then
    start "$svc/log"
  fi

  # ... and then the main process.
  start "$svc"

  echo "done."

done
