#!/bin/bash
# Build the SBo submission tarball SBo/<pkg>.tar.gz for one package.
# Same archive the post-commit hook produces, but on demand by name.
set -eu

usage() { echo "usage: ${0##*/} <package-name>" >&2; exit 2; }
[ $# -eq 1 ] || usage

pkg=$1
REPO_ROOT=$(git -C "$(dirname "$0")" rev-parse --show-toplevel)

# Where the tarball lands. Defaults to the current directory; override with
# the OUTPUT env var (e.g. OUTPUT=/tmp mksboarchive feroxbuster).
OUTPUT=${OUTPUT:-$PWD}

# Must be a real package dir one level deep with a matching .SlackBuild.
if [ ! -f "$REPO_ROOT/$pkg/$pkg.SlackBuild" ]; then
    echo "error: '$pkg' is not a package in this repo." >&2
    exit 1
fi

# Drop any sbodl source symlinks before archiving so they never end up in an
# SBo submission tarball (the pre-commit hook does the same for staged files).
find "$REPO_ROOT/$pkg" -type l -delete

mkdir -p "$OUTPUT"
tar -czf "$OUTPUT/$pkg.tar.gz" -C "$REPO_ROOT" "$pkg"
echo "Archive created: $OUTPUT/$pkg.tar.gz"
