#! /bin/sh
# postinst script for s6-66-init

set -e
_PATH_="/tmp/build"
SERVICE_SYSADM="/etc/66/service"
SEED_SYSADM="/etc/66/seed"
oblibs_tag="0.2.1.0"
ss_tag="0.7.0.0-preprod"
ss_tools_tag="0.1.0.1"
boot_66serv_tag="3.0.0"

if [ "$(id -u)" -gt 0 ]; then
    echo "Please run as root"
    exit 1
fi
auto_dir(){
    p="${1}"
    mkdir -p ${p} 1>/dev/null 2>&1 || exit 1
    unset p
}

echo "creating new 66 executables"

echo "#!/usr/lib/execline/bin/execlineb -P
66 boot -m -e /etc/66/environment
" > /etc/66/s6-66-init/current/bin/init
chmod +x /etc/66/s6-66-init/current/bin/init

echo "#!/usr/lib/execline/bin/execlineb -P
66 halt
" > /etc/66/s6-66-init/current/bin/halt
chmod +x /etc/66/s6-66-init/current/bin/halt

echo "#!/usr/lib/execline/bin/execlineb -P
66 reboot
" > /etc/66/s6-66-init/current/bin/reboot
chmod +x /etc/66/s6-66-init/current/bin/reboot

echo "#!/usr/lib/execline/bin/execlineb -P
66 poweroff
" > /etc/66/s6-66-init/current/bin/poweroff
chmod +x /etc/66/s6-66-init/current/bin/poweroff

echo "enabling s6-66-init"
echo "copying s6-rc-init files to /lib/s6-66 folder"
	yes | cp -rf /etc/66/s6-66-init/current/bin/init /lib/s6-66/init
	yes | cp -rf /etc/66/s6-66-init/current/bin/halt /lib/s6-66/halt
	yes | cp -rf /etc/66/s6-66-init/current/bin/reboot /lib/s6-66/reboot
	yes | cp -rf /etc/66/s6-66-init/current/bin/poweroff /lib/s6-66/poweroff
	
echo "updating grub"
	update-grub

echo "s6-rc-init has been installed"

echo "66 has been compiled and installed"
