#!/bin/sh
# Initial script for Linux Live Kit
# Author: Tomas M <http://www.linux-live.org/>

#exec 2>/var/log/init.trace
#PS4='${LINENO}: '
#set -xv

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

LIVEKITNAME="minios"
BEXT="sb"

. /lib/livekitlib

transfer_initramfs

MEMORY=/memory
CHANGES=$MEMORY/changes
UNION=$MEMORY/union
DATAMNT=$MEMORY/data
BUNDLES=$MEMORY/bundles

header -t "MiniOS" -s "Fast. Simple. Reliable."
header -u "https://minios.dev"

init_proc_sysfs

debug_start
                                                                                                                                                      debug_shell
# load some modules manually first, then modprobe everything we have
init_devs
init_union_modules
if [ "$KERNEL_TYPE" != "cloud" ]; then
    init_zram
fi

# modprobe all devices excluding network drivers
modprobe_everything -v /drivers/net/

# initialize blkid cache file
init_blkid_cache

# find data dir with filesystem bundles
DATA="$(find_data 45 "$DATAMNT")"
check_data_found "$DATA"
                                                                                                                                                      debug_shell
# copy to RAM if needed
DATA="$(copy_to_ram "$DATA" "$CHANGES")"
                                                                                                                                                      debug_shell
# setup persistent changes, if possible
persistent_changes "$DATA" "$CHANGES"
                                                                                                                                                      debug_shell
# setup running kernel files
setup_running_kernel "$DATA"
                                                                                                                                                      debug_shell
# mount bundles
mount_bundles "$DATA" "$BUNDLES"
                                                                                                                                                      debug_shell
# init aufs union, or setup overlayfs union
init_union "$CHANGES" "$UNION" "$BUNDLES"

# add data to aufs union
union_append_bundles "$BUNDLES" "$UNION"
                                                                                                                                                      debug_shell
# rootcopy
copy_rootcopy_content "$DATA" "$UNION"

# MiniOS setup
minios_boot "$DATA" "$UNION"
                                                                                                                                                      debug_shell
# create fstab
fstab_create "$UNION" "$DATAMNT"
                                                                                                                                                      debug_shell
# run user custom preinit
user_preinit "$DATA" "$UNION"

header -t "Live Kit done, starting MiniOS"

# show final timing before starting MiniOS
if [ "$TIMING_ENABLED" ] && [ -r /proc/uptime ]; then
    FINAL_TIME=$(awk '{print $1}' /proc/uptime)
    if [ -n "$TIMING_LAST_TIME" ]; then
        TOTAL_TIME=$(awk "BEGIN {printf \"%.2f\", $FINAL_TIME}")
        echo "  initramfs complete: ${TOTAL_TIME}s total" >&2
    fi
fi

change_root "$UNION"

header -t "!!ERROR occured, you shouldn't be here!!"
/bin/sh
