#!/bin/zsh
#MISE description="Compile release binary and archive."
#USAGE flag "--static" help="build static release"
#USAGE flag "-p --podman" help="use podman instead of docker"
#USAGE flag "-A --arch <arch>" help="build for <arch>"
#USAGE arg "<dist>" help="use container <dist>"

set -eo pipefail

NAME=copytree

: ${DOCKER:=docker}

typeset -a rb_args=() db_args=() dr_args=()
if [[ $usage_static ]]; then
    rb_args+='--static'
fi
if [[ $usage_podman ]]; then
    DOCKER=podman
fi
out_name="$usage_dist"
if [[ $usage_arch ]]; then
    db_args+=(--platform linux/$usage_arch)
    dr_args+=(--platform linux/$usage_arch)
    out_name="$out_name-$usage_arch"
fi
img_name="$NAME-build:$out_name"

echo "building with Docker container $usage_dist"
mkdir -p "target/$out_name"
docker build -t "$img_name" -f containers/Dockerfile.$usage_dist "${db_args[@]}" .
docker run --rm -v "$PWD/target/$out_name:/src/$NAME/target" "${dr_args[@]}" "$img_name" build:local-release "${rb_args[@]}"
cp "target/$out_name/$NAME"-*.tgz target
