#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BUILD_SCRIPT="$SCRIPT_DIR/build_static.sh"
BUILD_DIR="$SCRIPT_DIR/build"

HOST_UNAME="$(uname -m)"
case "$HOST_UNAME" in
    x86_64)
        OUTPUT_NAME="nsigner_static_x86_64"
        ;;
    aarch64|arm64)
        OUTPUT_NAME="nsigner_static_arm64"
        ;;
    armv7l|armv7)
        OUTPUT_NAME="nsigner_static_armv7"
        ;;
    *)
        echo "ERROR: Unsupported host architecture '$HOST_UNAME'"
        exit 1
        ;;
esac

"$BUILD_SCRIPT"

BINARY_PATH="$BUILD_DIR/$OUTPUT_NAME"
if [[ ! -x "$BINARY_PATH" ]]; then
    echo "ERROR: Compiled binary not found or not executable: $BINARY_PATH"
    exit 1
fi

exec "$BINARY_PATH" "$@"
