#!/usr/bin/env bash
set -e

[[ -z "$TARGET" ]] && echo "Please set the TARGET variable" && exit 1

if [ "$(id -u)" == "0" ]; then
  [[ -z "$TO_UID" ]] && echo "Please set the TO_UID variable" && exit 1
fi

cd "$(dirname "$0")"

cd secp256k1

if [ "$TARGET" == "mingw" ]; then
  CFLAGS="-fPIC"
  CONF_OPTS=" --host=x86_64-w64-mingw32"
elif [ "$TARGET" == "linux" ]; then
  CFLAGS="-fPIC"
elif [ "$TARGET" == "linuxArm64" ]; then
  CFLAGS="-fPIC"
  CONF_OPTS="--host=aarch64-linux-gnu"
elif [ "$TARGET" == "darwin" ]; then
  CFLAGS="-arch arm64 -arch x86_64"
  LDFLAGS="-arch arm64 -arch x86_64"
else
  echo "Unknown TARGET=$TARGET"
  exit 1
fi

./autogen.sh
CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS" ./configure $CONF_OPTS --enable-experimental --enable-module_ecdh --enable-module-recovery --enable-module-schnorrsig --enable-module-musig --enable-benchmark=no --enable-shared=no --enable-exhaustive-tests=no --enable-tests=no
make clean
make

[[ ! -z "$TO_UID" ]] && chown -R $TO_UID:$TO_UID .

cd ..

mkdir -p build/$TARGET
cp -v secp256k1/.libs/libsecp256k1.a build/$TARGET/

[[ ! -z "$TO_UID" ]] && chown -R $TO_UID:$TO_UID build

echo "Build done for $TARGET"
