#!/bin/sh
# cpw installer for Linux: curl -fsSL https://copypaste.wiki/install.sh | sh
set -eu
BASE="${CPW_BASE:-https://copypaste.wiki}"
case "$(uname -s)" in
  Linux) ;;
  *) echo "cpw: this installer is for Linux. On Windows download cpw.exe from $BASE/cli" >&2; exit 1 ;;
esac
case "$(uname -m)" in
  x86_64|amd64) ARCH=amd64; SUM=dab202305d91f87ecedffe35613ee1ad91365ac5d9e04cf310c790ea6ccd660f ;;
  aarch64|arm64) ARCH=arm64; SUM=d9edd7e10e4007e35660911412e7378d059eb949b91a43ef9d41c79a9e086fbd ;;
  *) echo "cpw: unsupported CPU $(uname -m) (only x86_64 and arm64 are built)" >&2; exit 1 ;;
esac
TMP="$(mktemp)"
trap 'rm -f "$TMP"' EXIT
URL="$BASE/downloads/cpw-linux-$ARCH"
echo "Downloading $URL"
if command -v curl >/dev/null 2>&1; then curl -fsSL "$URL" -o "$TMP"
elif command -v wget >/dev/null 2>&1; then wget -qO "$TMP" "$URL"
else echo "cpw: install curl or wget first" >&2; exit 1; fi
if command -v sha256sum >/dev/null 2>&1; then GOT="$(sha256sum "$TMP" | cut -d' ' -f1)"
else GOT="$(shasum -a 256 "$TMP" | cut -d' ' -f1)"; fi
if [ "$GOT" != "$SUM" ]; then echo "cpw: checksum mismatch, download aborted" >&2; exit 1; fi
chmod +x "$TMP"
"$TMP" install
