#!/usr/bin/env bash
set -euo pipefail

BASE_URL="${BASE_URL:-https://git.jewguard.xyz/linusware/main/raw/branch/main}"
INSTALL_DIR="${HOME}/linusware"

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

info() {
  echo -e "${GREEN}[*]${NC} $1"
}
warn() {
  echo -e "${YELLOW}[!]${NC} $1"
}
error() {
  echo -e "${RED}[x]${NC} $1"
  exit 1
}
command -v curl >/dev/null 2>&1 || error "curl is required"
command -v md5sum >/dev/null 2>&1 || error "md5sum is required"
command -v tar >/dev/null 2>&1 || error "tar is required"

rm -rf $HOME/linusware
mkdir -p "${INSTALL_DIR}/external" "${INSTALL_DIR}/internal"

download() {
  local url="$1" dest="$2"
  local tmp; tmp="$(mktemp)"
  curl -fsSL -o "$tmp" "$url" || { rm -f "$tmp"; return 1; }
  mv "$tmp" "$dest"
}

install_edition() {
  local edition="$1"
  local status_url="${BASE_URL}/${edition}/status.json"
  local status_file="${INSTALL_DIR}/${edition}/status.json"

  info "Checking ${edition} edition..."

  download "$status_url" "$status_file" || {
    warn "Could not fetch status.json for ${edition}, skipping"
    return
  }

  local updated
  updated=$(sed -n 's/.*"updated":[[:space:]]*\([a-z]*\).*/\1/p' "$status_file")
  if [ "$updated" != "true" ]; then
    info "${edition}: not yet released, skipping"
    return
  fi

  local version
  version=$(sed -n 's/.*"version":[[:space:]]*"\([^"]*\)".*/\1/p' "$status_file")
  info "Downloading ${edition} v${version}..."

  local md5_url="${BASE_URL}/${edition}/bin/MD5SUM"
  local md5_file; md5_file="$(mktemp)"
  download "$md5_url" "$md5_file" || {
    rm -f "$md5_file"
    warn "Could not fetch MD5SUM for ${edition}"
    return
  }
  local expected_hash
  expected_hash=$(tr -cd '[:xdigit:]' < "$md5_file"); rm -f "$md5_file"

  local archive_url="${BASE_URL}/${edition}/bin/linusware.tar.gz"
  local tmp_archive; tmp_archive="$(mktemp)"
  download "$archive_url" "$tmp_archive" || {
    rm -f "$tmp_archive"
    error "Failed to download ${edition} archive"
  }

  local actual_hash
  actual_hash=$(md5sum "$tmp_archive" | cut -d' ' -f1)
  if [ "$actual_hash" != "$expected_hash" ]; then
    rm -f "$tmp_archive"
    error "MD5 mismatch for ${edition} (expected ${expected_hash}, got ${actual_hash})"
  fi

  rm -rf "${INSTALL_DIR}/${edition}/linusware"
  tar -xzf "$tmp_archive" -C "${INSTALL_DIR}/${edition}"
  rm -f "$tmp_archive"

  local run_path="${INSTALL_DIR}/${edition}/linusware/run.sh"
  chmod +x "$run_path"
  echo "$version" > "${INSTALL_DIR}/${edition}/version.txt"

  info "${edition} v${version} installed successfully"
}

# main
info "Installing LinusWare to ${INSTALL_DIR}"
echo ""

install_edition external
install_edition internal

info "Downloading launcher and updater..."
download "${BASE_URL}/launcher.sh" "${INSTALL_DIR}/launcher.sh"
download "${BASE_URL}/update.sh" "${INSTALL_DIR}/update.sh"
chmod +x "${INSTALL_DIR}/launcher.sh" "${INSTALL_DIR}/update.sh"

echo ""
info "Installation complete!"
echo ""
echo "  Run:  ${INSTALL_DIR}/launcher.sh"
