#!/bin/sh
set -eu

base_url=${POLYFORM_DOWNLOAD_BASE:-https://omalled.com/polyform/downloads}
install_dir=${POLYFORM_INSTALL_DIR:-"$HOME/.local/bin"}

case "$(uname -s)-$(uname -m)" in
  Darwin-arm64) artifact=polyform-aarch64-apple-darwin ;;
  Linux-x86_64) artifact=polyform-x86_64-unknown-linux-gnu ;;
  *)
    echo "Polyform does not yet publish a binary for $(uname -s) $(uname -m)." >&2
    echo "Download the source from $base_url/polyform-source.tar.gz and build with Cargo." >&2
    exit 1
    ;;
esac

mkdir -p "$install_dir"
tmp_file=$(mktemp "${TMPDIR:-/tmp}/polyform.XXXXXX")
trap 'rm -f "$tmp_file"' EXIT HUP INT TERM
curl -fsSL "$base_url/$artifact" -o "$tmp_file"
chmod 755 "$tmp_file"
mv "$tmp_file" "$install_dir/polyform"
trap - EXIT HUP INT TERM

echo "Installed Polyform to $install_dir/polyform"
case ":$PATH:" in
  *":$install_dir:"*) ;;
  *) echo "Add $install_dir to PATH to run: polyform --help" ;;
esac
