#!/bin/sh -e

if [ -z "$1" ] || [ "$1" = "help" ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
  availsubcommands="$(find /usr/lib/s6-policy-common/bin/ -name "svctl-*" -not -name "*.unlocked" -printf '%f\n' | sort | sed 's/svctl-//')"
  printf "svctl: usage: svctl subcommand subargs. Valid subcommands:" 1>&2
  printf "%s\n" "$availsubcommands" | tr '\n' ',' | sed 's/,$/\n/' | sed 's/,/, /g' 1>&2
  exit 100
fi

subcommand="$1"

if [ ! -x /usr/lib/s6-policy-common/bin/svctl-"$subcommand" ]; then
  echo "unknown subcommand: $subcommand"
  exit 100
fi

shift
exec /usr/lib/s6-policy-common/bin/svctl-"$subcommand" "$@"
