# Maintainer:  Giovanni 'ItachiSan' Santini <giovannisantini93@yahoo.it>
# Contributor: Filipe Laíns (FFY00) <lains@archlinux.org>
# Contributor: Pieter Goetschalckx <3.14.e.ter <at> gmail <dot> com>

pkgname=franz
_pkgver=5.10.0 # -beta.0
pkgver=${_pkgver//-/} # Leaving it here for possible dev/beta package :)
pkgrel=3
# Due to the previous "_beta" naming
epoch=1
pkgdesc='Free messaging app for services like WhatsApp, Slack, Messenger and many more.'
arch=(x86_64 i686 armv7h aarch64)
url='https://meetfranz.com'
license=(Apache)
# Allow to easily switch between Electron versions.
# Expected one is 'electron19' (Electron 19). May change soon.
# This is automatically replaced in `franz.sh` with the package name, as
# the executable matches the package name (as of 2020-11-15).
_electron='electron24'
depends=($_electron bash)
makedepends=(git python nodejs-lts-hydrogen npm nvm expac)
source=("git+https://github.com/meetfranz/$pkgname#branch=chore/electron-19"
        franz.desktop
        franz.sh.in
        0001-Add-cmdline-option-to-open-with-window-closed.patch)
sha512sums=('SKIP'
            '049c4bf2e0f362f892e8eef28dd18a6c321251c686a9c9e49e4abfb778057de2fc68b95b4ff7bb8030a828a48b58554a56b810aba078c220cb01d5837083992e'
            '44486b524353eb06fd6b4e65ed586b10a765c9da7b5da9ccd98146cc7dc91fe0a8fdbfe8c792061b047d35f43338536acc7bd8ebadcee6793f194dad75ae50cd'
            '8f59b911eccd4f8c965f60ae4ba111195f48a76326e1c63391b1ab61b74d74dcb021fdb835da07037b28823cac1c3fd3b01f5ace501b6b6b7fe6db5e2aef8068')

_env_setup() {
  which nvm >/dev/null 2>&1 && nvm deactivate && nvm unload
  export NVM_DIR="$srcdir/.nvm"

  # The init script returns 3 if version specified
  # in ./.nvrc is not (yet) installed in $NVM_DIR
  # but nvm itself still gets loaded ok
  source /usr/share/nvm/init-nvm.sh || [[ $? != 1 ]]

  # Personal extras
  # Avoid installing Electron as we use the system one
  export ELECTRON_SKIP_BINARY_DOWNLOAD=1
  # For safety, define the NPM cache in $srcdir
  # Better configuration for npm cache and calling installed binaries
  export npm_config_cache="$srcdir/npm_cache"
}

prepare() {
  # Small patching
  cd $pkgname

  patch -Np1 --no-backup-if-mismatch < ../0001-Add-cmdline-option-to-open-with-window-closed.patch

  # # Adjust the electron version to use when building
  # echo "--> Electron package version: $electron_version"
  # sed -i -E "s|(\s+\"electron\":).*,|\1 \"$electron_version\",|" package.json

  # Prevent Franz from being launched in development mode
  # This changes all the occurences where 'isDevMode' is set to a value.
  grep -lr 'isDevMode =' src | xargs sed -E 's|^(.*isDevMode =) .*$|\1 false|' -i

  _env_setup

  echo "--> Install toolchain with nvm"
  nvm install

  echo "--> Install updated node-gyp"
  npm install node-gyp@9

  echo "--> Install modules dependencies"
  # The author still uses old dependencies resolution.
  # Luckily it is "documented" in the Appveyor CI file.
  # Also in the Github CI:
  # https://github.com/meetfranz/franz/blob/master/.github/workflows/build.yml#L56
  # About the double double escape, that is because the first '--' is for
  # npm exec, while the second is for lerna.
  # See: https://github.com/lerna/lerna/issues/2921
  # npm exec --package="lerna@3.8" --yes -- lerna bootstrap
  # However, after the installation above, all works fine ;)
  npm install
}

build() {
  cd $pkgname

  _env_setup

  # Actually build the package
  echo "--> lerna run build"
  npm exec lerna run build
  echo "--> gulp build"
  npm exec gulp build
  echo "--> electron-builder"
  # Not setting locale makes sh unhappy and makes somes node-gyp fail...
  LANG=C npm exec -- electron-builder --linux dir
}

package() {
  cd $pkgname

  # Install the .asar files
  install -dm 755 "$pkgdir/usr/lib/$pkgname"
  if [[ $CARCH = aarch64 ]]; then
    cp -r --no-preserve=ownership --preserve=mode \
       out/linux-arm64-unpacked/resources "$pkgdir/usr/lib/$pkgname/"
  else
    cp -r --no-preserve=ownership --preserve=mode \
       out/linux-unpacked/resources "$pkgdir/usr/lib/$pkgname/"
  fi

  # Install icon
  install -Dm 644 "$srcdir/franz.desktop" "$pkgdir/usr/share/applications/franz.desktop"
  install -Dm 644 build-helpers/images/icon.png "$pkgdir/usr/share/icons/franz.png"

  # Install run script
  sed "s|@ELECTRON@|$_electron|" "$srcdir/franz.sh.in" > franz.sh
  install -Dm 755 franz.sh "$pkgdir/usr/bin/franz"
}
