#!/bin/bash

# This entry point gets invoked invoked for systemd based
# bootstraps.
# It will load any custom prep for the repo in question, as
# defined in repos/name/init.sh then continue with the common
# bootstrap script

# ensure all script output is logged - we ensure that ttyS1 exists first
mknod /dev/ttyS1 c 4 65
exec > /dev/ttyS1 2>&1

set -x

# path consistency
export PATH=$PATH:/sbin

# load the customizations
echo "######## Loading repo customizations ########"
. /bin/repoinit
echo "######## Done repo customizations ######## -- exit $?"

mount -t proc none /proc
mount -t sysfs none /sys
mount -t devtmpfs none /dev

# make some of the basics and start udev
mknod -m 622 /dev/console c 5 1
mknod -m 666 /dev/null c 1 3
mknod -m 666 /dev/zero c 1 5
mknod -m 666 /dev/ptmx c 5 2
mknod -m 666 /dev/tty c 5 0
mknod -m 444 /dev/random c 1 8
mknod -m 444 /dev/urandom c 1 9
chown root:tty /dev/{console,ptmx,tty}

mkdir -p /dev/{pts,shm}
# attempt with ptmxmode first
mount -t devpts -o gid=5,mode=620,ptmxmode=666 none /dev/pts || mount -t devpts -o gid=5,mode=620 none /dev/pts
mount -t tmpfs none /dev/shm

# don't use `--daemon` option so that we can easily get the pid to kill it later
/usr/lib/systemd/systemd-udevd &
UDEV_PID=$!

udevadm trigger

echo 'Bring up loopback interface'
until ip link show lo;do sleep 0.1;done
ip link set lo up

# launch the main entry point
echo "######## Launching bootstrap entrypoint ########"
. /bin/bootstrap