#!/usr/bin/sh

## We only want this to script to do things when s6-rc is detected
while [ $(ps -p1 | grep -ic "s6-svscan") -eq 1 ] && [ -e /run/s6-rc ]; do
	export PATH=$PATH:/usr/sbin:/usr/lib/execline/bin:/usr/libexec
	trap 's6-rc -l /tmp/$(id -u)/s6-rc -D change default && rm -rf /tmp/$(id -u) || sleep 5 && pkill -9 waitpid' EXIT TERM
	
	exec sh -c '
		trap "s6-rc -l /tmp/$(id -u)/s6-rc -D change default && rm -rf /tmp/$(id -u) || sleep 5 && pkill -9 waitpid" EXIT TERM
		mkdir -p "/tmp/$(id -u)/service"
		[ ! -e "/tmp/$(id -u)/service/.s6-svscan" ] && bash -c "coproc s6-svscan /tmp/$(id -u)/service"
		[ ! -e "/tmp/$(id -u)/s6-rc" ] && s6-rc-init -c "$(/usr/lib/execline/bin/homeof $(whoami))/.s6-rc/compiled" -l "/tmp/$(id -u)/s6-rc" "/tmp/$(id -u)/service"
		s6-rc -l "/tmp/$(id -u)/s6-rc" -up change default
		'
done & 
