#!/usr/bin/env bash

# Define target OS/arch combinations to build
BUILD_TARGETS=(
  "linux/amd64"
  "linux/arm"
  "linux/arm64"
  "linux/riscv64"
  "windows/amd64"
  "windows/arm64"
  "darwin/amd64"
  "darwin/arm64"
)

# Define targets that should be compressed with UPX
COMPRESS_TARGETS=(
  "linux/amd64"
  "linux/arm"
  "linux/arm64"
  "windows/amd64"
)

go-build(){
  CGO_ENABLED=0 \
    GOOS=$GOOS \
    GOARCH=$GOARCH \
    go build \
    -o _dist/drycc-$1-$GOOS-$GOARCH \
    drycc.go
  # Check if current target is in COMPRESS_TARGETS list
  target="$GOOS/$GOARCH"
  if [[ ${COMPRESS_TARGETS[@]/$target/} != ${COMPRESS_TARGETS[@]} ]]; then
    upx --lzma --best _dist/drycc-$1-$GOOS-$GOARCH
  fi
}

# Process each target
for target in "${BUILD_TARGETS[@]}"; do
  # Split target into OS and architecture
  GOOS=${target%/*}
  GOARCH=${target#*/}
  go-build "$1"
done
