# Maintainer: Spencer Bull <spencer.bull@hey.com>

pkgname=elsewhen
pkgver=1.0.0
pkgrel=2
pkgdesc='World clock plugin for the Omarchy shell'
arch=('any')
url='https://github.com/omacom/elsewhen'
license=('MIT')

# What the plugin needs to load and run. It also shells out to bash, date
# (coreutils) and timedatectl (systemd) and reads /usr/share/zoneinfo
# (tzdata); those are members of the base group and stay implicit, per Arch
# convention. The citations for each entry are in .omarchy/README.md.
depends=(
  'omarchy'
  'python'
  'quickshell'
)

options=('!debug')

source=("$pkgname-$pkgver.tar.gz::$url/archive/refs/tags/v$pkgver.tar.gz")
sha256sums=('3124f0c0a19ebc1b158bcf04151cddd6c733ceeead88052186b6a54c46bee263')

package() {
  # Install alongside the bundled plugins in the shell's plugin directory.
  local plugin="$pkgdir/usr/share/omarchy/shell/plugins/omacom.elsewhen"
  cd "$srcdir/$pkgname-$pkgver" || return 1

  # The shell loads the entry point each manifest declares. A tree without
  # either would install cleanly and never load, so fail the build instead of
  # shipping it.
  [[ -f manifest.json ]] || {
    echo "release tree is missing manifest.json" >&2
    return 1
  }
  grep -Eq '"id"[[:space:]]*:[[:space:]]*"omacom\.elsewhen"' manifest.json || {
    echo "manifest.json does not declare the plugin id omacom.elsewhen" >&2
    return 1
  }
  grep -Eq '"barWidget"[[:space:]]*:[[:space:]]*"Panel\.qml"' manifest.json || {
    echo "manifest.json does not name Panel.qml as the bar widget entry point" >&2
    return 1
  }
  [[ -f Panel.qml ]] || {
    echo "release tree is missing the entry point Panel.qml" >&2
    return 1
  }

  # An explicit allow-list of runtime files, so tests/, .github/ and the rest
  # of the repository never reach the package. Directories end up 0755 and
  # every file 0644: worldclock-data.py runs as `python3 <path>` and needs no
  # execute bit. An unmatched glob is left literal and fails install, which
  # is the right outcome for a release tree missing its QML or JS.
  local file
  for file in manifest.json cities.json world.json worldclock-data.py *.qml *.js; do
    install -Dm644 "$file" "$plugin/$file"
  done

  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
  install -Dm644 README.md "$pkgdir/usr/share/doc/$pkgname/README.md"
}
