# Maintainer: cygSimple <cygsimple@users.sourceforge.net>
# Contributor: Alexey Pavlov <alexpux@gmail.com>

# Cross build requires specifying --host, --build and sometimes --target.
# The --build is always $CHOST as specified in /etc/makepkg.conf
# The --host will be $CHOST if --target is also specified otherwise $THOST
# The --target is specified if the binary runs as a cross tool
if [ -z "${THOST}" ]; then
  THOST=${CHOST}
fi

# The pkgbase is the controller of the templated actions
pkgbase=gettext

# The `eval'uated parts below require srcdir and pkgdir be set; but PKGBUILD
# is sourced before these are set so we must do it explicitly here.
srcdir=`pwd`/src
pkgdir=`pwd`/pkg

# Identify the package type to split the packages into.
pkgtypes=(
	'exe'
	'lib-static'
	'doc-info'
	'doc-man'
	'doc-html'
	'doc-text'
	'locale'
	#'data'
)

# We always provide a all meta package so that PACKAGE-all delivers
# every component of PACKAGE.
pkgtypes+=('all')

# If we are cross building then we cannot `make check`
# instead we create a -testing.pkg.tar with a -testing.install
# to execute the testing on the target host.
# pacman -U PACKAGE-testing.pkg.tar.xz will execute the `make check`
# after installing the /usr/src/testing/PACKAGE-PKGVER-PKGREL.
if test ! $CHOST = $THOST; then
  pkgtypes+=('testing')
  touch ${pkgbase}-testing.install
  cat <<'  EOD'>${pkgbase}-testing.install.in
post_install() {
  cd /usr/src/testing/@PKGBASE@-@PKGVER@-@PKGREL@/@PKGBLDDIR@
  make check
}

post_upgrade() {
  post_install
}

post_remove() {
  rm -rf /usr/src/testing/@PKGBASE@-@PKGVER@-@PKGREL@
}
  EOD
fi

# Then pkgname becomes an array of the concantenation of the pkgbase and the
# individual pkgtypes.
pkgname=()
for T in "${pkgtypes[@]}"; do
  pkgname+=("${pkgbase}-${T}")
done

# The pkgver comes from the upstream source distribution.
pkgver=0.19.8.1

# The pkgrel is the count of number of issues of the same pkgver.
pkgrel=1

# The pkgdesc here comes from an upstream description of the package source.
pkgdesc="GNU internationalization library"

# cygSimple is only providing x86_64
arch=('x86_64')

# The license comes from the upstream package source.
license=('GPL')

# The URL describing the package.
url="https://www.gnu.org/software/gettext"

# The source files both virtual and physical
source=(
	https://ftp.gnu.org/pub/gnu/gettext/${pkgbase}-${pkgver}.tar.gz{,.sig}
        0.19.8.1-autopoint-V.patch
        0.19.8.1-localename.patch
        0.19.8.1-no-woe32dll.patch
        0.19.8.1-tests-cygwin.patch
        0.19.8.1-cygwin-ftm.patch
        0.19.8.1-have_wprintf.patch
)

# The extra binaries should be declared here.
pkgexealsoprovides=(
	"autopoint=${pkgver}"
        "envsubst=${pkgver}"
	"gettextize=${pkgver}"
	"msgattrib=${pkgver}"
	"msgcat=${pkgver}"
	"msgcmp=${pkgver}"
	"msgcomm=${pkgver}"
	"msgconv=${pkgver}"
	"msgen=${pkgver}"
	"msgexec=${pkgver}"
	"msgfilter=${pkgver}"
	"msgfmt=${pkgver}"
	"msggrep=${pkgver}"
	"msginit=${pkgver}"
	"msgmerge=${pkgver}"
	"msgunfmt=${pkgver}"
	"msguniq=${pkgver}"
	"ngettext=${pkgver}"
	"recode-sr-latin=${pkgver}"
	"xgettext=${pkgver}"
)

# The extra libraries should be declared here.
pkglibalsoprovides=(
	"libasprintf=${pkgver}"
	"libintl=${pkgver}"
)

