#!/bin/bash

set -e

# Test
if [ "$1" = "test" ]; then
    # Install native dependencies
    apt update
    apt install -y npm

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

    # Generate dependencies
    make "depend/${2}"

    # Run tests
    make "test/${2}"

    exit 0
fi

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

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

    # 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

    # Generate dependencies
    make "depend/${2}"

    # Build
    CGO_ENABLED=0 bagop -j "$(nproc)" -b "$(basename ${2})" -x '(android/*|ios/*|plan9/*|aix/*|linux/loong64|freebsd/riscv64|wasip1/wasm|js/wasm|openbsd/mips64)' -p "make build/${2} DST=\$DST" -d out

    exit 0
fi
