#!/bin/sh
export PB_HOST_APP=$(cd "$(dirname $0)/../.." && pwd)
TOOLCHAIN="$HOME/Library/Application Support/Playbit/toolchain"
PB="$TOOLCHAIN/bin/pb"

if [ ! -x "$PB" ]; then
    if [ -f "$PB_HOST_APP/Contents/Resources/tc.tar.xz" ]; then
        echo "Installing toolchain at $TOOLCHAIN"
        mkdir -p "$TOOLCHAIN"
        (cd "$TOOLCHAIN" && tar -xJf "$PB_HOST_APP/Contents/Resources/tc.tar.xz")
    fi
    if [ ! -x "$PB" ]; then
        PB=$(command -v pb)
    fi
fi

if [ -x "$PB" ]; then
    exec "$PB" "$@"
fi

echo "$0: Cannot find Playbit toolchain" >&2
exit 1
