#!/bin/zsh
set -euo pipefail

tool_name="macOS Screenshot"
script_url="https://tools.kelownafilmstudios.com/macosscreenshot/macosscreenshot.py"

if [[ "$(uname -s)" != "Darwin" ]]; then
  print -u2 -- "$tool_name is for macOS only."
  exit 1
fi

if ! command -v curl >/dev/null 2>&1; then
  print -u2 -- "curl is required to install $tool_name."
  exit 1
fi

if [[ -x /usr/bin/python3 ]]; then
  python_bin="/usr/bin/python3"
elif command -v python3 >/dev/null 2>&1; then
  python_bin="$(command -v python3)"
else
  print -u2 -- "python3 is required to run $tool_name."
  exit 1
fi

tmp_root="$(mktemp -d "${TMPDIR:-/tmp}/macosscreenshot.XXXXXX")"
tmp_script="$tmp_root/macosscreenshot.py"
trap 'rm -rf "$tmp_root"' EXIT

print -- "Downloading $tool_name..."
curl -fsSL "$script_url" -o "$tmp_script"
chmod 700 "$tmp_script"

if [[ -r /dev/tty ]]; then
  "$python_bin" "$tmp_script" "$@" < /dev/tty
else
  "$python_bin" "$tmp_script" "$@"
fi
