#!/bin/bash

# omarchy:summary=Build and install an Omarchy RPM from a local checkout
# omarchy:group=dev
# omarchy:args=[package-name] [path-to-checkout]
# omarchy:examples=omarchy dev pkg-test | omarchy dev pkg-test omarchy-dev ~/omadora

set -euo pipefail

if [[ ${1:-} == "-h" || ${1:-} == "--help" ]]; then
  cat <<USAGE
Usage: omarchy dev pkg-test [package-name] [path-to-checkout]

Builds and installs an Omarchy RPM from a local Omarchy source
checkout. Use when changes need to land at fixed system paths (/etc/,
/usr/lib/systemd/, udev rule bodies, plymouth) that omarchy-dev-link
can't shadow.

Defaults:
  packages          omarchy-settings-dev omarchy-dev
  path-to-checkout  \$OMARCHY_PATH (the dev-linked checkout)

Examples:
  omarchy dev pkg-test
  omarchy dev pkg-test omarchy-settings-dev
  omarchy dev pkg-test omarchy-dev ~/omadora

The release of the built package is tagged 'dev.<short-sha>[.dirty]' so
'rpm -q' makes it obvious where the installed version came from.

Specs are read from \${OMARCHY_SPECS_DIR:-<checkout>/../omadora-pkg/packaging/rpm}/<package>/<package>.spec,
the omadora-pkg checkout beside this one;
a -dev package builds its release spec with dev_suffix=-dev.
USAGE
  exit 0
fi

dev_package_name() {
  local pkg="$1"

  case "$pkg" in
    omarchy | omarchy-settings)
      printf '%s-dev\n' "$pkg"
      ;;
    *)
      printf '%s\n' "$pkg"
      ;;
  esac
}

if (( $# == 0 )); then
  PKGS=(omarchy-settings-dev omarchy-dev)
  CHECKOUT="$OMARCHY_PATH"
  RPMBUILD_ARGS=()
else
  PKGS=("$(dev_package_name "$1")")
  CHECKOUT="${2:-$OMARCHY_PATH}"
  RPMBUILD_ARGS=("${@:3}")
fi
# The specs live in omadora-pkg, kept beside the source checkout. A -dev
# package is the same spec built with dev_suffix, which renames it and makes it
# replace the release package.
SPECS_ROOT="${OMARCHY_SPECS_DIR:-$CHECKOUT/../omadora-pkg/packaging/rpm}"

spec_path() {
  local base=${1%-dev}
  printf '%s/%s/%s.spec\n' "$SPECS_ROOT" "$base" "$base"
}

if [[ ! -d "$CHECKOUT" ]]; then
  echo "Error: checkout not found at $CHECKOUT" >&2
  exit 1
fi
for PKG in "${PKGS[@]}"; do
  if [[ ! -f $(spec_path "$PKG") ]]; then
    echo "Error: spec not found at $(spec_path "$PKG")" >&2
    echo "       Pass a different package name as arg 1, or set OMARCHY_SPECS_DIR." >&2
    exit 1
  fi
done

build_dir=$(mktemp -d -t omarchy-dev-pkg-test.XXXXXX)
trap 'rm -rf "$build_dir"' EXIT

# release=dev.<sha>[.dirty] so rpm -q makes the source obvious.
short_sha=$(git -C "$CHECKOUT" rev-parse --short HEAD 2>/dev/null || echo "local")
dirty=""
if [[ -d "$CHECKOUT/.git" ]] && [[ -n "$(git -C "$CHECKOUT" status --porcelain)" ]]; then
  dirty=".dirty"
fi
new_release="dev.${short_sha}${dirty}"

for PKG in "${PKGS[@]}"; do
  spec=$(spec_path "$PKG")
  suffix_args=()
  [[ $PKG != *-dev ]] || suffix_args=(--define "dev_suffix -dev")

  echo "Building $PKG ${new_release} from $CHECKOUT"
  echo "  spec: $spec"
  echo

  OMARCHY_SRC="$CHECKOUT" rpmbuild -bb \
    --define "_topdir $build_dir" \
    --define "omarchy_release ${new_release}" \
    "${suffix_args[@]}" \
    "${RPMBUILD_ARGS[@]}" \
    "$spec"

  # Install separately so a failed install leaves the previous version alone.
  # Dev builds frequently replace files owned by the released RPM; --allowerasing
  # lets the dev build take them over in one transaction.
  built_rpm=$(find "$build_dir/RPMS" -name "$PKG-*.rpm" | head -1)
  if [[ -z $built_rpm ]]; then
    echo "Error: no built RPM found under $build_dir/RPMS" >&2
    exit 1
  fi
  sudo dnf install -y --allowerasing "$built_rpm"
done
