#!/usr/bin/env bash

set -eux -o pipefail

SOURCE="$(realpath $(dirname "$0")/..)"

mkdir -p \
    ${SOURCE}/dist \
    ${SOURCE}/artifacts/"${BUILDARCH}"/{bin,etc,xtables-bin} \
    ${SOURCE}/artifacts/"${BUILDARCH}"/bin/aux

pushd /usr/src

# copy binaries
cp -d buildroot/output/target/usr/sbin/*tables*                  "${SOURCE}/artifacts/${BUILDARCH}/bin/"
cp buildroot/output/target/usr/sbin/{conntrack,ethtool,ipset}    "${SOURCE}/artifacts/${BUILDARCH}/bin/"
cp buildroot/output/target/usr/bin/{coreutils,find,fuse-overlayfs,nsenter,pigz,slirp4netns,xargs} "${SOURCE}/artifacts/${BUILDARCH}/bin/"
cp buildroot/output/target/sbin/{blkid,ip,losetup}               "${SOURCE}/artifacts/${BUILDARCH}/bin/"
cp buildroot/output/target/bin/busybox                           "${SOURCE}/artifacts/${BUILDARCH}/bin/"
cp buildroot/output/target/usr/sbin/nft                          "${SOURCE}/artifacts/${BUILDARCH}/bin/aux/"

cp -r ${SOURCE}/iptables-detect/*.sh "${SOURCE}/artifacts/${BUILDARCH}/xtables-bin/"

# setup links
link() { \
    for l in $(find -L buildroot/output/target/ -samefile buildroot/output/target/$1/$2 | xargs -n 1 basename | sort -u | grep -v "^$2\$"); do \
        ln -s "$2" "${SOURCE}/artifacts/${BUILDARCH}/bin/$l"; \
    done; \
};

link bin busybox;
link usr/bin coreutils;

ln -sf pigz "${SOURCE}/artifacts/${BUILDARCH}/bin/unpigz"
rm -f "${SOURCE}/artifacts/${BUILDARCH}/bin/mount"
rm -f "${SOURCE}/artifacts/${BUILDARCH}/bin/modprobe"

ln -sf ../busybox "${SOURCE}/artifacts/${BUILDARCH}/bin/aux/mount"
ln -sf ../busybox "${SOURCE}/artifacts/${BUILDARCH}/bin/aux/modprobe"

mv ${SOURCE}/artifacts/"${BUILDARCH}"/bin/*tables* "${SOURCE}/artifacts/${BUILDARCH}/xtables-bin/"
for target in iptables iptables-save iptables-restore ip6tables ip6tables-save ip6tables-restore; do
    ln -sf iptables-detect.sh "${SOURCE}/artifacts/${BUILDARCH}/xtables-bin/$target"
done
cp -rp ${SOURCE}/artifacts/"${BUILDARCH}"/xtables-bin/* "${SOURCE}/artifacts/${BUILDARCH}/bin/aux"

tar cf - -C "${SOURCE}/artifacts/${BUILDARCH}" ./bin ./etc > "${SOURCE}/dist/k3s-root-${BUILDARCH}.tar"
tar cf - -C "${SOURCE}/artifacts/${BUILDARCH}" --transform s/xtables-bin/bin/ ./xtables-bin > "${SOURCE}/dist/k3s-root-xtables-${BUILDARCH}.tar"
tar -vtf "${SOURCE}/dist/k3s-root-${BUILDARCH}.tar"

popd
