#!/bin/zsh
#MISE description="compile and package local release build"
#USAGE flag "--static" help="build static release"
#USAGE flag "-t --target <target>" help="build for <target>"

NAME=copytree
BUILD_DIR=target/release

typeset -a build_args=()
if [[ $usage_target ]]; then
    TARGET="$usage_target"
    build_args+=(--target "$usage_target")
    BUILD_DIR="target/$TARGET/release"
else
    TARGET="$(rustc -vV | sed -n 's/host:[[:space:]]*//p')"
fi

print -P "%Bbuilding for %F{cyan}$TARGET%f%b"
if [[ $OSTYPE == darwin* ]]; then
    export MACOSX_DEPLOYMENT_TARGET=10.12
    print "MacOS target version: $MACOSX_DEPLOYMENT_TARGET"
fi

if [[ $usage_static ]]; then
    print -P "%Benabling static build%b"
    export RUSTFLAGS='-C target-feature=+crt-static'
fi

print -P "%Bcompiling release build%b"
cargo build --release "${build_args[@]}"

print -P "%Bbundling binary%b"
tar czf target/copytree-$TARGET.tgz -C $BUILD_DIR copytree
print -P "%Bcreated %F{yellow}copytree-$TARGET.tgz%f%b"
