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

if [[ $# -eq 0 ]]; then
    echo 'You must specify a target architecture (x86_64 or aarch64)'
    exit 1
fi

rm -rf /tmp/libusb_$1

LIBUSB_VERSION=1.0.26

export CC="zig cc -target $1-linux-musl"
export LD="zig cc -target $1-linux-musl"

cd /tmp

wget -N https://github.com/libusb/libusb/releases/download/v$LIBUSB_VERSION/libusb-$LIBUSB_VERSION.tar.bz2
mkdir libusb_$1
tar -xf libusb-$LIBUSB_VERSION.tar.bz2 --strip-components=1 -C /tmp/libusb_$1

cd libusb_$1

./configure --host=$1-linux-musl --disable-shared --enable-static -disable-udev
make -j$(sysctl -n hw.ncpu)
make -j$(sysctl -n hw.ncpu) install DESTDIR=/tmp/install_$1
