# Maintainer: Ryan Hughes <ryan@omarchy.org>
pkgname='omarchy-dev'
pkgver=4.0.0.r847.g4de185b
pkgrel=1
_pkgver_base=4.0.0
_pkgver_base_tag=v3.8.2
pkgdesc='Beautiful, modern, and opinionated Arch Linux by DHH (omarchy-4 branch tip)'
arch=('any')
url='https://github.com/basecamp/omarchy'
license=('MIT')
provides=('omarchy')
conflicts=('omarchy')
install='omarchy-dev.install'

# Hard depends: only packages whose removal would brick the desktop. The ISO
# pacstraps the full default install set from install/omarchy-base.packages.
# Keeping optional apps out of hard depends lets users remove them via Omarchy
# commands without pacman refusing because of a hard dependency.
depends=(
  # Omarchy meta
  'omarchy-keyring'
  'omarchy-settings-dev'

  # Bootloader stack. Lives here rather than on omarchy-settings because
  # omarchy-settings is also installed into the live ISO env (for plymouth
  # / /etc/skel seeding), where the limine-mkinitcpio-hook would fail with
  # 'Cannot detect an ESP path' inside the chroot. The omarchy-settings
  # config drop-ins under /etc/mkinitcpio.conf.d/, /etc/limine-entry-tool.d/,
  # and /etc/snapper/config-templates/ only do anything when these are
  # installed alongside.
  'limine'
  'limine-mkinitcpio-hook'
  'limine-snapper-sync'
  'snapper'

  # Wayland / Hyprland core
  'hyprland'
  'quickshell'
  'uwsm'
  'sddm'
  'xdg-desktop-portal-hyprland'

  # Audio / auth / portal essentials
  'wireplumber'
  'pipewire'
  'gnome-keyring'

  # Required by omarchy-* scripts and omarchy-shell
  'gum'
  'jq'
  'git'
  'perl' # /usr/bin/omarchy-shell invokes perl directly

  # System font (UI assumes it)
  'ttf-jetbrains-mono-nerd-basic'
)

makedepends=(
  'git'
)

optdepends=(
  # Development
  '1password: Password manager'
  '1password-cli: 1Password CLI'
  'github-cli: GitHub CLI tool'
  'lazygit: Terminal UI for git'
  'lazydocker: Terminal UI for docker'

  # Browsers & comms
  'omarchy-chromium-bin: Customized Chromium browser'
  'signal-desktop: Encrypted messaging'
  'spotify: Music streaming'

  # Productivity
  'obsidian: Note-taking app'
  'typora: Markdown editor'
  'libreoffice: Office suite'

  # Media & graphics
  'obs-studio: Screen recording and streaming'
  'mpv: Video player'
  'imv: Image viewer'
  'kdenlive: Video editor'
  'pinta: Image editor'

  # System utilities
  'btop: System monitor'

  # Printing
  'cups: Printing support'
  'system-config-printer: Printer configuration'

  # Hardware support
  'asdcontrol: Apple display brightness control'
  'blueberry: Bluetooth manager'

  # Developer tools
  'cargo: Rust package manager'
  'clang: C/C++ compiler'
  'python-poetry-core: Python packaging'
  'tree-sitter-cli: Parser generator tool'

  # Optional omarchy companion packages
)

# Source: omarchy-4 branch tip by default; set OMARCHY_SRC=/path/to/checkout
# to build from a local source tree instead (mirrors omarchy-zsh/omarchy-fish DX).
if [[ -n "${OMARCHY_SRC:-}" ]]; then
  source=()
  sha256sums=()
else
  source=("omarchy::git+https://github.com/basecamp/omarchy.git#branch=omarchy-4")
  sha256sums=('SKIP')
fi

pkgver() {
  cd "$srcdir/omarchy"

  local commit_count commit_hash
  if git rev-parse --verify "${_pkgver_base_tag}^{commit}" >/dev/null 2>&1; then
    commit_count=$(git rev-list --count "${_pkgver_base_tag}..HEAD")
  else
    commit_count=$(git rev-list --count HEAD)
  fi
  commit_hash=$(git rev-parse --short=7 HEAD)

  printf "%s.r%s.g%s" "$_pkgver_base" "$commit_count" "$commit_hash"
}

prepare() {
  if [[ -n "${OMARCHY_SRC:-}" ]]; then
    rm -rf "$srcdir/omarchy"
    mkdir -p "$srcdir/omarchy"
    cp -a "${OMARCHY_SRC}/." "$srcdir/omarchy/"
  fi
}

package() {
  cd "$srcdir/omarchy"

  # Runtime binaries — ship everything in bin/ except the debug helpers
  # that ship from omarchy-settings (needed before omarchy is installed,
  # e.g. on the ISO live env).
  install -d "$pkgdir/usr/bin" "$pkgdir/usr/share/omarchy/bin"
  for bin in bin/*; do
    [[ -f $bin ]] || continue
    local base
    base=$(basename "$bin")
    case "$base" in
      omarchy-debug|omarchy-debug-idle|omarchy-upload-log)
        continue
        ;;
    esac
    install -Dm755 "$bin" "$pkgdir/usr/bin/$base"
    ln -s "/usr/bin/$base" "$pkgdir/usr/share/omarchy/bin/$base"
  done

  install -d "$pkgdir/usr/share/omarchy"

  # Guard direct pacman upgrades from bypassing the Omarchy update pipeline.
  # Ship the hook with its Exec target so package skew cannot leave a broken
  # hook pointing at a missing binary.
  install -Dm644 default/libalpm/hooks/00-omarchy-update-guard.hook "$pkgdir/usr/share/libalpm/hooks/00-omarchy-update-guard.hook"

  # Target-side setup scripts and package lists used by ISO finalization,
  # first login, future users, migrations, and reinstall/reset commands.
  cp -a install "$pkgdir/usr/share/omarchy/"

  # Theme bundles for theme switching.
  cp -a themes "$pkgdir/usr/share/omarchy/"

  # Versioned migrations runner reads from this tree.
  cp -a migrations "$pkgdir/usr/share/omarchy/"

  # Fresh users created after install are already on the package layout in this
  # build, so seed user migration markers through /etc/skel. Existing users keep
  # their own state and still run pending user migrations on update.
  if compgen -G 'migrations/user/*.sh' >/dev/null; then
    install -d "$pkgdir/etc/skel/.local/state/omarchy/migrations/user"
    for migration in migrations/user/*.sh; do
      touch "$pkgdir/etc/skel/.local/state/omarchy/migrations/user/$(basename "$migration")"
    done
  fi

  # Quickshell desktop shell tree.
  cp -a shell "$pkgdir/usr/share/omarchy/"

  # Runtime version file.
  install -Dm644 version "$pkgdir/usr/share/omarchy/version"
}
