#!/bin/sh -e

. /usr/lib/s6-policy-common/lib/svctl-common.sh

if test -z "$1" ; then
  echo "svctl-start: usage: svctl-start service..." 1>&2
  exit 100
fi

timeout="$(svctl_timeout)"
rc=0
s6-rc -t "$timeout" -u change "$@" || rc=$?
if [ "$rc" -ne 0 ]; then
  echo "svctl-start: fatal: the state transition failed or exceeded the limit of" 1>&2
  echo "svctl-start:        ${timeout} ms (s6-rc rc=${rc})." 1>&2
  svctl_report_pending up "$@"
  exit "$rc"
fi

# s6-rc is satisfied -- that does not yet mean the service is running.
#
# If the supervisor holds down a longrun that s6-rc lists as active (after an
# "s6-svc -d" or a down file), the transition above was a no-op: from s6-rc's
# point of view there was nothing to do. Without this follow-up "svctl start"
# ended with 0 and the service stayed down (Deploy, 22.09.2026, dd81a1b).
svctl_resume_held_down "$timeout" "$@" || exit 1
