#!/bin/sh -e

init_conf(){
    while read -r f; do
        if [ "$(basename "$f")" = "s6-init" ]; then
            s6_init_remake
        fi
        ln -sf "$(basename "$f")" "/usr/bin/init"
        echo "	==> Activated $(basename "$f")"
    done
}

init_detect(){
    artix_inits="openrc-init runit-init s6-init dinit-init"
    for supported in ${artix_inits}; do
        bin=/usr/bin/"$supported"
        if [ -f "$bin" ];then
            init=$(basename "$bin")
        fi
    done

    if [ -n "$init" ];then
        echo "	==> Found $init"
        if [ "$init" = "s6-init" ]; then
            s6_init_remake
        fi
        ln -sf "$init" "/usr/bin/init"
    else
        echo "	==> No supported init found."
    fi
}

s6_init_remake(){
    # we need to ensure that s6-linux-init-maker is run on every machine
    rm -rf /tmp/current
    s6-linux-init-maker -1 -G "/usr/bin/agetty -L -8 tty12 115200" -c /etc/s6/current /tmp/current
    mv /tmp/current/bin/init /tmp/current/bin/s6-init
    cp -a /tmp/current/bin /usr
    rm -rf /tmp/current/{bin,scripts}
    cp -a /tmp/current /etc/s6
}

case "$1" in
    update) init_conf ;;
    detect) init_detect ;;
    *) echo >&2 "  Invalid operation '$1'"; exit 1 ;;
esac
