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

usage $# 1 1 service

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-/service}/"$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