# The pkgconfigure is used to identify the name of the configure script.
# Not everyone uses autoconf tools.
pkgconfigure="${srcdir}/${pkgbase}-${pkgver}/configure"

# The pkgconfigoptions is used to add configure options that aren't standard.
# When specifying --target --host must be $CHOST.
pkgconfigoptions=(
	'--prefix=/usr'
	"--host=${THOST}"
	#"--host=${CHOST}"
	"--build=${CHOST}"
	#"--target=${THOST}"
	'--sysconfdir=/etc'
	'--sharedstatedir=/var'
	'--localstatedir=/var'
	'--disable-shared'
	'--enable-static'
	'--with-included-libcroco'
	'--with-included-libunistring'
	#'--with-included-libxml'
	'--with-included-glib'
	'--with-included-gettext'
	'--without-emacs'
	'--disable-java'
	'--disable-native-java'
	'--disable-csharp'
	'--disable-openmp'
	'--cache-file=config.cache'
)

# The pkgmake is used to identify the make program to use
# Not everyone likes GNU make
pkgmake=/usr/bin/make

# The pkgdestdir identifies deposition directory for creating packages.
pkgdestdir="${srcdir}/dest"

# The pkgsrcdir identifies the source directory of the package.
pkgsrcdir="${srcdir}/${pkgbase}-${pkgver}"

# The pkgblddir identifies the build directory for the source.
pkgblddir="${srcdir}/${pkgbase}-${pkgver}"

# The pkgmakeinstall is used to identify the method to use for installing
pkgmakeinstall="/usr/bin/make DESTDIR=${pkgdestdir} install"

# The pkgmakecheck is used to identify the method for testing the package.
pkgmakecheck='make check'

# The sha256sums array is in the same order as the source array, use 'SKIP'
# if the source file doesn't or shouldn't have one.
sha256sums=(
	'ff942af0e438ced4a8b0ea4b0b6e0d6d657157c5e2364de57baa279c1c125c43'
        'SKIP'
        'f984f690de13af2ef512a3df29de8dd8eb174c225f3a29825f0c707012dccc09'  #0.19.8.1-autopoint-V.patch
        'cbad20633511bcda0c586f6db12ee718269edc8dc83a85f5d106501ff26160b7'  #0.19.8.1-localename.patch
        '6dc06fea296b6688a5fdf170e396c3337610e65b42a96794b0e747417dfc7171'  #0.19.8.1-no-woe32dll.patch
        '08afe896e91c6951a36ce109e0b05dd91fe262eed9d198924d5b4c9a3ddd0da4'  #0.19.8.1-tests-cygwin.patch
        '6a1c176553ad2d1d6322e7c1ec976db83e9c909dc90dd32a49cfaad933e47a9a'  #0.19.8.1-cygwin-ftm.patch
        'e918de3294a335a425164f0c0716e1b20f8dc0c9e3bfe068a8f9f6ef263f65e6'  #0.19.8.1-have_wprintf.patch
)

# The validpgpkeys for the upstream source packages if there are any.
# You need these keys in your keystore.
validpgpkeys=(
	#'@VALIDPGPKEYS@'
)

# The make process depends on packages listed here.
makedepends=(
	#@MAKEDEPENDS@
)
checkdepends=(
	#@CHECKDEPENDS@
)

# ####################### #
#      Helper macros      #
# ####################### #
apply_patch_with_msg() {
  if [[ $1 == +([0-9]) ]]; then
    plvl=$1
    shift
  else
    plvl=1
  fi
  for F in "$@"
  do
    msg2 "NOT Applying patch file $F"
    #patch -Nbp${plvl} -i "${srcdir}/${F}"
  done
}

del_file_exists() {
  for _f in "$@"
  do
    if [ -l $_f ] || [ -f $_f ]; then
      rm -f $_f
    fi
    if [ -d $_f ]; then
      rm -rf $_f
    fi
  done
}

fn_exists() {
  test x$(type -t $1) = xfunction
}

# ####################### #
#         Hooks           #
# ####################### #
# prefix pre_ or post_ to #
#  any of the standard    #
#  functions (prepare,    #
#  build, check) to add   #
#  operations for each    #
#  step.                  #
# Create auto_conf() if   #
#  some configuration     #
#  script needs to exe-   #
#  cute to create the     #
#  configure scripts.     #
#  E.G. autoreconf -vfi   #
#                         #
# @see gettext/PKGBUILD   #
#  for examples of use.   #
# ####################### #

