#!/bin/sh

# Installer for git-remote-gitopia
# This script is POSIX-compliant and should run on any system with a standard shell,
# including Alpine Linux (ash), Debian (dash), and systems with bash.
#
# Usage: curl -sfL https://get.gitopia.com | sh
#   or   curl -sfL https://get.gitopia.com | sudo sh

# Exit immediately if a command exits with a non-zero status.
# Exit immediately if a variable is unset.
set -eu

# --- Configuration ---
USER="gitopia"
PROG="git-remote-gitopia"
PROG2="git-gitopia"
PROG3="git-credential-gitopia"
VERSION="2.1.1" # Hardcoded version
INSTALL_DIR="/usr/local/bin"
OBJECTS_URL="https://server.gitopia.com"

# --- Helper Functions ---
# Print an error message to stderr and exit.
fail() {
  echo "Error: $1" >&2
  exit 1
}

# --- Main Installation Logic ---

# 1. Platform Detection
# Detect OS and architecture in a portable way.
OS_RAW=$(uname -s)
case "$OS_RAW" in
  Linux)
    OS="linux"
    ;;
  Darwin)
    OS="darwin"
    ;;
  *)
    fail "Unsupported operating system: $OS_RAW"
    ;;
esac

ARCH_RAW=$(uname -m)
case "$ARCH_RAW" in
  x86_64 | amd64)
    ARCH="amd64"
    ;;
  aarch64 | arm64)
    ARCH="arm64"
    ;;
  armv*)
    ARCH="arm"
    ;;
  i386 | i686)
    ARCH="386"
    ;;
  *)
    fail "Unsupported architecture: $ARCH_RAW"
    ;;
esac

PLATFORM="${OS}_${ARCH}"

# 3. Construct Download URL
# We assume all downloads are .tar.gz files.
FILENAME="${PROG}_${VERSION}_${PLATFORM}.tar.gz"
URL="${OBJECTS_URL}/releases/${USER}/${PROG}/v${VERSION}/${FILENAME}"

# 4. Dependency and Permissions Check
# Check for required tools. command -v is the POSIX way.
if command -v curl >/dev/null 2>&1; then
  DOWNLOADER="curl --fail -# -L"
elif command -v wget >/dev/null 2>&1; then
  DOWNLOADER="wget -qO-"
else
  fail "Either 'curl' or 'wget' is required for downloading."
fi

if ! command -v tar >/dev/null 2>&1; then
  fail "'tar' is required for extraction."
fi

# Check for write permissions on the installation directory before downloading.
if [ ! -w "$INSTALL_DIR" ]; then
    echo "Info: Write permissions are required for '$INSTALL_DIR'."
    echo "Info: You may be prompted for your password by 'sudo'."
    SUDO="sudo"
else
    SUDO=""
fi

# 5. Download and Extract
# Create a secure temporary directory that will be cleaned up automatically on exit.
TMP_DIR=$(mktemp -d -t gitopia-install.XXXXXX)
trap 'rm -rf "$TMP_DIR"' EXIT

echo "Installing ${USER}/${PROG} v${VERSION} for ${PLATFORM}..."
echo "Downloading from: $URL"

cd "$TMP_DIR"
# The pipeline downloads and extracts in one step. `set -e` ensures it fails if any part fails.
$DOWNLOADER "$URL" | tar zxf - || fail "Download or extraction failed. Please check the URL and your network connection."

# 6. Installation
# Verify that the expected binaries are present after extraction.
for bin in "$PROG" "$PROG2" "$PROG3"; do
  if [ ! -f "$bin" ]; then
    fail "Could not find the '$bin' binary in the downloaded archive."
  fi
done

echo "Installing binaries to $INSTALL_DIR..."

# Use the 'install' command, which is designed for this. It sets permissions and ownership.
# It's more portable and reliable than `mv` followed by `chmod`.
$SUDO install -m 755 "$PROG" "$INSTALL_DIR"
$SUDO install -m 755 "$PROG2" "$INSTALL_DIR"
$SUDO install -m 755 "$PROG3" "$INSTALL_DIR"

echo
echo "Successfully installed!"
echo "  - $INSTALL_DIR/$PROG"
echo "  - $INSTALL_DIR/$PROG2"
echo "  - $INSTALL_DIR/$PROG3"
echo