#!/bin/sh
set -e
MSG_NO_BLINK_EXEC='/bin/blink unavailable or not executable on image'
MSG_BLINK_FAILED='/bin/blink returned a non-zero error code'
MSG_WALT_REBOOT_NO_EXEC='/bin/walt-reboot is not executable on image'

final_root="$1"

# Uncomment this for debug log in /persist
#set -x
#exec >> "$final_root/persist/walt-net-service.log"
#exec 2>&1

on_exit()
{
    echo "[bg] walt-net-service exited! What happened?? Let's reboot!"
    sleep 5
    reboot -f
}

trap on_exit EXIT
echo "[bg] walt-net-service started."

# create fifos to communicate with the nc process
fifo_in=$(mktemp -u); mkfifo $fifo_in
fifo_out=$(mktemp -u); mkfifo $fifo_out

# start the nc process in the background
# to communicate with the server
while [ 1 ]
do
    busybox nc -l -p %(walt_node_net_service_port)s
done < $fifo_out > $fifo_in &

# pipe fd 5 to standard output of nc (we will read
# requests from the server there)
# pipe fd 6 to standard input of nc (we will send
# results there)
exec 6>$fifo_out 5<$fifo_in

do_walt_reboot() {
    cd "$final_root"
    echo "Calling bin/walt-reboot"
    chroot . bin/walt-reboot
    # if we are here, this means bin/walt-reboot failed
    # to reboot the OS.
    echo -n "Command 'bin/walt-reboot' failed. "
    echo "Falling back to standard 'reboot -f'."
    cd /
    reboot -f
}

# Caution with optional files /bin/walt-reboot & /bin/blink:
# Tests such as
# [ -e "$final_root/bin/walt-reboot" ]
# will not work when these files are symlinks to an
# absolute target, since the target path should also be
# interpreted from the chroot.
# Therefore
# chroot "$final_root" [ -e "/bin/walt-reboot" ]
# is the correct expression.

# loop and handle coming requests
while read req args <&5
do
    # respond to server if it checks whether we are alive
    [ "$req" = "PING" ] && {
        echo OK >&6
    }
    # respond to DMESG requests
    [ "$req" = "DMESG" ] && {
        echo OK >&6
        echo >&6
        busybox dmesg >&6
    }
    # respond to SHELL requests
    [ "$req" = "SHELL" ] && {
        echo OK >&6
        echo >&6
        busybox sh -i <&5 >&6 2>&1
    }
    # reboot the node when we receive REBOOT
    [ "$req" = "REBOOT" ] && {
        chroot "$final_root" [ -e "/bin/walt-reboot" ] && {
            chroot "$final_root" [ -x "/bin/walt-reboot" ] && {
                echo OK >&6
                do_walt_reboot
            } || {
                echo KO $MSG_WALT_REBOOT_NO_EXEC >&6
            }
        } || {
            echo OK >&6
            reboot -f
        }
    }
    # start /bin/blink [0|1] when we receive BLINK [0|1]
    [ "$req" = "BLINK" ] && {
        chroot "$final_root" [ -x "/bin/blink" ] && {
            cd "$final_root"
            chroot . bin/blink $args && {
                # all is fine
                echo OK >&6
            } || {
                # /bin/blink apparently failed
                echo KO $MSG_BLINK_FAILED >&6
            }
            cd -
        } || {
            # no /bin/blink on this image
            echo KO $MSG_NO_BLINK_EXEC >&6
        }
    }
done

