#!/bin/sh

# Copyright 2021 Guilhem Moulin <guilhem@debian.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

PREREQ=""

prereqs() {
    echo "$PREREQ"
}

case "$1" in
    prereqs)
        prereqs
        exit 0
    ;;
esac

. /usr/share/initramfs-tools/hook-functions
set -eu

copy_exec /sbin/sfdisk

# we'd like to run before busybox to ship GNU tar and mke2fs(8) from
# e2fsprogs instead, but we can't express reverse dependencies and
# copy_exe() is a no-op when the target exists, so we remove any
# pre-existing binaries from $DESTDIR before calling copy_exec() again
for exe in /bin/tar /sbin/mke2fs; do
    rm -f -- "$DESTDIR$exe" "$DESTDIR/usr$exe"
    copy_exec "$exe"
done

# copy the key file used to format and unlock the root FS so we can call
# `cryptsetup luksFormat` and `cryptsetup luksOpen` from the initrd
install -Dm0600 -- "$AUTOPKGTEST_TMP/rootfs.key" "$DESTDIR/cryptroot/rootfs.key"

exit 0