# Change or remove auto_conf as necessary
# autoreconf is the more common option so included for convenience
# @see gettext/PKGBUILD:auto_conf() for another example
auto_conf() {
  pushd ${pkgbase}-${pkgver} > /dev/null
    libtoolize --copy --force
    ./autogen.sh --skip-gnulib
  popd > /dev/null
}

pre_prepare() {
  pushd ${pkgbase}-${pkgver}/gettext-tools/misc > /dev/null
    if [ -e archive ]; then
      rm -rf archive
    fi
    mkdir archive
    tar -xvf archive.dir.tar -C archive
    rm -f archvie.dir.tar
  popd
}

post_prepare() {
  pushd ${pkgbase}-${pkgver}/gettext-tools/misc/archive > /dev/null
    tar -cvf ../archive.dir.tar *
  popd > /dev/null
  rm -rf archive
}

pre_build() {
  pushd ${pkgbase}-${pkgver} > /dev/null
    cat > config.cache <<'    EOF'
ac_cv_header_dirent_h=no
    EOF
  popd > /dev/null
}

post_build() {
  rm -f ${srcdir}/dest/usr/lib/charset.alias
}

# ####################### #
#     Required macros     #
# ####################### #
prepare() {
  fn_exists pre_prepare && pre_prepare || true
  # Patches are always listed in the source array.  This scheme will force the
  # patch to be at level 1 deep and the file name to end in .patch
  cd ${pkgbase}-${pkgver}
  for F in ${source[@]}; do
    if test "${F##*.}" = 'patch'; then
      apply_patch_with_msg $F
    fi
  done
  fn_exists auto_conf && auto_conf || true
  fn_exists post_prepare && post_prepare || true
}

build() {
  fn_exists pre_build && pre_build || true
  cd ${pkgbase}-${pkgver}
  $pkgconfigure \
	${pkgconfigoptions[@]}
  ${pkgmake}
  ${pkgmakeinstall}
  fn_exists post_build && post_build || true
}

check() {
  fn_exists pre_check && pre_check || true
  if test "${THOST}" = "${CHOST}"; then
    cd ${pkgbase}-${pkgver}
    ${pkgmakecheck}
  else
    msg2 "NOTE: The make check function is disabled when cross compiling."
  fi
  fn_exists post_check && post_check || true
}

# ####################### #
#      Package macros     #
# ####################### #

# The pdir variable is used because of the timing of the eval.
pdir=${pkgdir}/${pkgbase}

eval "package_${pkgbase}-all() {
  pkgdesc='${pkgbase^^} all package'
  depends=()
  for I in ${pkgtypes[@]}; do
    case \$I in
	    testing|all) ;;
	    *) depends+=(${pkgbase}-\${I}) ;;
    esac
  done
}"

eval "package_${pkgbase}-exe() {
  pkgdesc='${pkgbase^^} executable files'
  provides=('${pkgbase}=${pkgver}' ${pkgexealsoprovides[@]})
  groups=('executables')

  mkdir -p ${pdir}-exe/usr
  cp -a ${pkgdestdir}/usr/bin ${pdir}-exe/usr/
}"

eval "package_${pkgbase}-lib-static() {
  pkgdesc='${pkgbase^^} headers and static libraries'
  provides=('${pkgbase}-lib=${pkgver}' '${pkgbase}-devel=${pkgver}' ${pkglibalsoprovides[@]})
  groups=('libraries' 'libstatic')

  mkdir -p ${pdir}-lib-static/usr/share
  cp -a ${pkgdestdir}/usr/{include,lib} ${pdir}-lib-static/usr/
  if [ -e ${pkgdestdir}/usr/share/aclocal ]; then
    cp -a ${pkgdestdir}/usr/share/aclocal ${pdir}-lib-static/usr/share/
  fi
}"

