################################################################################
#
# docbook.cygclass - functions for packaging DocBook SGML/XML and DSSSL/XSL
#
# Part of cygport - Cygwin packaging application
# Copyright (C) 2006-2016 Yaakov Selkowitz
# Provided by the Cygwin Ports project <http://sourceware.org/cygwinports/>
#
# cygport is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cygport is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cygport.  If not, see <http://www.gnu.org/licenses/>.
#
################################################################################

#****h* Cygclasses/docbook.cygclass
#  SYNOPSIS
#  inherit docbook
#  DESCRIPTION
#  DocBook is a schema for SGML and XML documents meant for formatting
#  documents and books, and is sometimes used for software documentation.
#  It is generally processed through the (Open)Jade parser, either
#  directly or through the jw/docbook2* wrappers.
#
#  This cygclass handles downloading, installation and registration of
#  DocBook SGML, XML, and Simplified XML DTDs, as well as the DSSSL and
#  XSL stylesheets.
#****

my_pn=${PN%%[0-9]*}
my_p=${my_pn}-${PV}

dtd_type=${my_pn#docbook-}

DOCBOOK_SGML_CATALOG=/etc/sgml/sgml-docbook.cat
DOCBOOK_XML_CATALOG=/etc/xml/docbook
DOCBOOK_ROOT=/usr/share/sgml/docbook

case ${dtd_type} in
	dsssl|xsl*)
		DOCBOOK_DIR=${DOCBOOK_ROOT}/${dtd_type}-stylesheets
		;;
	sgml|xml*)
		DOCBOOK_DIR=${DOCBOOK_ROOT}/${dtd_type}-dtd-${PV}
		DOCBOOK_PACKAGE_CATALOG=/etc/sgml/${dtd_type}-docbook-${PV}.cat
		;;
	*)	error "docbook: Unknown DTD type: ${dtd_type}"
esac

#****o* docbook.cygclass/CATEGORY (docbook)
#  DEFINITION
CATEGORY="Text"
#****
#****o* docbook.cygclass/SUMMARY (docbook)
#  DEFINITION
SUMMARY="DocBook ${dtd_type} DTD, version ${PV}"
#****
#****o* docbook.cygclass/DESCRIPTION (docbook)
#  DEFINITION
DESCRIPTION="DocBook is a schema maintained by the DocBook Technical Committee
of OASIS. It is particularly well suited to books and papers about computer
hardware and software (though it is by no means limited to these applications).
This package provides version ${PV} of the DocBook DTDs for ${dtd_type}."
#****
#****o* docbook.cygclass/HOMEPAGE (docbook)
#  DEFINITION
HOMEPAGE="http://www.oasis-open.org/docbook/"
#****

SRC_DIR=.

#****o* docbook.cygclass/SRC_URI (docbook)
#  DESCRIPTION
#  Download location for the DocBook package.
#****
case "${dtd_type}:${PV}" in
	sgml:3.[01]*|sgml:4.[01]*)
		SRC_URI="http://www.oasis-open.org/docbook/${dtd_type//-//}/${PV}/docbk${PV//.}.zip"
		PATCH_URI="mirror://portage/app-text/${my_pn}-dtd/files/${my_pn}-dtd-${PV}-catalog.diff"
		;;
	sgml:4.[2-5])
		SRC_URI="http://www.oasis-open.org/docbook/${dtd_type//-//}/${PV}/docbook-${PV}.zip"
		PATCH_URI="mirror://portage/app-text/${my_pn}-dtd/files/${my_pn}-dtd-${PV}-catalog.diff"
		;;
	xml:4.1.2)
		SRC_URI="http://www.oasis-open.org/docbook/${dtd_type//-//}/${PV}/docbkx${PV//.}.zip"
		PATCH_URI="http://cvs.fedoraproject.org/viewvc/rpms/docbook-dtds/devel/docbook-dtd412-entities.patch?view=co"
		;;
	xml:4.[2-5])
		SRC_URI="http://www.oasis-open.org/docbook/${dtd_type//-//}/${PV}/docbook-xml-${PV}.zip"
		;;
	xml-simple:1.*)
		SRC_URI="http://www.oasis-open.org/docbook/${dtd_type//-//}/${PV}/docbook-simple-${PV}.zip
		         mirror://portage/app-text/docbook-xml-simple-dtd/files/docbook-xml-simple-dtd-${PV}.cat"
		;;
	xml-simple:4.1.2.4)
		SRC_URI="mirror://gentoo/sdb4124.zip
		         mirror://portage/app-text/docbook-xml-simple-dtd/files/docbook-xml-simple-dtd-${PV}.catalog"
		;;
	xml-simple:4.1.2.5)
		SRC_URI="http://www.oasis-open.org/docbook/${dtd_type//-//}/${PV}/simple${PV//.}.zip"
		;;
	dsssl:1.*|xsl:1*|xsl-ns:1*)
		DESCRIPTION="DocBook ${dtd_type} stylesheets"
		HOMEPAGE="http://docbook.sourceforge.net/"
		SRC_URI="mirror://sourceforge/docbook/${my_p}.tar.bz2"
		SRC_DIR="${my_p}"
		;;
	*)	error "docbook: unknown or unsupported DTD version: ${dtd_type} ${PV}" ;;
esac

unset my_pn my_p

