#!/bin/execlineb -S0

##
## ensure our vital fifo exists
##

if { s6-rmrf /var/run/s6/uncaught-logs-fifo }
if { s6-mkfifo -m 0600 -- /var/run/s6/uncaught-logs-fifo }


##
## ensure ownership & permissions for our vital files
##

if { s6-rmrf /var/log/s6-uncaught-logs }
if { s6-mkdir -p /var/log/s6-uncaught-logs }
if { s6-envuidgid -D 32768:32768 nobody s6-chown -U /var/log/s6-uncaught-logs }
if { s6-chmod 2700 /var/log/s6-uncaught-logs }


##
## init the scandir with our base services
##

if { s6-rmrf /var/run/s6/services }
if { s6-hiercopy /etc/s6/services /var/run/s6/services }


##
## fork the "init-stage2" script
##

background
{
  # block until the supervision tree is running
  redirfd -w 3 /var/run/s6/uncaught-logs-fifo
  fdclose 3

  # add some environment
  s6-envdir -- /etc/s6/init/env-stage2

  # run the script
  /etc/s6/init-catchall/init-stage2 $@
}
unexport !


##
## run the rest of stage 1 with sanitized descriptors
##

redirfd -r 0 /dev/null
redirfd -wnb 1 /var/run/s6/uncaught-logs-fifo
fdmove -c 2 1


##
## start the supervision tree
##

s6-svscan -t0 /var/run/s6/services
