#!/bin/sh
# install zigman — read the zig language reference in your terminal.
#   curl -fsSL https://nate.tngl.io/zigman/install.sh | sh
set -eu

base="https://nate.tngl.io/zigman/release"

os=$(uname -s)
arch=$(uname -m)
case "$os" in
    Darwin) os=macos ;;
    Linux) os=linux ;;
    *) echo "zigman: unsupported OS '$os' — build from source: https://tangled.org/zzstoatzz.io/zigman" >&2; exit 1 ;;
esac
case "$arch" in
    arm64 | aarch64) arch=aarch64 ;;
    x86_64 | amd64) arch=x86_64 ;;
    *) echo "zigman: unsupported arch '$arch'" >&2; exit 1 ;;
esac

target="${arch}-${os}"
dest="${ZIGMAN_INSTALL:-$HOME/.local/bin}"
url="$base/$target.tar.gz"

tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
echo "zigman: downloading $target"
if ! curl -fSL "$url" -o "$tmp/zigman.tar.gz"; then
    echo "zigman: no prebuilt binary for $target at $url" >&2
    echo "zigman: build from source instead: https://tangled.org/zzstoatzz.io/zigman" >&2
    exit 1
fi
tar -xzf "$tmp/zigman.tar.gz" -C "$tmp"
mkdir -p "$dest"
mv "$tmp/zigman" "$dest/zigman"
chmod +x "$dest/zigman"

echo "zigman: installed to $dest/zigman"
case ":$PATH:" in
    *":$dest:"*) ;;
    *) echo "zigman: add $dest to your PATH to run 'zigman'" ;;
esac
