#!/bin/sh

# SPDX-License-Identifier: MPL-2.0

# The initramfs entry point (i.e., the init process).
#
# This sets up the basic environment and
# transfers control by executing the command-line arguments of the script,
# which typically either provide a shell for user interaction or
# run tests automatically.
#
# TODO: This script simulates the process of mounting filesystems
# as performed by a generic init process.
# It should later be replaced by the actual init process.

if [ "$#" -eq 0 ]; then
    echo "No arguments were provided to the init process. Spawn a shell by default."
    echo "Usage: $0 <prog> [args...]"
    set -- /bin/sh
fi

mount -t sysfs none /sys
mount -t proc none /proc
mount -t cgroup2 none /sys/fs/cgroup
mount -t configfs none /sys/kernel/config
mount -t ext2 /dev/vda /ext2
mount -t exfat /dev/vdb /exfat

# When the init process starts,
# it does not have a process group, session, or controlling terminal.
# The `script` tool is a standard Linux utility
# that captures everything displayed on your terminal screen and records it to a file.
# It tricks a program into thinking it is running in an interactive terminal.
#
# TODO: Use `cttyhack` once we support it, which is a better choice.
script /dev/null -q -c "$*"

poweroff -f
