# Maintainer: Asuka Minato <i at asukaminato dot eu dot org>
# Maintainer: Kid <hi at xuann dot wang>
# Maintainer: Mateus Honorato <mateush.honorato@gmail.com>
# Contributor: Jaime Martínez Rincón <jaime@jamezrin.name>

pkgname=notion-app-electron
pkgver=4.3.0
_bettersqlite3ver=11.7.2
_bufferutilver=4.0.8
_elecronver=130
_electron_version=33
pkgrel=4
pkgdesc="Your connected workspace for wiki, docs & projects"
arch=(aarch64)
url=https://www.notion.so/desktop
license=(custom)
depends=(
  bash
  gcc-libs
  glibc
  hicolor-icon-theme
  electron${_electron_version}
)
makedepends=(
  7zip
  asar
  npm
  python
  python-setuptools
)
replaces=(notion-app-nativefier)

source=(
  https://desktop-release.notion-static.com/Notion%20Setup%20${pkgver}.exe
  https://github.com/WiseLibs/better-sqlite3/releases/download/v${_bettersqlite3ver}/better-sqlite3-v${_bettersqlite3ver}-electron-v${_elecronver}-linux-arm64.tar.gz
  https://github.com/websockets/bufferutil/archive/refs/tags/v${_bufferutilver}.tar.gz
  notion-app.in
  notion.desktop
  notion.png
)
sha256sums=('99c8bdb4f28c4a45861fcc818012739ac5385d5147b19cf17f2e095dd222cf99'
            'aa75e7d0c95d686ebd302ce778d1714846b47a2f886adb532c2c1f18a9186199'
            'e8357a79d1a79e56d9183fe594eff9ffb1df4d5ac8f101765c0e1f4065b0957e'
            '7f9ff318a5496eec764cff471f05cf55e23481bc54e4dc457976ea4941d7b5b2'
            '19a5f973f1e9291081aa05512e07c61447e8c30e1a43dd22d0cc1090837d1e19'
            '61ecb0c334becf60da4a94482f10672434944e4d93e691651ec666cafb036646')

build() {
  sed -e "s/@ELECTRON_VER@/${_electron_version}/g" "$srcdir/notion-app.in" > "$srcdir/notion-app"
  (
    cd bufferutil-${_bufferutilver}/
    npm install
    npm run prebuild --openssl_fips=''
  )

  # extracting app.asar from installer with 7z and ignoring errors
  7z x "./Notion%20Setup%20${pkgver}.exe" "\$PLUGINSDIR/app-64.7z" -y -bse0 -bso0 || true
  7z x "./\$PLUGINSDIR/app-64.7z" "resources/app.asar" "resources/app.asar.unpacked" -y -bse0 -bso0 || true
  rm "./\$PLUGINSDIR/app-64.7z"
  # extracting resources from app.asar
  asar e "$srcdir/resources/app.asar" "$srcdir/asar_patched"
  # replacing better_sqlite3 release in the patched resources
  mv "$srcdir/build/Release/better_sqlite3.node" "$srcdir/asar_patched/node_modules/better-sqlite3/build/Release/"
  # replacing bufferutil release in the patched resources
  mv "$srcdir/bufferutil-${_bufferutilver}/prebuilds/linux-arm64/node.napi.node" "$srcdir/asar_patched/node_modules/bufferutil/build/Release/bufferutil.node"
  # removing some unnecessary files
  rm "$srcdir/asar_patched/node_modules/node-mac-window" -r
  rm "$srcdir/asar_patched/node_modules/better-sqlite3/build/Release/test_extension.node"
  # adding tray icon to the unpacked resources
  cp "$srcdir/notion.png" "$srcdir/asar_patched/.webpack/main/trayIcon.png"
  # fixing tray icon and right click menu
  sed -i 's|this.tray.on("click",(()=>{this.onClick()}))|this.tray.setContextMenu(this.trayMenu),this.tray.on("click",(()=>{this.onClick()}))|g' "$srcdir/asar_patched/.webpack/main/index.js"
  sed -i 's|getIcon(){[^}]*}|getIcon(){return require("path").resolve(__dirname, "trayIcon.png");}|g' "$srcdir/asar_patched/.webpack/main/index.js"
  # fake the useragent as windows to fix the spellchecker languages selector and other issues
  sed -i 's|e.setUserAgent(`${e.getUserAgent()} WantsServiceWorker`),|e.setUserAgent(`${e.getUserAgent().replace("Linux", "Windows")} WantsServiceWorker`),|g' "$srcdir/asar_patched/.webpack/main/index.js"
  # fully disabling auto updates
  sed -i 's|if("darwin"===process.platform){const e=l.systemPreferences?.getUserDefault(C,"boolean"),t=_.Store.getState().app.preferences?.isAutoUpdaterDisabled,r=_.Store.getState().app.preferences?.isAutoUpdaterOSSupportBypass,n=(0,v.isOsUnsupportedForAutoUpdates)();return Boolean(e\|\|t\|\|!r&&n)}return!1|return!0|g' "$srcdir/asar_patched/.webpack/main/index.js"
  # avoid running duplicated instances, fixes url opening
  sed -i 's|handleOpenUrl);else if("win32"===process.platform)|handleOpenUrl);else if("linux"===process.platform)|g' "$srcdir/asar_patched/.webpack/main/index.js"
  sed -i 's|async function(){await(0,m.setupObservability)(),|o.app.requestSingleInstanceLock() ? async function(){await(0,m.setupObservability)(),|g' "$srcdir/asar_patched/.webpack/main/index.js"
  sed -i 's|setupAboutPanel)()}()}()|setupAboutPanel)()}()}() : o.app.quit();|g' "$srcdir/asar_patched/.webpack/main/index.js"
  # use the windows version of the tray menu
  sed -i 's|r="win32"===process.platform?function(e,t)|r="linux"===process.platform?function(e,t)|g' "$srcdir/asar_patched/.webpack/main/index.js"
  # use the windows logic for tray icon
  sed -i 's/return!(!r||!n)||"win32"===process.platform/return!(!r||!n)||"linux"===process.platform/g' "$srcdir/asar_patched/.webpack/main/index.js"
  # this can disable app menu when the options won't work. disbled in the current version because it's working now, but it's here for future reference
  # sed -i 's|Menu.setApplicationMenu(p(e))|Menu.setApplicationMenu(null)|g' "$srcdir/asar_patched/.webpack/main/index.js"
  # repacking asar with all the patches
  asar p "$srcdir/asar_patched" "$srcdir/app.asar" --unpack *.node
}

package() {
  local usr="$pkgdir/usr"
  local share="$usr/share"
  local lib="$usr/lib/notion-app"

  install -d "$lib"
  cp "$srcdir/asar_patched/package.json" "$lib"
  cp "$srcdir/app.asar" "$lib"
  cp "$srcdir/app.asar.unpacked" "$lib" -r
  install -Dm755 notion-app -t "$usr/bin"
  install -Dm644 "$srcdir/notion.desktop" -t "$share/applications"
  install -Dm644 "$srcdir/notion.png" -t "$share/icons/hicolor/256x256/apps"
  find "$pkgdir" -type d -empty -delete
}
