#!/bin/bash

set -e

# Test
if [ "$1" = "test" ]; then
  # Configure Git
  git config --global --add safe.directory '*'

  # Generate dependencies
  make depend

  # Run tests
  make test

  exit 0
fi

# Go
if [ "$1" = "go" ]; then
  # Install native dependencies
  apt update
  apt install -y curl make

  # Install bagop
  curl -L -o /tmp/bagop "https://github.com/pojntfx/bagop/releases/latest/download/bagop.linux-$(uname -m)"
  install /tmp/bagop /usr/local/bin

  # Configure Git
  git config --global --add safe.directory '*'

  # Generate dependencies
  make depend

  # Build
  CGO_ENABLED=0 bagop -j "$(nproc)" -b "$2" -x '(aix/*|android/*|darwin/*|dragonfly/*|freebsd/*|illumos/*|ios/*|js/*|linux/386|linux/arm|linux/arm64|linux/loong64|linux/mips|linux/mips64|linux/mips64le|linux/mipsle|linux/ppc64|linux/ppc64le|linux/riscv64|linux/s390x|netbsd/*|openbsd/*|plan9/*|solaris/*|windows/*|wasip1/wasm)' -p "make build/$2 DST=\$DST" -d out

  exit 0
fi