#!/bin/ksh
# $Id: sgml.sh.in,v 1.4 2003/01/28 19:23:11 jmmv Exp $
#
# xmlcatmgr
# Copyright (c) 2003, Julio M. Merino Vidal
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name of the author nor the names of contributors may
#    be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

sgml_types="PUBLIC 2 ENTITY 2 DOCTYPE 2 LINKTYPE 2 NOTATION 2 OVERRIDE 1"
sgml_types="$sgml_types SYSTEM 2 SGMLDECL 1 DOCUMENT 1 CATALOG 1 BASE 1"
sgml_types="$sgml_types DELEGATE 2"

sgml_action_add() {
    type="`echo $action_arg1 | tr [a-z] [A-Z]`"

    if [ ! -f $catalog ]; then
        err "catalog $catalog does not exist"
    fi
#    if [ -n "`grep -F \"$action_arg2\" $catalog`" ]; then
#        err "entry already exists"
#    fi

    if [ "`head -n 1 $catalog`" = "<?xml version=\"1.0\"?>" ]; then
        warn "catalog has a XML header, bypassing -s..."
        xml_action_add
        return
    fi

    set -- $sgml_types
    found="no"
    while [ "$found" = "no" -a $# -gt 0 ]; do
        kw="$1"; kw_argc="$2"
        shift; shift
        if [ "$kw" = "$type" ]; then found="yes"; fi
    done

    if [ "$found" = "no" ]; then
        err "type $type not recognized"
    fi

#    if [ $(($action_argc - 1)) -ne $kw_argc ]; then
#        err "type $type requires exactly $kw_argc arguments"
#    fi

    echo "$type \"$action_arg2\"" >> $catalog
    if [ -n "$action_arg3" -a "$action_arg3" != "--" ]; then
        printf "\t\"$action_arg3\"\n" >> $catalog
    fi
    echo >> $catalog
}

sgml_action_create() {
    if [ -f $catalog ]; then
        err "catalog $catalog already exists"
    fi
    touch $catalog
}

sgml_action_destroy() {
    rm -f $catalog
}

sgml_action_remove() {
    value="$action_arg1"

    if [ ! -f $catalog ]; then
        err "catalog $catalog does not exist"
    fi
    while [ -f $catalog.tmp ]; do
        echo "catalog $catalog is beeing modified, waiting..."
        sleep 1
    done

    quotemeta value

    /usr/bin/nawk -f - $catalog > $catalog.tmp <<EOF
BEGIN { skip = 0 }

/--.*--/ {
    print
    next
}

/\"$value\"/ {
    skip = 1
    next
}

/^[a-zA-Z]/ {
    skip = 0
}

{ if (skip == 0) print }
EOF

    cp $catalog.tmp $catalog
    rm -f $catalog.tmp
}

# Local Variables: ***
# mode: shell-script ***
# End: ***
#!/bin/ksh
# $Id: xml.sh.in,v 1.3 2003/01/28 18:14:30 jmmv Exp $
#
# xmlcatmgr
# Copyright (c) 2003, Julio M. Merino Vidal
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name of the author nor the names of contributors may
#    be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

xml_action_add() {
    type="$action_arg1"

    if [ ! -f $catalog ]; then
        err "catalog $catalog does not exist"
    fi

    if [ "`head -n 1 $catalog`" != "<?xml version=\"1.0\"?>" ]; then
        warn "catalog does not have a XML header, using -s..."
        sgml_action_add
        return
    fi

    case $type in
        delegatePublic)
            entry="<delegatePublic publicIdStartString=\"$action_arg2\" catalog=\"$action_arg3\" />"
            ;;
        delegateSystem)
            entry="<delegateSystem systemIdStartString=\"$action_arg2\" catalog=\"$action_arg3\" />"
            ;;
        delegateURI)
            entry="<delegateURI uriStartString=\"$action_arg2\" catalog=\"$action_arg3\" />"
            ;;
        group)
            err "entry \`group' not implemented"
            ;;
        nextCatalog)
            entry="<nextCatalog catalog=\"$action_arg2\" />"
            ;;
        public)
            entry="<public publicId=\"$action_arg2\" uri=\"$action_arg3\" />"
            ;;
        rewriteSystem)
            entry="<rewriteSystem systemIdStartString=\"$action_arg2\" rewritePrefix=\"$action_arg3\" />"
            ;;
        rewriteURI)
            entry="<rewriteURI uriStartString=\"$action_arg2\" rewritePrefix=\"$action_arg3\" />"
            ;;
        system)
            entry="<system systemId=\"$action_arg2\" uri=\"$action_arg3\" />"
            ;;
        uri)
            entry="<uri name=\"$action_arg2\" uri=\"$action_arg3\" />"
            ;;
        *)
            err "type $type not recognized"
            ;;
    esac

    while [ -f $catalog.tmp ]; do
        echo "catalog $catalog is beeing modified, waiting..."
        sleep 1
    done

    quotemeta entry

    /usr/bin/nawk -f - $catalog > $catalog.tmp <<EOF
