#!/bin/sh
# Copyright (c) 2015, 2016, 2017, 2023 Jonas 'Sortie' Termansen.
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# update-initrd
# Generate an initrd(7) that locates and chain boots the real root filesystem.

# This script will run on the previous stable release during a sysmerge(8).

set -e

sysroot=
sysmerge=false

dashdash=
previous_option=
for argument do
  if test -n "$previous_option"; then
    eval $previous_option=\$argument
    previous_option=
    continue
  fi
  case $argument in
  *=?*) parameter=$(expr "X$argument" : '[^=]*=\(.*\)' || true) ;;
  *=)   parameter= ;;
  *)    parameter=yes ;;
  esac
  case $dashdash$argument in
  --) dashdash=yes ;;
  --sysmerge) sysmerge=true ;;
  --sysroot=*) sysroot=$parameter ;;
  --sysroot) previous_option=sysroot ;;
  -*) echo "$0: unrecognized option $argument" >&2
      $option_checking && exit 1 ;;
  *) echo "$0: unexpected operand $argument" >&2
     exit 1 ;;
  esac
done

if test -n "$previous_option"; then
  echo "$0: option '$argument' requires an argument" >&2
  exit 1
fi

root="$sysroot"
output="$sysroot"
if $sysmerge; then
  root="$sysroot/sysmerge"
  output="$root"
# TODO: After releasing Sortix 1.1, remove the check for /sysmerge as the old
#       update-initrd will invoke the newer version and expect the new version
#       to notice that it is the version in /sysmerge. However, write the new
#       initrd to /boot rather than /sysmerge/boot since 1.0 doesn't implement
#       the new scheme.
elif [ -e /sysmerge -a "$0" = /sysmerge/sbin/update-initrd ]; then
  sysmerge=true
  root="$sysroot/sysmerge"
  output="$sysroot"
fi
if [ ! -e "$sysroot/etc/fstab" ]; then
  echo "$0: $sysroot/etc/fstab: Need a filesystem table to make an initrd" >&2
  exit 1
fi
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT HUP INT QUIT TERM
mkdir "$tmp/bin"
mkdir "$tmp/sbin"
cp "$root/sbin/init" "$tmp/sbin"
cp "$root/sbin/extfs" "$tmp/sbin"
test -f "$root/sbin/fsck.ext2" &&
cp "$root/sbin/fsck.ext2" "$tmp/sbin"
mkdir "$tmp/etc"
cp "$sysroot/etc/fstab" "$tmp/etc/fstab"
mkdir "$tmp/etc/init"
if $sysmerge; then
  cat > "$tmp/etc/init/default" << EOF
require chain-merge exit-code
EOF
else
  cat > "$tmp/etc/init/default" << EOF
require chain exit-code
EOF
fi
mkdir -p "$output/boot"
LC_AL=C ls -A "$tmp" |
tar -C "$tmp" -cf "$output/boot/sortix.initrd" \
    --numeric-owner --owner=0 --group=0 -T -
