#!/bin/bash

# Default architecture
DEFAULT_ARCH="linux/amd64"

# Supported architectures (adjust as needed)
ARCHITECTURES=("linux/amd64" "linux/arm64" "linux/arm/v7")

# Find the appropriate directory containing Go code (assumes 'src' or project root)
find_go_directory() {
  if [ -d "src" ] && [ -f "src/go.mod" ]; then
    echo "src"  # Source code is within 'src'
  else
    echo "."  # Source code is in the root directory
  fi
}

# Build function 
build_binary() {
    os=$1
    arch=$2
    output_name="host_check"
    go_dir=$(find_go_directory) 

    if [[ "$os/$arch" != "$DEFAULT_ARCH" ]]; then
        output_name="${output_name}_${os}_${arch}"
    fi

    mkdir -p dist

    echo "Building for ${os}/${arch} -> dist/${output_name}"
    GOOS=${os} GOARCH=${arch} go build -v -o dist/${output_name} $go_dir
}

# Main Build Process
# Initialize Go module if needed
if [ ! -f "go.mod" ]; then
    echo "Initializing Go module..."
    go mod init git.nixc.us/Nixius/host_check 
    go mod tidy
fi

for arch in "${ARCHITECTURES[@]}"; do
    IFS='/' read -r -a parts <<< "$arch" 
    os=${parts[0]}
    arch=${parts[1]}

    build_binary $os $arch
done