/<\/catalog>/ {
    print "  $entry"
    print
    exit
}

{ print }
EOF

    cp $catalog.tmp $catalog
    rm -f $catalog.tmp
}

xml_action_create() {
    if [ -f $catalog ]; then
        err "catalog $catalog already exists"
    fi
    cat > $catalog <<EOF
<?xml version="1.0"?>
<!DOCTYPE catalog PUBLIC "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN"
	"http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd">

<!-- Created by xmlcatmgr-0.2 -->

<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
</catalog>
EOF
}

xml_action_destroy() {
    rm -f $catalog
}

xml_action_remove() {
    value="$action_arg1"

    if [ ! -f $catalog ]; then
        err "catalog $catalog does not exist"
    fi
    while [ -f $catalog.tmp ]; do
        echo "catalog $catalog is beeing modified, waiting..."
        sleep 1
    done

    quotemeta value

    /usr/bin/nawk -f - $catalog > $catalog.tmp <<EOF
/\"$value\"/ {
    next
}

{ print }
EOF

    cp $catalog.tmp $catalog
    rm -f $catalog.tmp
}

# Local Variables: ***
# mode: shell-script ***
# End: ***
#!/bin/ksh
# $Id: xmlcatmgr.sh.in,v 1.8 2003/01/28 15:09:55 jmmv Exp $
#
# xmlcatmgr
# Copyright (c) 2003, Julio M. Merino Vidal
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name of the author nor the names of contributors may
#    be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

# ------------------------------------------------------------------------
# Common functions
# ------------------------------------------------------------------------

err() {
    echo "$progname: $*"
    exit 1
}

warn() {
    echo "$progname: WARNING - $*"
}

usage() {
    if [ -n "$*" ]; then
        echo "error: $*"
    fi
    echo "usage: $progname [-c catalog] [-sv] action [action_args]"
}

version() {
    echo "xmlcatmgr 0.2"
}

quotemeta() {
    qm_var="$1"
    eval qm_value=\"\$$qm_var\"

    qm_char='-e s|\/|\\\/|g'
    qm_char="$qm_char -e s/\./\\\./g"
    qm_char="$qm_char -e s/\?/\\\?/g"
    qm_char="$qm_char -e s/\*/\\\*/g"
    qm_char="$qm_char -e s/\\\"/\\\\\"/g"

    qm_value="`echo $qm_value | sed $qm_char`"
    eval $qm_var=\"\$qm_value\"
}

# ------------------------------------------------------------------------
# Main program
# ------------------------------------------------------------------------

progname="`basename $0`"
catalog="/usr/pkg/etc/xml/catalog"
type="xml"

while getopts c:sv f; do
    case $f in
        c)
            catalog="$OPTARG"
            ;;
        s)
            type="sgml"
            ;;
        v)
            version
            exit 0
            ;;
        \?)
            usage
            exit 1
            ;;
    esac
done
shift `expr $OPTIND - 1`

if [ $# -eq 0 ]; then
    usage
    exit 1
fi

action="$1"; shift
action_arg1="$1"
action_arg2="$2"
action_arg3="$3"
action_argc="$#"

case "$action" in
    add|--add)
        if [ $# -eq 0 ]; then
            usage "argument count mismatch for \`add' action"
            exit 1
        fi
        ${type}_action_add
        ;;
    create|--create)
        if [ $# -ne 0 ]; then
            usage "argument count mismatch for \`create' action"
            exit 1
        fi
        ${type}_action_create
        ;;
    destroy|--destroy)
        if [ $# -ne 0 ]; then
            usage "argument count mismatch for \`destroy' action"
            exit 1
        fi
        ${type}_action_destroy
        ;;
    remove|--remove)
        if [ $# -ne 1 ]; then
            usage "argument count mismatch for \`remove' action"
            exit 1
        fi
        ${type}_action_remove
        ;;
    *)
        echo "$progname: unknown action $action"
        ;;
esac

exit 0

# Local Variables: ***
# mode: shell-script ***
# End: ***
