#!/usr/bin/execlineb -S0

# This file is executed (not as process 1!) as soon as s6-svscan
# starts, with the original stdin/out/err, but NOT the original
# environment.
# Purpose of this file: to perform all the one-time initialization tasks.

# Merge environments from our custom stage into current context
s6-envdir -I /var/run/s6/env-stage2

# this env decides what to do if stage2 fails
backtick -D 0 -n S6_BEHAVIOUR_IF_STAGE2_FAILS { printcontenv S6_BEHAVIOUR_IF_STAGE2_FAILS }
import -u S6_BEHAVIOUR_IF_STAGE2_FAILS

foreground
{
  if
  {
    /etc/s6/init/init-stage2-redirfd
    foreground
    {
      ##
      ## fix-attrs.d: ensure owners and permissions are correct
      ##

      if { s6-echo "[fix-attrs.d] applying owners & permissions fixes..." }
      if
      {
        pipeline { s6-ls -0 -- /etc/fix-attrs.d }
        pipeline { s6-sort -0 -- }
        forstdin -0 -- i
        import -u i
        if { s6-echo -- "[fix-attrs.d] ${i}: applying... " }
        foreground { redirfd -r 0 /etc/fix-attrs.d/${i} fix-attrs }
        import -u ?
        if { s6-echo -- "[fix-attrs.d] ${i}: exited ${?}." }
        ifelse { s6-test ${S6_BEHAVIOUR_IF_STAGE2_FAILS} -eq 0 } { exit 0 }
        exit ${?}
      }
      if { s6-echo -- "[fix-attrs.d] done." }


      ##
      ## cont-init.d: one-time init scripts
      ##

      if { s6-echo "[cont-init.d] executing container initialization scripts..." }
      if
      {
        pipeline { s6-ls -0 -- /etc/cont-init.d }
        pipeline { s6-sort -0 -- }
        forstdin -o 0 -0 -- i
        import -u i
        if { s6-echo -- "[cont-init.d] ${i}: executing... " }
        foreground { /etc/cont-init.d/${i} }
        import -u ?
        if { s6-echo -- "[cont-init.d] ${i}: exited ${?}." }
        ifelse { s6-test ${S6_BEHAVIOUR_IF_STAGE2_FAILS} -eq 0 } { exit 0 }
        exit ${?}
      }
      if { s6-echo -- "[cont-init.d] done." }

      ##
      ## services.d: long-lived processes to be supervised
      ##


      if { s6-echo "[services.d] starting services" }
      if
      {
        pipeline { s6-ls -0 -- /etc/services.d }
        forstdin -0 -p -- i
        import -u i
        if { s6-test -d /etc/services.d/${i} }
        s6-hiercopy /etc/services.d/${i} /var/run/s6/services/${i}
      }
      if { s6-svscanctl -a /var/run/s6/services }
      if { s6-echo -- "[services.d] done." }
    }
    import -u ?
    ifelse { s6-test ${S6_BEHAVIOUR_IF_STAGE2_FAILS} -eq 0 } { exit 0 }
    exit ${?}
  }


  ##
  ## The init is complete. If the user has given a CMD, run it now, then
  ## kill everything when it exits.
  ##

  if -t { s6-test $# -ne 0 }
  foreground { s6-setsid -gq -- with-contenv $@ }
  import -u ?
  if { s6-echo -- "${1} exited ${?}" }

  # Make CMD exit code available in stage3
  foreground { redirfd -w 1 /var/run/s6/env-stage3/S6_CMD_EXITED s6-echo -n -- "${?}" }

  # Stop supervision tree
  foreground { s6-svscanctl -t /var/run/s6/services }

  # Race condition here, but it's ok: the sync in stage 3 takes ages,
  # and if we get killed by the SIGTERM instead of the SIGKILL, meh, who cares.

  # Wait to be nuked
  s6-pause -th
}
import -u ?
if { s6-test ${?} -ne 0 }
if { s6-test ${S6_BEHAVIOUR_IF_STAGE2_FAILS} -ne 0 }
ifelse { s6-test ${S6_BEHAVIOUR_IF_STAGE2_FAILS} -ne 1 }
{
  # Stop supervision tree (if it wasn't already due to CMD execution)
  if { s6-test ! -f /var/run/s6/env-stage3/S6_CMD_EXITED }
  s6-svscanctl -t /var/run/s6/services
}
s6-echo -- "\n!!!!!\n init-stage2 failed.\n!!!!!"
