#!/bin/sh

# Fixups needed first time a system is booted.
# This runs early in boot once after an image is installed, as soon as
# possible after the root filesystem is mounted read/write.  The
# cumulus-firstboot.service disables itself the first time it runs

vx=0
# Determine if we are on VX platform or not.  If so, leave tty1 enabled
if [ -e /etc/cumulus/switchd.env ]; then
    . /etc/cumulus/switchd.env
    [ "$VX" = "-vx" ] && vx=1
fi

# Also see the stop commands at the end of the script.
# > /dev/null to hide the unlink and symlink messages
# prevent the debug filesysem from being mounted during bootup
/bin/systemctl --no-block mask sys-kernel-debug.mount > /dev/null

# No graphics interface, so no virtual tty
[ $vx -eq 0 ] &&
    /bin/systemctl --no-block disable getty@tty1.service > /dev/null

platform=$(/usr/bin/platform-detect)
lfile=/etc/cumulus/.license.rmp
case "${platform}" in
cel,pebble )
    if [ ! -f ${lfile} ]; then
        cat > ${lfile} << \EOF
This file satisfies the conditions for systemd.service startup for switches
with builtin licenses.
If it is removed, switchd will not be able to be started.
EOF
        chmod 444 ${lfile}
    fi
esac

# some systems with celeron-based cpu's have multiple watchdogs
# For those, we select the iTCO watchdog, and then edit watchdog.conf
# to use that watchdog device (if it still points to /dev/watchdog).
# wd_identify won't work with watchdog in use, so stop if necessary.
multw=$(/bin/ls /dev/watchdog[0-9]* 2>/dev/null)
case "${multw}" in
*watchdog1*) # more than one watchdog
    for w in ${multw}; do
	wrel=${w##*/}
	echo watchdog-device = $w > /tmp/watch-${wrel}
	type=$(wd_identify -c /tmp/watch-${wrel})
	case "${type}" in
	iTCO_wdt)
	    sed -i 's,/dev/watchdog$,'$w, /etc/watchdog.conf
	    break;
            ;;
	esac
    done
    ;;
esac

/bin/systemctl daemon-reload # so getty won't run this boot

# We don't want them to run on this boot, and systemd has already
# scheduled them to run; the daemon-reload doesn't prevent that.
# We don't want the Stop messages showing up during first boot,
# but there really isn't a way to avoid that, since systemd prints
# them.
/bin/systemctl --no-block stop sys-kernel-debug.mount
[ $vx -eq 0 ] &&
    /bin/systemctl --no-block stop getty@tty1.service

exit 0