eval "package_${pkgbase}-doc-info() {
  pkgdesc='${pkgbase^^} info documentation'
  groups=('documentation' 'doc-info' '${pkgbase}-doc')
  depends=('info-exe')
  options=('!strip')

  install=${pkgbase}-doc-info.install

  mkdir -p ${pdir}-doc-info/usr/share
  cp -a ${pkgdestdir}/usr/share/info ${pdir}-doc-info/usr/share/
}"

eval "package_${pkgbase}-doc-man() {
  pkgdesc='${pkgbase^^} man documentation'
  groups=('documentation' 'doc-man' '${pkgbase}-doc')
  depends=('man-exe')
  options=('!strip')

  mkdir -p ${pdir}-doc-man/usr/share
  cp -a ${pkgdestdir}/usr/share/man ${pdir}-doc-man/usr/share/
}"

eval "package_${pkgbase}-doc-html() {
  pkgdesc='${pkgbase^^} html documentation'
  groups=('documentation' 'doc-html' '${pkgbase}-doc')
  options=('!strip')

  mkdir -p ${pdir}-doc-html/usr/share/doc/html
  cp -a ${pkgdestdir}/usr/share/doc/${pkgbase} ${pdir}-doc-html/usr/share/doc/hmtl/
  rm -rf ${pdir}-doc-html/usr/share/doc/html/${pkgbase}/examples
}"

eval "package_${pkgbase}-doc-text() {
  pkgdesc='${pkgbase^^} text documentation'
  groups=('documentation' 'doc-text' '${pkgbase}-doc')
  options=('!strip')

  mkdir -p ${pdir}-doc-text/usr/share/doc/text/${pkgbase}
  cp -a ${pkgdestdir}/usr/share/doc/${pkgbase}/examples ${pdir}-doc-text/usr/share/doc/text/${pkgbase}/
}"

eval "package_${pkgbase}-locale() {
  pkgdesc='${pkgbase^^} locale message database files'
  groups=('locale')
  options=('!strip')

  mkdir -p ${pdir}-locale/usr/share
  cp -a ${pkgdestdir}/usr/share/locale ${pdir}-locale/usr/share/
}"

eval "package_${pkgbase}-data() {
  pkgdesc='${pkgbase^^} data files'
  groups=('data')
  options=('!strip')

  mkdir -p ${pdir}-data/usr/share
  cp -a ${pkgdestdir}/usr/share/${pkgbase} ${pdir}-data/usr/share/
  cp -a ${pkgdestdir}/usr/share/${pkgbase}-${pkgver} ${pdir}-data/usr/share/
}"

eval "package_${pkgbase}-testing() {
  pkgdesc='${pkgbase^^} preconfigured and built files to be used for native host testing'
  groups=('testing')
  provides=('${pkgbase}-testing=${pkgver}')
  options=('staticlibs' '!strip' 'libtool')
  install='${pkgbase}-testing.install'

  cat ${startdir}/${pkgbase}-testing.install.in | \
    sed -e 's/@PKGBASE@/${pkgbase}/' \
        -e 's/@PKGVER@/${pkgver}/' \
        -e 's/@PKGREL@/${pkgrel}/' \
        -e 's~@PKGBLDDIR@~`basename ${pkgblddir}`~' > \
	  ${startdir}/${pkgbase}-testing.install
  mkdir -p ${pdir}-testing/usr/src/testing/${pkgbase}-${pkgver}-${pkgrel}
  cp -a ${pkgsrcdir} ${pdir}-testing/usr/src/testing/${pkgbase}-${pkgver}-${pkgrel}/
  if test ! '$pkgblddir' = '$pkgsrcdir'; then
    cp -a ${pkgblddir} ${pdir}-testing/usr/src/testing/${pkgbase}-${pkgver}-${pkgrel}/
  fi
  find ${pdir}-testing/usr/src/testing -type d -name autom4te.cache -prune -exec rm -rf {} \; 
  for I in \`grep -rl ${srcdir}/ ${pdir}-testing/usr/src/testing/\`
  do
    sed -e 's~${srcdir}/~/usr/src/testing/${pkgbase}-${pkgver}-${pkgrel}/~g' \$I > \${I}.new
    mv -f \${I}.new \${I}
  done
}"
