# shellcheck shell=bash disable=SC2034,SC2154
# Packager: Storm Dragon <storm_dragon@stormux.org>

pkgname=cthulhu
pkgver=2026.05.25
pkgrel=1slint
source=()
url="https://git.stormux.org/storm/cthulhu"
options=('nosrcpack')

depends=(
  'at-spi2-core'
  'brltty'
  'gobject-introspection'
  'gsettings-desktop-schemas'
  'gstreamer'
  'gst-plugins-base'
  'gst-plugins-good'
  'gtk+3'
  'hicolor-icon-theme'
  'liblouis'
  'libwnck3'
  'pango'
  'python-requests'
  'python-atspi'
  'pycairo3.11'
  'python3-dasbus'
  'pygobject3.11'
  'python3-pluggy'
  'python3-tomlkit'
  'setproctitle3.11'
  'socat'
  'speech-dispatcher'
  'xkbcomp'
  'xmodmap'
  'python3-pyautogui'
  'python3-msgpack'
  'python3-tornado'
  'python3-pillow'
  'python-pdf2image'
  'python-pytesseract'
  'python-scipy'
  'python-webcolors'
  'python-xlib'
  'tesseract'
  'tesseract-data'
)
makedepends=(
  'git'
  'meson'
  'ninja'
  'pandoc'
)

slackdesc=\
(
#|-----handy-ruler------------------------------------------------------|
"cthulhu (Screen reader for blind or visually impaired users)"
""
"Cthulhu is a screen reader for individuals who are blind or visually"
"impaired, forked from Orca. It provides a way to access applications"
"and toolkits that support the AT-SPI (e.g., the GNOME desktop)."
""
"Homepage: https://git.stormux.org/storm/cthulhu"
""
""
""
)

doinst() {
if [ -x /usr/bin/update-desktop-database ]; then
  /usr/bin/update-desktop-database -q usr/share/applications >/dev/null 2>&1
fi

if [ -e usr/share/icons/hicolor/icon-theme.cache ]; then
  if [ -x /usr/bin/gtk-update-icon-cache ]; then
    /usr/bin/gtk-update-icon-cache usr/share/icons/hicolor >/dev/null 2>&1
  fi
fi
}

build() {
  cd "$SRC" || exit 1
  rm -rf "$pkgname-$pkgver"
  git clone -b "$pkgver" https://git.stormux.org/storm/cthulhu.git "$pkgname-$pkgver"
  cd "$pkgname-$pkgver" || exit 1

  chown -R root:root .
  find -L . \
    \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
    -o -perm 511 \) -exec chmod 755 {} \; -o \
    \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
    -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

  mkdir -p "$PKG/usr/share/glib-2.0/schemas"
  cp "$startdir/toggle.screenreader.gschema.xml" "$PKG/usr/share/glib-2.0/schemas"
  mkdir -p "$PKG/usr/libexec"
  cp "$startdir/toggle_screenreader.sh" "$PKG/usr/libexec"
  chmod 755 "$PKG/usr/libexec/toggle_screenreader.sh"

  rm -rf build
  export PYTHON=/usr/bin/python3.11
  if [ ! -x /usr/bin/python3.11 ]; then
    echo "$pkgname build requires /usr/bin/python3.11" >&2
    exit 1
  fi
  pythonShimDir="$(mktemp -d)"
  ln -s /usr/bin/python3.11 "$pythonShimDir/python3"

  PATH="$pythonShimDir:$PATH" \
  CFLAGS="$SLKCFLAGS" \
  CXXFLAGS="$SLKCFLAGS" \
  meson setup build \
    --prefix=/usr \
    --libdir=/usr/lib"${LIBDIRSUFFIX}" \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --buildtype=release

  PATH="$pythonShimDir:$PATH" meson compile -C build -j "$numjobs"
  PATH="$pythonShimDir:$PATH" DESTDIR="$PKG" meson install -C build
  rm -rf "$pythonShimDir"

  mkdir -p "$PKG/usr/doc/$pkgname-$pkgver"
  pandoc README.md -o "$PKG/usr/doc/$pkgname-$pkgver/README.html"
  pandoc README.md -o "$PKG/usr/doc/$pkgname-$pkgver/README.pdf"
  pandoc "$startdir/Cthulhu_and_Orca.md" -o \
    "$PKG/usr/doc/$pkgname-$pkgver/Cthulhu_and_Orca.html"
  pandoc "$startdir/Cthulhu_and_Orca.md" -o \
    "$PKG/usr/doc/$pkgname-$pkgver/Cthulhu_and_Orca.pdf"
  cp -a COPYING ChangeLog NEWS README.md \
    "$PKG/usr/doc/$pkgname-$pkgver"
  sed 's#Cthulhu Authors#Orca authors#' AUTHORS > \
    "$PKG/usr/doc/$pkgname-$pkgver/AUTHORS"
  cp "$startdir/SLKBUILD" \
    "$PKG/usr/doc/$pkgname-$pkgver/SLKBUILD"

  sed -i \
    -e "1s|^#!/usr/bin/python3$|#!/usr/bin/python3.11|" \
    -e "1s|^#!/usr/bin/env python3$|#!/usr/bin/python3.11|" \
    "$PKG/usr/bin/$pkgname"
  cthulhuLauncher="$PKG/usr/bin/$pkgname"
  tmpLauncher="$(mktemp)"
  awk '
    /if pythondir is None:/ {
      print "        if pythondir is None:"
      print "            for candidate_dir in sys.path:"
      print "                if not candidate_dir:"
      print "                    continue"
      print "                if os.path.exists(os.path.join(candidate_dir, '\''cthulhu'\'', '\''__init__.py'\'')):"
      print "                    pythondir = candidate_dir"
      print "                    break"
    }
    {
      print
      if ($0 ~ /possible_pythondirs = \[/) {
        print "            os.path.join(prefix, '\''lib64'\'', python_version, '\''site-packages'\''),"
        print "            os.path.join('\''/usr'\'', '\''lib64'\'', python_version, '\''site-packages'\''),  # System lib64 fallback"
        print "            os.path.join('\''/usr/local'\'', '\''lib64'\'', python_version, '\''site-packages'\''),  # /usr/local lib64 fallback"
      }
    }
  ' "$cthulhuLauncher" > "$tmpLauncher"
  mv "$tmpLauncher" "$cthulhuLauncher"
  chmod 755 "$PKG/usr/bin/$pkgname"
}
