#!/usr/bin/env bash

set -euo pipefail

SELF_DIR="$(CDPATH= cd -- "$(dirname "$0")" && pwd)"
PACKAGE_FILE="$(find "$SELF_DIR" -maxdepth 1 -type f -name 'freebasic-*.tar.xz' | sort | tail -n 1)"

if [ -z "${PACKAGE_FILE:-}" ]; then
	echo "ERROR: no freebasic-*.tar.xz package found next to this script." >&2
	exit 1
fi

case "$(uname -s)" in
	CYGWIN*) ;;
	*)
		echo "ERROR: this installer must be run inside Cygwin." >&2
		exit 1
		;;
esac

if [ ! -w /usr/bin ]; then
	echo "ERROR: installation requires write access to /usr." >&2
	echo "Re-run this script from an elevated Cygwin shell or as a user with permission to write to /usr." >&2
	exit 1
fi

echo "==> Installing package: $PACKAGE_FILE"
tar -C / -xJf "$PACKAGE_FILE"

echo ""
echo "Installed FreeBASIC into /usr."
echo "Compiler path: /usr/bin/fbc.exe"
