# $Id$
# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>
#stolen from Archlinux with some modifications for our layout
_realname=sgml-common
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.6.3
pkgrel=3
pkgdesc='Tools for maintaining centralized SGML catalogs. (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.docbook.org/xml/'
license=('GPL')
install=sgml-common-${MSYSTEM}.install
depends=('sh')
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools")
source=("https://www.sourceware.org/pub/docbook-tools/new-trials/SOURCES/sgml-common-${pkgver}.tgz"
        'sgml-common-0.6.3-manpage-1.patch'
        'sgml-common-0.6.3-sysconfdir.patch')
noextract=("sgml-common-${pkgver}.tgz")
sha512sums=('3c4a55f555596b2a6bf6af66c497679226e7b40625ac16832150488311cba7f9fc523435eea5837262a5517f5c09c4c0e07fe4a2cf02184d4027609c26e4bc0c'
            'c584ff6e66e1d58456c07849bf96fedd3f1df0b93430f6301c4d4ab9906821dea0cbac82eada19197540b5e31f13febd04fadda5b6e2ba4b0b6ee8f0328292f7'
            '3385de6890d7370a8fa575e13aead270bd2b19687f40305867c3b208d671d3582f063109b11b0b39783a3b0e010f48e49e739cd44d11c1d5245fa6046271492a')

prepare() {
  cd $srcdir
  # work around an issue with symbolic links in the tarball.
  /usr/bin/bsdtar zxvf $srcdir/sgml-common-${pkgver}.tgz || true 
  cd "${srcdir}"/${_realname}-${pkgver}
  # The symbolic links themselves were simply linking files from /usr/share/automake to the dist folder.
  cp /usr/share/automake-1.15/install-sh .
  cp /usr/share/automake-1.15/mkinstalldirs .
  cp /usr/share/automake-1.15/missing .
  cp /usr/share/automake-1.15/INSTALL .
  cp /usr/share/automake-1.15/COPYING .
  patch -Np1 -i $srcdir/sgml-common-0.6.3-manpage-1.patch
  patch -Np1 -i $srcdir/sgml-common-0.6.3-sysconfdir.patch
  WANT_AUTOCONF=latest autoreconf -vfi
}

build() {
  cd "$srcdir"/${_realname}-${pkgver}
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  mkdir -p "${srcdir}"/build-${MSYSTEM} \
   && cp -Rf * "${srcdir}"/build-${MSYSTEM} \
   && cd "${srcdir}"/build-${MSYSTEM}
   ./configure --prefix=${MINGW_PREFIX} \
    --sysconfdir=${MINGW_PREFIX}/etc \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST}
  make
}
package(){
  cd "${srcdir}"/build-${MSYSTEM}
  make DESTDIR="$pkgdir" docdir=${MINGW_PREFIX}/share/doc install
# patch sgmlwhich so it will work with our setup
  sed -i -e "s|\/etc\/|${MINGW_PREFIX}\/etc\/|g" $pkgdir${MINGW_PREFIX}/bin/sgmlwhich

#setup our configuration for sgml/sgml/conf
  sed -i -e "s|SGML_BASE_DIR=\/usr\/share\/sgml|SGML_BASE_DIR=${MINGW_PREFIX}/share/sgml|g" \
         -e "s|SGML_CATALOGS_DIR=\/etc\/sgml|SGML_CATALOGS_DIR=${MINGW_PREFIX}\/etc\/sgml|g" \
      $pkgdir${MINGW_PREFIX}/etc/sgml/sgml.conf
  
#   "$pkgdir/usr/doc" "$pkgdir/usr/share/doc"
}

