#!/bin/sh
# Copyright 2016, 2017 Cumulus Networks, Inc.  All rights reserved

export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/cumulus/bin

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

# We only run once.  Disable ourselves as our very first action, so
# if something goes wrong, we won't run again.
/bin/systemctl --no-block disable cumulus-firstboot.service

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

# dnsmasq is enabled by default when the package is installed.  We install
# dnsmasq but do not want it enabled unless actually configured and in use,
# so disable it on a new image install.  Customer must enable when configured.
# stop because systemd already has it queued to start
/bin/systemctl --no-block stop dnsmasq > /dev/null
/bin/systemctl --no-block disable dnsmasq > /dev/null

# By default most platforms do not have a graphics interface, so
# disable virtual ttys.
enable_vtty=no

# See if the system has at least one PCI device of class 0x0300 (VGA
# compatible controller).
has_vga=$(lspci -nm|awk '{ if ($2 == "\"0300\"") print "yes"; }'|head -n 1)
if [ "$has_vga" = "yes" ] ; then
    enable_vtty=yes
fi

if [ "$enable_vtty" = "no" ] ; then
    /bin/systemctl --no-block disable getty@tty1.service > /dev/null
fi

platform=$(/usr/bin/platform-detect)
lfile=/etc/cumulus/.license.rmp
[ $vx -eq 1 ] && lfile=/etc/cumulus/.license.vx
case "${platform}" in
cel,pebble*|cumulus,vx|quanta,ly4r)
    if [ ! -f ${lfile} ]; then
        cat > ${lfile} << \EOF
This file satisfies the conditions for switchd.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

# We don't want vga/graphics login to run, except on VX, because there
# is no hardware.  systemd has already scheduled it to run; the daemon-reload
# doesn't prevent that.
# We would prefer that the Stop messages not show up during first boot,
# but there really isn't a way to avoid that, since systemd prints them.
if [ "$enable_vtty" = "no" ] ; then
    /bin/systemctl --no-block stop getty@tty1.service
fi

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

#
# Run all of the scripts in /usr/lib/cumulus/firstboot.d/*.firstboot
#
FIRSTBOOT_DIR="/usr/lib/cumulus/firstboot.d"
[ -d "${FIRSTBOOT_DIR}" ] && {
    scripts=$(/bin/ls ${FIRSTBOOT_DIR}/*.firstboot 2>/dev/null) && {
        for s in ${scripts} ; do
            ${s}
        done
    }
}

exit 0
