# Maintainer: Tércio Martins <echo dGVyY2lvd2VuZGVsQGdtYWlsLmNvbQo= | base64 -d>

#Included in Slint by Didier Spaier idieratslintdotfr

pkgname=epsonscan2
pkgver=6.7.65.0
export _pkgver="$pkgver-1"
pkgrel=1slint
slackdesc="Epson scanner management utility"
url="http://support.epson.net/linux/en/epsonscan2.php"
license=('GPL-3.0-or-later')
depends=('libjpeg-turbo' 'libpng' 'libtiff' 'libusb' 'qt5-base' 'sane' 'zlib')
makedepends=('boost' 'cmake' 'qt5-singlecoreapplication' 'rapidjson')
optdepends=('epsonscan2-non-free-plugin: OCR support and wireless scanning')
options=('nosrcpack')
source=('https://download3.ebz.epson.net/dsc/f/03/00/15/87/25/a00d9b45f416c29b5b5cf569e76448b918c0eabd/epsonscan2-6.7.65.0-1.src.tar.gz'
        '0002-Fix-crash.patch'
        '0003-Use-XDG-open-to-open-the-directory.patch'
        '0004-Fix-a-crash-on-an-OOB-container-access.patch')
b2sums=('f198eecacd84d973bccfd7e8f768aeb6c1a97e3c90045c76c3c553550d5daac356e916bac09bd95ec6d9993bbfcce622317b16da9bf513f5fc66ea8f048e47d1'
        '9637be5374f6406b79b0004486764d4b6f8b3c113ac880df7c6baf87808807463627fc3d365d164ffc77644daf04d936b7a60e1f9514c36941e94c77377d2be7'
        '519ee6aaa8b395cc18249e42a4e52b9904d5d01798d47f7af33e8d0cdb96c0fdd70d750731baf8ada22cfd691b301dad6cd12d831c773da6b1953ef0ce467e21'
        '9634925263f93a6601f65b0f998ec292a35f1e109a26c2e7a44c6e129c33fc9a12e92466c98c893f6ac7052f5009efb09270cd2cb62228df6f81b019488fa12f')
doinst() {
  # Update any existing icon cache files:
  if find /usr/share/icons -maxdepth 2 2> /dev/null | grep -q icon-theme.cache ; then
    for theme_dir in /usr/share/icons/* ; do
      if [ -r ${theme_dir}/icon-theme.cache ]; then
        /usr/bin/gtk-update-icon-cache -t -f ${theme_dir} 1> /dev/null 2> /dev/null &
      fi
    done
    # This would be a large file and probably shouldn't be there.
    if [ -r /usr/share/icons/icon-theme.cache ]; then
      rm -f /usr/share/icons/icon-theme.cache
    fi
  fi
  
  # Update desktop database:
  if [ -x /usr/bin/update-desktop-database ]; then
    /usr/bin/update-desktop-database /usr/share/applications 1> /dev/null 2> /dev/null
  fi
}
build() {
  
  sed -i 's|/lib/udev|${CMAKE_INSTALL_PREFIX}/lib/udev|' \
         "$pkgname-$_pkgver/CMakeLists.txt"
  sed -i '1 i #include "zlib.h"' \
         "$pkgname-$_pkgver/src/CommonUtility/DbgLog.cpp"
  sed -i '/zlib/d' \
         "$pkgname-$_pkgver/src/Controller/CMakeLists.txt"

  # Stability improvements from Flatpak maintainers
  # https://github.com/flathub/net.epson.epsonscan2
  for file in 0002-Fix-crash \
              0003-Use-XDG-open-to-open-the-directory \
              0004-Fix-a-crash-on-an-OOB-container-access
  do
    patch --directory="$pkgname-$_pkgver" --verbose  --forward --strip=1 --input="../$file.patch" || exit
  done
  cmake $pkgname-$_pkgver \
        -B build \
        -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_INSTALL_PREFIX=/usr \
        -DQT_VERSION_MAJOR=5
        
  cmake --build build
  DESTDIR="$PKG" cmake --install build

  install -Dm644 "$pkgname-$_pkgver/desktop/rpm/i686/$pkgname.desktop" \
                 "$PKG/usr/share/applications/$pkgname.desktop"
  install -d $PKG/usr/lib/sane ; cd $PKG/usr/lib/sane
  ln -s ../$pkgname/libsane-epsonscan2.so libsane-epsonscan2.so
  ln -s ../$pkgname/libsane-epsonscan2.so libsane-epsonscan2.so.1
  ln -s ../$pkgname/libsane-epsonscan2.so libsane-epsonscan2.so.1.0.0
  mkdir -p "$PKG/usr/doc/${pkgname}-$pkgver"
  find  "$PKG"/usr/share/doc -type f|while read p; do
    cp "$p" "$PKG/usr/doc/${pkgname}-$pkgver/"
  done
  rm -r "$PKG/usr/share/doc"
}
