#!/bin/bash
# Executed by init script

TERM=${TERM:-linux}

INITHOOKS_DEFAULT=/etc/default/inithooks
. $INITHOOKS_DEFAULT

TKLINFO=/var/lib/turnkey-info

unset PID INITHOOKS_LOGFILE

REDIRECT_OUTPUT=$(echo $REDIRECT_OUTPUT | tr [A-Z] [a-z])

log() {
    # log to journal as well as $INITHOOKS_LOGFILE
    LEVEL=$1 # err|warn|info|debug
    shift
    logger -t inithooks -p $LEVEL "$@"
    [[ -n "$INITHOOKS_LOGFILE" ]] \
        && echo "${LEVEL^^}: $@" >> "$INITHOOKS_LOGFILE"
}

if [[ "$REDIRECT_OUTPUT" == "true" ]]; then
    # redirect stdout/stderr (use when preseeding headless deployments)
    export INITHOOKS_LOGFILE=/var/log/inithooks.log
    touch $INITHOOKS_LOGFILE
    chmod 640 $INITHOOKS_LOGFILE

    # on xen redirection is performed by the inithooks-xen service
    # on lxc and other headless deployments, redirection is handled below
    # otherwise redirection is handled by inithooks service and redirected to
    # tty8

    if [[ ! -f "$TKLINFO/xen" ]]; then
        TTY=$(cat /sys/devices/virtual/tty/tty0/active)
        [[ -z $TTY ]] && TTY=console
        tail -f $INITHOOKS_LOGFILE > /dev/$TTY &
        PID="$!"
    fi
fi

exec_scripts() {
    script_dir=$1
    [[ -d "$script_dir" ]] || return 0
    for SCRIPT in $(find $script_dir -type f -or -type l | sort); do
        [[ -e $INITHOOKS_CONF ]] && . $INITHOOKS_CONF
        script=$(basename $SCRIPT)
        if [[ ! -x "$SCRIPT" ]]; then
            log warn "[$script] skipping"
            continue
        fi
        log info "[$script] running"
        "$SCRIPT"
        exit_code=$?
        if [[ "$exit_code" -eq 0 ]]; then
            log info "[$script] successfully completed"
        elif [[ "$script" = "95secupdates" ]] && [[ "$exit_code" -eq 2 ]]; then
            log info "[$script] detected live system - skipping"
        elif [[ "$script" = "95secupdates" ]] && [[ "$exit_code" -eq 42 ]]; then
            log warn "[$script] reboot is required"
            log err 'Rebooting now!'
            systemctl reboot
            exit 0
        else
            log err "[$script] failed - exit code $exit_code"
        fi
    done
    return 0
}

[[ -e $INITHOOKS_CONF ]] && . $INITHOOKS_CONF
export INITHOOKS_CONF=$INITHOOKS_CONF

if [[ "${RUN_FIRSTBOOT,,}" == "true" ]]; then
    exec_scripts $INITHOOKS_PATH/firstboot.d
fi
exec_scripts $INITHOOKS_PATH/everyboot.d

if [[ -n "$PID" ]];  then
    kill -9 $PID || true
fi

if [[ "${REDIRECT_OUTPUT,,}" == "true" ]]; then
    log info "Inithook run completed, exiting."
else
    log info "Inithook run completed, now starting confconsole"
    sleep 2 # anyway to replace this?
    confconsole --usage
    log info "Confconsole started, inithooks exiting"
fi

exit 0
