# Contributor: Sławomir Kowalski <suawekk+aur@gmail.com>
# Contributor: bill-auger <bill-auger@programmer.net>


pkgname=anki-ccbc
pkgver=0.1.0
_pyqt4topyqt5_cid='ca307cdcf66cee7f0c4838ea4ef5174b95d6b6f8'
pkgrel=1
pkgdesc="helps you remember facts (eg: foreign words/phrases) efficiently (front-end for anki flash cards)"
arch=(any)
url=https://github.com/lovac42/CCBC
license=(AGPL3)

depends=(desktop-file-utils python-beautifulsoup4 python-decorator python-httplib2
         python-markdown python-pillow python-pyaudio python-pyqt5 python-requests python-send2trash
         python-sqlalchemy qt5-webkit)
optdepends=("mplayer: for playing sounds")
provides=(anki)
conflicts=(anki)
replaces=(anki)

install=${pkgname}.install
source=(${pkgname}-${pkgver}.tar.gz::https://github.com/lovac42/CCBC/archive/v${pkgver}.tar.gz
        pyqt4topyqt5-${_pyqt4topyqt5_cid}.tar.gz::https://github.com/rferrazz/pyqt4topyqt5/archive/${_pyqt4topyqt5_cid}.tar.gz
        pyqt4topyqt5-binary-to-utf.patch)
sha512sums=('674a1bc5d4733be9f2068ffa7878fe7ad7639deab04ca3fdde12c398adfadade7d594bcd6ab3e9d8754d653a105be8c182f5aa2218ed5198934d0f1892b747ff'
            '0bdf2a7f8d58e51fd2d32d87b1ab8f91fc8a7f822b1ed29c7e22a61aebfd7768cef6a077829e0f0e0e1791c800e2c71a188be68c3a33b897f9a585c3e1a4091f'
            '4626a231cd14b86bc9af2927ac5ead5685c4c1aaf10fd0d3ca7020aeb96ec928975be7290a4dc4814ff9c70c8f8e1d80f271e45c4130d7afe6cd90ef618e740f')


prepare()
{
  mv "${srcdir}"/{CCBC,${pkgname}}-${pkgver}
  cd "${srcdir}"/${pkgname}-${pkgver}/

  ## convert pyqt4 to pyqt5 ##

  local rx='s!s = SIGNAL("triggered()")!!g; s!\(self.connect([^,]\+, \)s\([^)]\+\))!\1SIGNAL("triggered()")\2)!g'
  sed -i "${rx}" aqt/main.py
  local pyqt4to5_script=../pyqt4topyqt5-${_pyqt4topyqt5_cid}/pyqt4topyqt5.py
  local pyqt4to5_patch="${srcdir}"/pyqt4topyqt5.patch
  local success_msg="pyqt4topyqt5.py: generated and applied diff file: ${pyqt4to5_patch#${srcdir}/*}"


  ## generate pyqt4topyqt5 patch file for source package ##

  # pyqt4topyqt5 creates malformed patch files
  # https://github.com/rferrazz/pyqt4topyqt5/pull/24
  patch ${pyqt4to5_script} < "${srcdir}"/pyqt4topyqt5-binary-to-utf.patch
  python ${pyqt4to5_script} --nolog --diff=${pyqt4to5_patch} -o ../${pkgname}-qt5 . > /dev/null
  cp -a ../${pkgname}-qt5/* . ; rm -rf ../${pkgname}-qt5/ ;

  # validate and sanitize patch
  if   [[ -f ${pyqt4to5_patch} ]]
  then echo ${success_msg}
       perl -0777 -i -pe "s|^--- ${srcdir}/${pkgname}-${pkgver}/([^\t\n]*)[^\n]*\n\+\+\+ [^\n]*$|--- \1\n+++ \1|gms" ${pyqt4to5_patch}
  else echo "pyqt4topyqt5 failure" ; return 1 ;
  fi


  ## more pyqt4 to pyqt5 adaptations ##

  # QIcon and QPixmap are now in Gui
  rx='s!QtWidgets.Q\(Icon\|Pixmap\)!QtGui.Q\1!g;'
  sed -i "${rx}"                    aqt/{,forms/}*.py
  # Some enums and defaults are now somewhere else
  rx='s!QDesktopServices.storageLocation!QStandardPaths.standardLocations!g;'
  sed -i "${rx}"                    aqt/{,forms/}*.py
  rx='s!QDesktopServices.\(Documents\|Home\)\(Location\)!QStandardPaths.\1\2!g;'
  sed -i "${rx}"                    aqt/{,forms/}*.py
  # For some reason, it was fine to combine Core and Gui into Widgets
  rx='s!\(from PyQt5 import \)\(.*QtCore, QtGui\).*as.*\( QtWidgets\)!\1\2\n\1\3!g;'
  sed -i "${rx}"                    aqt/forms/*.py
  # QMessageBox is now in QtWidgets
  rx='s!\(from PyQt5\.\)\(QtGui\)\( import \*\)!\1\2\3\n\1\QtWidgets\3!g'
  sed -i "${rx}"                    aqt/qt.py
  # 'loc' is originally a list, but needs to be a string.
  # Get the last element (consider first as alternative)
  rx='s!\(if loc\[\):\(-1\] == QStandardPaths.standardLocations\)!\1\2!g; s!\(return os.path.join(loc\)\(, "Anki")\)!\1[-1]\2!g'
  sed -i "${rx}"                    aqt/profiles.py
  # cgi.escape has been depreceated in Python 3.8
  # https://github.com/posativ/isso/issues/603
  sed -i 's!cgi!html!g'             aqt/{browser.py,reviewer.py}
  sed -i 's!\(import cgi\)!\# \1!g' aqt/{browser.py,reviewer.py}


  ## tweak Makefile ##

  # defer XDG integration to .install hook
  sed -i 's!^	-xdg-mime .*!!g'        Makefile
}

package()
{
  cd "${srcdir}"/${pkgname}-${pkgver}/

  make install DESTDIR="${pkgdir}"

  # rename system meta-data files
  mv "${pkgdir}"/usr/share/{anki,${pkgname}}/
  mv "${pkgdir}"/usr/share/applications/{anki,${pkgname}}.desktop
  sed -i "s!/usr/share/anki!/usr/share/${pkgname}!" "${pkgdir}"/usr/bin/anki

  # install XDG meta-data
  install -Dm644 anki.xml "${pkgdir}"/usr/share/${pkgname}/anki.xml
}
