targets := `find crossfiles -type f | awk -F'[/.]' '{print $2}' | sed -z 's/\n/|/g'`
bash := `which bash`

list-targets:
    @echo '{{ targets }}'

[working-directory('.')]
core target="x86_64-linux": (check-supported target)
    @echo 'Building libcirce-core for target {{ target }}'
    meson setup --cross-file $(pwd)/crossfiles/{{ target }}.ini build-{{ target }}
    ninja -C build-{{ target }} libcirce-core.a

[working-directory('.')]
test target="x86_64-linux": (check-supported target)
    @echo 'Building libcirce-core for target {{ target }}'
    cd build-{{ target }} && meson test

[working-directory('.')]
check-supported target:
    #!{{ bash }}
    set -euo pipefail

    if [[(! "{{ target }}" =~ ^({{ targets }})$) || (-z "{{ target }}")]]; then
        echo "Target not supported. Use 'just list-targets' for a list of supported core targets."
        exit 1
    fi

[working-directory('.')]
cleanall:
    #!{{ bash }}
    set -euo pipefail

    echo 'Cleaning repo'
    for target in `echo '{{ targets }}' | sed 's/|/\n/g'`
    do
        if [[ -d "build-${target}" ]]; then
            ninja -C build-${target} clean
        fi
    done
