#!/bin/bash
set -e

# download and install dropshell

# 1. Determine architecture
# ----------------------------------------------------------------------------- 

ARCH=$(uname -m)
if [[ "$ARCH" == "x86_64" ]]; then
    BIN=dropshell.x86_64
elif [[ "$ARCH" == "aarch64" || "$ARCH" == "arm64" ]]; then
    BIN=dropshell.aarch64
else
    echo "Unsupported architecture: $ARCH" >&2
    exit 1
fi


# 2. Download the appropriate binary to a temp directory
# -----------------------------------------------------------------------------
TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT
URL="https://gitea.jde.nz/public/dropshell/releases/download/latest/$BIN"
echo "Downloading $BIN from $URL..."

TARGET_PATH="${HOME}/.local/bin/dropshell"

mkdir -p "${HOME}/.local/bin"

curl -fsSL -o "$TARGET_PATH" "$URL"

if [ ! -f "$TARGET_PATH" ]; then
    echo "Failed to download dropshell" >&2
    exit 1
fi

chmod +x "$TARGET_PATH"

if [ ! -f "${HOME}/.local/bin/ds" ]; then
    ln -s "$TARGET_PATH" "${HOME}/.local/bin/ds"
fi

echo "dropshell installed successfully to $TARGET_PATH"

echo "Please:"
echo "1. run '${TARGET_PATH} edit' to edit the configuration."
echo "2. run '${TARGET_PATH} install' to install dropshell components on this computer."
echo "3. run 'source ~/.bashrc' to add to your path and autocomplete for the current shell."