#!/usr/bin/bash

# Copy Autobuild4 templates to $PWD/autobuild and restart Autobuild4
# in-place.
#
# Kernel Autobuild sources should only have `defines', `build', a `patches'
# directory, and per-arch kernel configs in their respective folders. No
# other files should be shipped.
# Notably, the directory should not contain `postinst.in', `prerm.in' or
# `prepare'. These files will be overwritten.
#
# linux-kernel-something
#   \- <dir> autobuild
#       |- defines
#       |- build            READ NOTES
#       |- <dir> patches
#       |   |- 0001-some-bug.patch
#       |   |- .....
#       |   \- XXXX-shame-on-nvidia.patch
#       |- <dir> amd64
#       |   \- config
#       |- <dir> arm64
#       |   \- config
#       |- <dir> riscv64
#       |   \- config
#       .....
#
# The `defines` file should contain their normal contents.
# The `build` script should only SOURCE this script, and do nothing else.
#

if [[ -z "$AB" ]]; then
    echo "This script must be sourced from an Autobuild build script."
    exit 1
fi

if [[ ! -d "./autobuild" ]]; then
    aberr "No autobuild directory! Cannot proceed!"
    exit 1
fi

if [[ ! -f "./autobuild/defines" ]]; then
    aberr "No autobuild/defines! Cannot proceed!"
    exit 1
fi

INSTANCE_DIR="$PWD/autobuild"
TEMPLATE_DIR="/usr/share/autobuild4/linux-kernel/"

abinfo "Overwriting build directory..."
rsync -av "$TEMPLATE_DIR/" "$INSTANCE_DIR/"
RET=$?
if (($RET)); then
    aberr "Could not copy build files to $INSTANCE_DIR"
    exit $RET
fi

unset AB
exec /usr/lib/autobuild4/autobuild

# vim: set sts=4 ts=4 sw=4 expandtab :
