# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_pyname=Sphinx
_realname=sphinx
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.8.3
pkgrel=1
pkgdesc="Python documentation generator (mingw-w64)"
arch=('any')
license=('BSD')
url="http://sphinx.pocoo.org/"
makedepends=(
            "${MINGW_PACKAGE_PREFIX}-python3-colorama"
            "${MINGW_PACKAGE_PREFIX}-python3-docutils"
            "${MINGW_PACKAGE_PREFIX}-python3-imagesize"
            "${MINGW_PACKAGE_PREFIX}-python3-jinja"
            "${MINGW_PACKAGE_PREFIX}-python3-pygments"
            "${MINGW_PACKAGE_PREFIX}-python3-requests"
            "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
            "${MINGW_PACKAGE_PREFIX}-python3-six"
            "${MINGW_PACKAGE_PREFIX}-python3-sqlalchemy"
            "${MINGW_PACKAGE_PREFIX}-python3-whoosh"
            "${MINGW_PACKAGE_PREFIX}-python2-colorama"
            "${MINGW_PACKAGE_PREFIX}-python2-docutils"
            "${MINGW_PACKAGE_PREFIX}-python2-imagesize"
            "${MINGW_PACKAGE_PREFIX}-python2-jinja"
            "${MINGW_PACKAGE_PREFIX}-python2-pygments"
            "${MINGW_PACKAGE_PREFIX}-python2-requests"
            "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
            "${MINGW_PACKAGE_PREFIX}-python2-six"
            "${MINGW_PACKAGE_PREFIX}-python2-sqlalchemy"
            "${MINGW_PACKAGE_PREFIX}-python2-whoosh"
            )
checkdepends=(
              "${MINGW_PACKAGE_PREFIX}-python3-nose"
              "${MINGW_PACKAGE_PREFIX}-python2-nose"
              "${MINGW_PACKAGE_PREFIX}-python3-snowballstemmer"
              "${MINGW_PACKAGE_PREFIX}-python2-snowballstemmer"
              "${MINGW_PACKAGE_PREFIX}-python3-babel"
              "${MINGW_PACKAGE_PREFIX}-python2-babel"
              "${MINGW_PACKAGE_PREFIX}-python3-sphinx-alabaster-theme"
              "${MINGW_PACKAGE_PREFIX}-python2-sphinx-alabaster-theme"
              "${MINGW_PACKAGE_PREFIX}-python3-sphinx_rtd_theme"
              "${MINGW_PACKAGE_PREFIX}-python2-sphinx_rtd_theme"
              "${MINGW_PACKAGE_PREFIX}-python2-mock"
              "${MINGW_PACKAGE_PREFIX}-python3-mock"
              "${MINGW_PACKAGE_PREFIX}-python2-enum34"
              #'texlive-latexextra'
              )
#_dtoken="40/45/d4a68a1f8dc669714f48d251afb4352036f87be5e9873cd27e57b9c141f0"
source=("${_realname}-${pkgver}.tar.gz"::"https://pypi.org/packages/source/S/${_pyname}/${_pyname}-${pkgver}.tar.gz")
sha256sums=('c4cb17ba44acffae3d3209646b6baec1e215cad3065e852c68cc569d4df1b9f8')

prepare() {
  # source duplication is required because makefile modify source code
  # setup.py --build tricks don't works well
  cd ${srcdir}
  for pver in {2,3}; do 
    rm -rf python${pver}-build-${CARCH} | true
    cp -r "${_pyname}-${pkgver}" "python${pver}-build-${CARCH}"
  done

  # change python2 interpreter
  find python2-build-${CARCH} -type f -exec \
  sed -i '1s,^#! \?/usr/bin/\(env \|\)python$,#!/usr/bin/python2,' {} \;
  # change sphinx-binaries name in source code
  find python2-build-${CARCH} -type f -name '*.py' -exec \
  sed -ri 's,(sphinx-(:?build|apidoc|autogen|quickstart)),\12,' {} \;

}

build() {
  cd "${srcdir}"
  for pver in {2,3}; do  
    msg "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done  
}