#****I* docbook.cygclass/docbook_install
#  SYNOPSIS
#  cd $S
#  docbook_install
#  DESCRIPTION
#  Installs the DocBook package into $D.
#****
docbook_install() {
	insinto ${DOCBOOK_DIR}

	# FIXME sgml/xml-4.5 added a catalog.xml file
	case ${dtd_type} in
		dsssl)
			dobin bin/*.pl
			doman bin/*.1
			cp -r {common,dtds,frames,html,images,lib,olink,print}/ ${D}${DOCBOOK_DIR}/
			rm -f $(find ${D}${DOCBOOK_DIR}/ -name ChangeLog -o -name XREF -o -name catalog)
			doins VERSION catalog
			;;
		sgml)
			doins *.dcl *.dtd *.mod
			newins docbook.cat catalog
			;;
		xml)
			doins *.cat *.dtd *.mod
			insinto ${DOCBOOK_DIR}/ent
			doins ent/*.ent
			# use the local copy even if a URI is specified
			sed -i -e '1i\\OVERRIDE YES' ${D}${DOCBOOK_DIR}/docbook.cat
			;;
		xml-simple)
			doins *.css *.dtd *.mod
			newins docbook-xml-simple-dtd-${PV}.cat* docbook.cat
			;;
		xsl|xsl-ns)
			doins VERSION
			cp -r {common,eclipse,fo,highlighting,html*}/ ${D}${DOCBOOK_DIR}/
			cp -r {images,javahelp,lib,manpages,profiling}/ ${D}${DOCBOOK_DIR}/
			cp -r {roundtrip,slides,template,website,xhtml}/ ${D}${DOCBOOK_DIR}/
			;;
	esac
}

#****I* docbook.cygclass/docbook_dsssl_postinst
#  DESCRIPTION
#  Creates postinstall/preremove scripts to de/register the DSSSL stylesheets
#  with the system SGML catalog.
#****
docbook_dsssl_postinst() {
	dodir /etc/postinstall /etc/preremove

	cat >> ${D}/etc/postinstall/${PN}.sh <<-_EOF
		/usr/bin/install-catalog --add ${DOCBOOK_SGML_CATALOG} ${DOCBOOK_DIR}/catalog

		_EOF

	cat >> ${D}/etc/preremove/${PN}.sh <<-_EOF
		/usr/bin/install-catalog --remove ${DOCBOOK_SGML_CATALOG} ${DOCBOOK_DIR}/catalog

		_EOF
}

#****I* docbook.cygclass/docbook_sgml_postinst
#  DESCRIPTION
#  Creates postinstall/preremove scripts to de/register the SGML/XML DTDs
#  with the system SGML catalog.
#  NOTE
#  XML DTDs must also be registered in the SGML catalog.
#****
docbook_sgml_postinst() {
	local cat_name

	case ${dtd_type} in
		sgml)	cat_name="catalog" ;;
		xml*)	cat_name="docbook.cat" ;;
	esac

	dodir /etc/postinstall /etc/preremove

	cat >> ${D}/etc/postinstall/${PN}.sh <<-_EOF
		/usr/bin/install-catalog --add ${DOCBOOK_PACKAGE_CATALOG} ${DOCBOOK_SGML_CATALOG}
		/usr/bin/install-catalog --add ${DOCBOOK_PACKAGE_CATALOG} ${DOCBOOK_DIR}/${cat_name}

		_EOF

	cat >> ${D}/etc/preremove/${PN}.sh <<-_EOF
		/usr/bin/install-catalog --remove ${DOCBOOK_PACKAGE_CATALOG} ${DOCBOOK_SGML_CATALOG}
		/usr/bin/install-catalog --remove ${DOCBOOK_PACKAGE_CATALOG} ${DOCBOOK_DIR}/${cat_name}

		_EOF
}

#****I* docbook.cygclass/docbook_xml_postinst
#  DESCRIPTION
#  Creates postinstall/preremove scripts to de/register the XML DTDs or
#  XSL stylesheets with the system XML catalog.
#****
docbook_xml_postinst() {
	dodir /etc/postinstall /etc/preremove

	cat >> ${D}/etc/postinstall/${PN}.sh <<-_EOF
		/usr/bin/rm -f ${DOCBOOK_XML_CATALOG}
		/usr/bin/build-docbook-catalog

		_EOF

	cat >> ${D}/etc/preremove/${PN}.sh <<-_EOF
		/usr/bin/rm -f ${DOCBOOK_XML_CATALOG}
		/usr/bin/rm -f ${DOCBOOK_DIR}/*docbook*.dtd
		/usr/bin/build-docbook-catalog

		_EOF
}

#****o* docbook.cygclass/src_compile (docbook)
#  DEFINITION
src_compile() { :; }
#****

#****o* docbook.cygclass/src_install (docbook)
#  DESCRIPTION
#  Runs docbook_install followed by the appropriate combination of
#  docbook_dsssl_postinst, docbook_sgml_postinst, and/or docbook_xml_postinst.
#****
src_install() {
	cd ${S}
	docbook_install

	case ${dtd_type} in
		dsssl)		docbook_dsssl_postinst ;;
		sgml|xml*)	docbook_sgml_postinst ;;
	esac

	# XML DTDs need to be registered with both catalogs
	case ${dtd_type} in
		xml*|xsl*)	docbook_xml_postinst ;;
	esac
}

readonly -f docbook_install docbook_dsssl_postinst docbook_sgml_postinst \
            docbook_xml_postinst
