#!/bin/sh
##
## install xhtml dtds and such
## Adapted from the install-dtds script from package docbook-xml
##  this is somewhat redundant with debian/rules but shell scripting
##  inside makefiles is painful

set -e

if [ $# -ne 1 ]; then
    echo "error: install prefix not provided" >&2
    exit 1
fi
prefix=$1

package=w3c-dtd-xhtml
install_file="install -o root -g root -m 644 -p"
make_dir="install -d -o root -g root -m 755"
xmldir=${prefix}/usr/share/xml
#dtddir=${sgmldir}/html/dtd/xhtml
dtddir=${xmldir}/xhtml/schema/dtd
# DTD dir without the prefix
dtddirrel=${dtddir#${prefix}}
#entdir=${sgmldir}/html/entities/xhtml
entdir=${xmldir}/entities/xhtml
entdirrel=${entdir#${prefix}}
sgmlconfdir=${prefix}/etc/sgml/${package}
docdir=${prefix}/usr/share/doc/${package}

echodo () {
    echo "$@"
    "$@"
}

# Look at all the subdirectories containing DTDs
for ver in `find * -type d -maxdepth 1 -name '[0-9b]*'`; do
    ${make_dir} ${dtddir}/$ver

    dir=${ver}

    if [ -d $ver/DTD ]; then
      dir=${ver}/DTD
    fi

    # Install dtds, etc. We don't need *.dcl in this case, because
    # a suitable .dcl is provided in sgml-data.
    for file in  $dir/*.dtd $dir/*.mod ; do
        if [ -f $file ]; then
            echodo ${install_file} $file ${dtddir}/$ver/
        fi
    done

    # Install SGML catalogs
    for file in $dir/catalog $dir/*.cat $dir/*.soc ; do
        if [ -f $file ]; then
	    echo "Adding $file to debian/sgmlcatalogs"
            echo ${file} ${dtddirrel}/$ver/catalog >> debian/sgmlcatalogs

	    # If there is no XML catalog, generate it
            if [ ! -f $dir/$ver/catalog.xml ]; then
		echo "Creating $dir/catalog.xml"
		debian/sotoxml.pl $file >$dir/catalog.xml
	    fi
        fi
    done

    # Create links for the .dcl files
    for file in $dir/xml1.dcl $dir/xhtml1.dcl; do
        if [ -f $file ]; then
          ln -s ../../../../declaration/xml.dcl ${dtddir}/$ver/`basename $file`
        fi
    done

    for file in $dir/xml1n.dcl ; do
        if [ -f $file ]; then
          ln -s ../../../../declaration/xml1n.dcl ${dtddir}/$ver/`basename $file`
        fi
    done

    # Install entities
    for file in $dir/*.ent ; do
        ${make_dir} ${entdir}/
        if [ -f $file ]; then
            echodo ${install_file} $file ${entdir}/
        fi
    done

    # Install examples
    for file in $ver/*.xml; do
        if [ -f $file -a $file != $ver/catalog.xml ]; then
            ${make_dir} ${docdir}/examples/$ver
            echodo ${install_file} $file ${docdir}/examples/$ver/
        fi
    done

    # Install docs
    for file in $ver/README $ver/ChangeLog $ver/*.txt; do
        if [ -f $file ]; then
            ${make_dir} ${docdir}/$ver
            echodo ${install_file} $file ${docdir}/$ver/
        fi
    done
done

# If there is a general catalog, install it
if [ -f catalog ]; then
  echo catalog ${dtddirrel}/catalog >> debian/sgmlcatalogs

  # If there is no XML catalog, generate one
  if [ ! -f catalog.xml ]; then
    echo "Creating catalog.xml"
    debian/sotoxml.pl catalog >catalog.xml
  fi

fi

# If there is an catalog for the entities, otherwise install one
if [ -f entities/catalog ]; then
  echo entities/catalog ${entdirrel}/catalog >> debian/sgmlcatalogs

  # If there no XML catalog, generate it
  if [ ! -f entities/catalog.xml ]; then
    echo "Creating entities/catalog.xml"
    debian/sotoxml.pl entities/catalog >entities/catalog.xml
  fi
fi
