# Maintainer (arch): Morten Linderud <foxboron@archlinux.org>
# Maintainer (arch): Lukas Fleischer <lfleischer@archlinux.org>
# Contributor: Pierre Neidhardt <ambrevar@gmail.com>
# Contributor: Florian Bruhin (The Compiler) <archlinux.org@the-compiler.org>
# Maintainer: Omar Vega Ramos <ovruni@gnu.org.pe>
# Contributor: André Silva <emulatorman@hyperbola.info>
# Contributor: bill-auger <bill-auger@programmer.net>


# parabola changes and rationale:
#   - removed 'qt5-webengine' dependency
#   - set webkit backend as default
#   - condensed the excessive webkit rant on the welcome screen
#   - pin to strongly-coupled dependency versions
#   - freeze at v2.5.x (final releases with webkit support)

# NOTE: for each qt5-base N.N.N release, re-build as a chain - build order:
#         (qt5-base-N.N.N) <- qt5-webkit <- python-pyqt5 <- qutebrowser


pkgname=qutebrowser
pkgver=2.5.4
pkgrel=2
pkgrel+=.parabola8
pkgdesc="A keyboard-driven, vim-like browser based on PyQt5"
pkgdesc+=" and QtWebKit"
arch=("any")
arch=(armv7h i686 x86_64) # per arch version constraints
url="https://www.qutebrowser.org/"
license=("GPL")
depends=("python-jinja" "python-pyqt5" "python-yaml" "qt5-base" "python-pyqtwebengine")
depends=( ${depends[*]/python-pyqtwebengine/qt5-webkit} )
makedepends=("asciidoc" "python-setuptools")
makedepends=( ${makedepends[*]/asciidoc/asciidoc>=10.2.0} ) # <10.2.0 depends on python2
optdepends=("python-adblock: adblocking backend"
            "python-pygments"
            "pdfjs: displaying PDF in-browser")
optdepends+=("gst-libav: media playback with qt5-webkit backend"
             "gst-plugins-base: media playback with qt5-webkit backend"
             "gst-plugins-good: media playback with qt5-webkit backend"
             "gst-plugins-bad: media playback with qt5-webkit backend"
             "gst-plugins-ugly: media playback with qt5-webkit backend")
options=(!emptydirs)
source=("https://github.com/qutebrowser/qutebrowser/releases/download/v$pkgver/qutebrowser-$pkgver.tar.gz"
        "https://github.com/qutebrowser/qutebrowser/releases/download/v$pkgver/qutebrowser-$pkgver.tar.gz.asc")
source+=("warning-webkit.html")
noextract=(qutebrowser-$pkgver.tar.gz)
validpgpkeys=("E04E560002401B8EF0E76F0A916EB0C8FD55A072") # Florian Bruhin
sha256sums=('a460b2202527e42a670c26d225d9fa6417d092cc1f16f3a95e7bc95dd89c1ab1'
            'SKIP')
sha256sums+=('e18216f24387b61a109611958bb59248beaf5afc90da8654e29ea8b0a7ef1610')


_version_constraint() # (dep_pkgname [precision])
{
  Log() { [[ "${FUNCNAME[2]}" == package ]] && echo "$@" >&2 || : ; }


  local dep_pkgname=$1
  declare -i req_precision=$2
  local full_version=$(pacman -S --print-format='%v' ${dep_pkgname} 2> /dev/null | tail -n 1)
  local n_dots=$(tmp=${full_version%-*} ; tmp=${tmp//[^\.]} ; echo "${#tmp}" ;)
  local def_precision=$(( n_dots + 1 ))
  local is_prec_valid=$(( req_precision > 0 && req_precision <= def_precision ))
  local precision=$((( is_prec_valid )) && echo ${req_precision} || echo ${def_precision})
  local pkgver_rx='[0-9A-Za-z_]+'
  pkgver_rx=$(sed 's|\]|\+]|' <<<${pkgver_rx}) # according to the wiki, '+' is not allowed,
                                               # but some pkgver have it (eg: 5.15.10+kde+r130)
  local subver_rx='\.'${pkgver_rx}
  local pkgrel_rx='[0-9]+'
  local garbage_rx='[^0-9].*'
  local capture_rx=${pkgver_rx}
  for (( n_dots=1 ; n_dots < precision ; ++n_dots )) ; do capture_rx+=${subver_rx} ; done ;
  local version pkgrel has_dot_char version_min version_max constraint_string
  declare -i subver subver_inc pkgrel_inc

  if   [[ "${full_version}" =~ ^(${capture_rx})(${subver_rx})*-(${pkgrel_rx}).*$ ]]
  then version=${BASH_REMATCH[1]} # pkgver cut to the requested precision
       #unused=${BASH_REMATCH[2]} # discarded pkgver segments
       pkgrel=${BASH_REMATCH[3]}  # pkgrel with non-numerics right-trimmed
       has_dot_char=$([[ "${version}" =~ \. ]] ; echo $(( ! $? )) ; )
       subver=$(sed "s|${garbage_rx}||" <<<${version##*.}) # right-trim from any non-numeric
       version=$( (( has_dot_char )) && echo ${version%.*}.${subver} || echo ${subver} )
       subver_inc=$(( subver + 1 ))
       pkgrel_inc=$(( pkgrel + 1 ))
       version_min=$(   (( ! is_prec_valid    )) && echo ${full_version%-*}-${pkgrel} || \
                                                    echo ${version}                      )
       version_max=$( ( (( ! is_prec_valid    )) && echo ${full_version%-*}-${pkgrel_inc} ) || \
                      ( [[ "${version}" =~ \. ]] && echo ${version%.*}.${subver_inc}      ) || \
                                                    echo ${subver_inc}                         )
       constraint_string="${dep_pkgname}>=${version_min} ${dep_pkgname}<${version_max}"

       Log "Applied version constraint: '${constraint_string}'"
  else Log "ERROR: in _version_constraint() parsing: dep_pkgname='${dep_pkgname}' full_version='${full_version}'"
       exit 1
  fi

  unset -f Log

  echo -n "${constraint_string}"
}


prepare() {
    # bsdtar: Pathname can't be converted from UTF-8 to current locale
    tar xzf "$srcdir"/qutebrowser-$pkgver.tar.gz

    cd "qutebrowser-$pkgver"

    cp "$srcdir"/warning-webkit.html qutebrowser/html/

    # make sure webkit is the default backend
    sed -i 's/webengine/webkit/' qutebrowser/config/configdata.yml
}

build() {
    cd "$pkgname-$pkgver"
    make -f misc/Makefile all
}

package() {
    # pin to strongly-coupled dependency versions
    depends=( $(_version_constraint python   2)                        \
              $(_version_constraint qt5-base 3) ${depends[*]%qt5-base} )

    cd "$pkgname-$pkgver"
    make -f misc/Makefile DESTDIR="$pkgdir" PREFIX=/usr install
}
