#!/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`

warn() {
  echo "$program: Warning: $@" >&2
}

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

usage_nocd() {
  min=$1; max=$2; opts=$3; msg=$4; shift 4

  while getopts q$opts flag; do
    case $flag in
      q)
        exec >/dev/null
	;;
      \? | :)
        exit 1
	;;
      ?)
        eval "opt_${flag}=true"
	;;
    esac
  done

  shift=$(( $OPTIND - 1 ))
  shift $shift

  if [ $# -lt $min -o $# -gt $max ]; then
    echo "$0: usage: $program [-q] $msg" >&2
    exit 1
  fi
}

usage() {
  usage_nocd "$@"
  cd "$SVSCANDIR"
}

SVSCANDIR="${SVSCANDIR-/service}"
SVCLOCKDIR="${SVCLOCKDIR-/var/service}"
usage 1 1 '' service "$@"
shift $(( $OPTIND - 1 ))

svc="$1"

svc-stop -q "$svc"

if ! [ -L "$svc" ]; then
  fatal "Service '$svc' is not symlinked into /service and can't be removed."
fi

# Remove the symlink without loosing track of where it points to
cd "$svc"
rm "$SVSCANDIR/$svc"

# Stop the supervise tasks
if [ -d log ]; then
  svc -dx log
  sleep 1
  rm log/down log/supervise/{control,lock,ok,status}
  rmdir log/supervise
fi
svc -dx .
sleep 1
rm down supervise/{control,lock,ok,status}
rmdir supervise
