# Maintainer (arch): Evangelos Foutras <foutrelis@archlinux.org>
# Maintainer: Robin Candau <antiz@archlinux.org>
# Contributor: Xavier Devlamynck <magicrhesus@ouranos.be>
# Contributor: André Silva <emulatorman@hyperbola.info>
# Maintainer: Omar Vega Ramos <ovruni@gnu.org.pe>
# Contributor: bill-auger <bill-auger@programmer.net>


# parabola changes and rationale
# - rebranded "about" modal
# - pin to strongly-coupled dependency versions
# - build from source-ball rather than VCS sources


pkgname=libxfce4ui
pkgver=4.20.2
pkgrel=1
_pkgver_i686=4.18.6
_pkgrel_i686=3.0
eval "[[ -v _pkgver_${CARCH} ]] && pkgver=\$_pkgver_${CARCH}" # our different arches do not
eval "[[ -v _pkgrel_${CARCH} ]] && pkgrel=\$_pkgrel_${CARCH}" # always roll at the same speed
pkgrel+=.parabola1
pkgdesc="Widgets library for the Xfce desktop environment"
arch=('x86_64')
arch+=('armv7h' 'i686')
url="https://docs.xfce.org/xfce/libxfce4ui/start"
license=('GPL-2.0-only')
depends=('libxfce4util' 'gtk3' 'xfconf' 'libsm' 'startup-notification'
         'libgtop' 'libepoxy' 'hicolor-icon-theme' 'libgudev')
#depends+=('libxcrypt-compat') # BR# 3643 'xfce4-whiskermenu-plugin'
makedepends=('git' 'glib2-devel' 'gobject-introspection' 'vala' 'xfce4-dev-tools')
[[ "${CARCH}" == i686 ]] && makedepends=( ${makedepends[*]/glib2-devel/} ) # n/a in arch32
makedepends=( ${makedepends[*]/git/} )                                                    # use source-ball
source=(https://archive.xfce.org/src/xfce/$pkgname/${pkgver%.*}/$pkgname-$pkgver.tar.bz2) # use source-ball
sha256sums=('5d3d67b1244a10cee0e89b045766c05fe1035f7938f0410ac6a3d8222b5df907')           # use source-ball
_sha256sum_i686='77dd99206cc8c6c7f69c269c83c7ee6a037bca9d4a89b1a6d9765e5a09ce30cd'
eval "[[ -v _sha256sum_${CARCH} ]] && sha256sums[0]=\$_sha256sum_${CARCH}" # upstream source-ball


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

  PkgVersion()
  {
    if   [[ "${dep_pkgname}" =~ / ]]
    then pacman -S --print-format='%v' ${dep_pkgname} 2> /dev/null | tail -n 1
    else pacman -Ss ^${dep_pkgname}$ | head -n 1 | cut -d ' ' -f 2
    fi
  }


  local dep_pkgname=$1
  declare -i req_precision=$2
  local full_version=$(PkgVersion)
  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 epoch_rx='[0-9]+:'
  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 epoch version pkgrel has_dot_char version_min version_max constraint_string
  declare -i subver subver_inc pkgrel_inc

  if   [[ "${full_version}" =~ ^(${epoch_rx})*(${capture_rx})(${subver_rx})*-(${pkgrel_rx}).*$ ]]
  then epoch=${BASH_REMATCH[1]}   # optional epoch
       version=${BASH_REMATCH[2]} # pkgver cut to the requested precision
       #unused=${BASH_REMATCH[3]} # discarded pkgver segments
       pkgrel=${BASH_REMATCH[4]}  # 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} )
       version=${epoch}${version}
       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() {
  # link source-ball root dir as VCS root dirname (minimize diff)
  ln -fs ${pkgname}{-${pkgver},}

  # source-ball build does not utilize ./autogen.sh
}

build() {
  cd $pkgname
  ./configure \
    --build=$CBUILD \
    --host=$CHOST \
    --target=$CTARGET \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --enable-x11 \
    --enable-wayland \
    --disable-debug \
    --enable-gtk-doc \
    --with-vendor-info='Parabola GNU/Linux-libre' \
    --enable-maintainer-mode
  make
}

check() {
  cd $pkgname
  make check
}

package() {
  # pin to current 'xfce4-session' (versions do not always align)
  depends+=( $(_version_constraint xfce4-session 3) \
             $(_version_constraint libxfce4util  3) ) # BR# 3643

  cd $pkgname
  make DESTDIR="$pkgdir" install
}
