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

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

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

cd /tmp

if [ ! -d "unicorn_$1" ]; then
    git clone https://github.com/unicorn-engine/unicorn.git unicorn_$1
    cd unicorn_$1
else
    cd "unicorn_$1"
    git fetch
    git pull
fi

rm -rf build
mkdir build
cd build

cmake .. -DCMAKE_BUILD_TYPE=Release -DUNICORN_ARCH=aarch64
make -j$(sysctl -n hw.ncpu)
make -j$(sysctl -n hw.ncpu) install DESTDIR=/tmp/install_$1