package_python3-sphinx() {
  pkgdesc='Python3 documentation generator (mingw-w64)'
  depends=("${MINGW_PACKAGE_PREFIX}-python3-babel"
           "${MINGW_PACKAGE_PREFIX}-python3-certifi"
           "${MINGW_PACKAGE_PREFIX}-python3-chardet"
           "${MINGW_PACKAGE_PREFIX}-python3-colorama"
           "${MINGW_PACKAGE_PREFIX}-python3-docutils"
           "${MINGW_PACKAGE_PREFIX}-python3-idna"
           "${MINGW_PACKAGE_PREFIX}-python3-imagesize"
           "${MINGW_PACKAGE_PREFIX}-python3-jinja"
           "${MINGW_PACKAGE_PREFIX}-python3-packaging"
           "${MINGW_PACKAGE_PREFIX}-python3-pygments"
           "${MINGW_PACKAGE_PREFIX}-python3-requests"
           "${MINGW_PACKAGE_PREFIX}-python3-sphinx_rtd_theme"
           "${MINGW_PACKAGE_PREFIX}-python3-snowballstemmer"
           "${MINGW_PACKAGE_PREFIX}-python3-sphinx-alabaster-theme"
           "${MINGW_PACKAGE_PREFIX}-python3-sphinxcontrib-websupport"
           "${MINGW_PACKAGE_PREFIX}-python3-six"
           "${MINGW_PACKAGE_PREFIX}-python3-sqlalchemy"
           "${MINGW_PACKAGE_PREFIX}-python3-urllib3"
           "${MINGW_PACKAGE_PREFIX}-python3-whoosh")
  #optdepends=("${MINGW_PACKAGE_PREFIX}-texlive-latexextra: for generation of PDF documentation")
  optdepends=("${MINGW_PACKAGE_PREFIX}-imagemagick: ext.imageconverter")

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" -O1

  #rm -f ${pkgdir}${MINGW_PREFIX}/bin/sphinx{.exe,-script.py,.exe.manifest}
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|/usr/bin/env |${MINGW_PREFIX}|g" \
        -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done
}

package_python2-sphinx() {
  pkgdesc='Python2 documentation generator (mingw-w64)'
  depends=("${MINGW_PACKAGE_PREFIX}-python2-babel"
           "${MINGW_PACKAGE_PREFIX}-python2-certifi"
           "${MINGW_PACKAGE_PREFIX}-python2-colorama"
           "${MINGW_PACKAGE_PREFIX}-python2-chardet"
           "${MINGW_PACKAGE_PREFIX}-python2-docutils"
           "${MINGW_PACKAGE_PREFIX}-python2-idna"
           "${MINGW_PACKAGE_PREFIX}-python2-imagesize"
           "${MINGW_PACKAGE_PREFIX}-python2-jinja"
           "${MINGW_PACKAGE_PREFIX}-python2-packaging"
           "${MINGW_PACKAGE_PREFIX}-python2-pygments"
           "${MINGW_PACKAGE_PREFIX}-python2-requests"
           "${MINGW_PACKAGE_PREFIX}-python2-sphinx_rtd_theme"
           "${MINGW_PACKAGE_PREFIX}-python2-snowballstemmer"
           "${MINGW_PACKAGE_PREFIX}-python2-sphinx-alabaster-theme"
           "${MINGW_PACKAGE_PREFIX}-python2-sphinxcontrib-websupport"
           "${MINGW_PACKAGE_PREFIX}-python2-six"
           "${MINGW_PACKAGE_PREFIX}-python2-sqlalchemy"
           "${MINGW_PACKAGE_PREFIX}-python2-urllib3"
           "${MINGW_PACKAGE_PREFIX}-python2-whoosh"
           "${MINGW_PACKAGE_PREFIX}-python2-typing")
  #optdepends=("${MINGW_PACKAGE_PREFIX}-texlive-latexextra: for generation of PDF documentation")
  optdepends=("${MINGW_PACKAGE_PREFIX}-imagemagick: ext.imageconverter")

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" -O1

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|/usr/bin/env |${MINGW_PREFIX}|g" \
        -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done
}

package_mingw-w64-i686-python2-sphinx() {
  install=${_realname}2-${CARCH}.install
  package_python2-sphinx
}

package_mingw-w64-i686-python3-sphinx() {
  install=${_realname}3-${CARCH}.install 
  package_python3-sphinx
}

package_mingw-w64-x86_64-python2-sphinx() {
  install=${_realname}2-${CARCH}.install
  package_python2-sphinx
}

package_mingw-w64-x86_64-python3-sphinx() {
  install=${_realname}3-${CARCH}.install
  package_python3-sphinx
}
