# Maintainer (arch): Andreas Radke <andyrtr@archlinux.org>
# Maintainer: Omar Vega Ramos <ovruni@gnu.org.pe>
# Contrbutor: bill-auger <bill-auger@programmer.net>


# parabola changes and rationale:
#  - remove foomatic-db-nonfree recommendation
#  - pin to strongly-coupled dependency versions
#  - substitute 'systemd' dependency with 'udev' (your-init-freedom compat)


pkgname=cups-filters
pkgver=1.28.16
pkgrel=3
pkgrel+=.parabola1
pkgdesc="OpenPrinting CUPS Filters"
arch=('x86_64')
arch+=('armv7h' 'i686')
url="https://wiki.linuxfoundation.org/openprinting/cups-filters"
license=('custom')
depends=('lcms2' 'poppler' 'qpdf' 'ijs' 'libcups>=2.2.6-2' 'systemd' 'libexif')
depends=( ${depends[@]/systemd/udev} ) # your-init-freedom compat
makedepends=('ghostscript' 'python' 'mupdf-tools' 'liblouis')
makedepends_i686=('pkgconf')
checkdepends=('ttf-dejavu') # ttf-dejavu for make check
optdepends=('ghostscript: for non-PostScript printers to print with CUPS to convert PostScript to raster images'
	    'foomatic-db: drivers use Ghostscript to convert PostScript to a printable form directly'
	    'foomatic-db-engine: drivers use Ghostscript to convert PostScript to a printable form directly'
	    'imagemagick: for Braille embosser support'
	    'liblouis: for Braille embosser support'
	    'antiword: to convert MS Word documents for Braille embosser support'
	    'docx2txt: to convert Microsoft OOXML text from DOCX files for Braille embosser support')
backup=(etc/cups/cups-browsed.conf)
source=(https://www.openprinting.org/download/cups-filters/$pkgname-$pkgver.tar.xz)
sha256sums=('3a400bfa751da2020775cd7d48d1647448551ff051f9345abc1df6357b199ac0')


_SHOULD_SKIP_CHECK=0 # check() requires networking
(( _SHOULD_SKIP_CHECK )) && checkdepends=( ${checkdepends[*]/ttf-dejavu/} ) || :


_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}"
}


build() {
  cd "$pkgname"-$pkgver
  ./configure --prefix=/usr  \
    --sysconfdir=/etc \
    --sbindir=/usr/bin \
    --localstatedir=/var \
    --with-rcdir=no \
    --enable-avahi \
    --with-browseremoteprotocols=DNSSD,CUPS \
    --with-test-font-path=/usr/share/fonts/TTF/DejaVuSans.ttf
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
  make
}

check() {
  (( _SHOULD_SKIP_CHECK )) && return || :

  cd "$pkgname"-$pkgver
  make check
}

package() {
  # pin to strongly-coupled dependency versions
  depends=( ${depends[*]/qpdf/} $(_version_constraint qpdf 1) )
  depends+=( libpoppler.so libpoppler-cpp.so )

  cd "$pkgname"-$pkgver
  make DESTDIR="$pkgdir/" install
  
  # add upstream systemd support file
  install -Dm644 utils/cups-browsed.service "${pkgdir}"/usr/lib/systemd/system/cups-browsed.service
  sed -i "s|/usr/sbin/cups-browsed|/usr/bin/cups-browsed|" "${pkgdir}"/usr/lib/systemd/system/cups-browsed.service
  #sed -i "s|cups.service|org.cups.cupsd.service|g" "${pkgdir}"/usr/lib/systemd/system/cups-browsed.service
  
  # use cups group from cups pkg FS#56818
  chgrp -R 209 "${pkgdir}"/etc/cups

  # license
  mkdir -p "${pkgdir}"/usr/share/licenses/${pkgname}
  install -m644 "${srcdir}"/${pkgname}-${pkgver}/COPYING "${pkgdir}"/usr/share/licenses/${pkgname}/
}
