#!/bin/sh
#
# Copyright (c) 2003-2006 ONGS Inc. <info@ongs.co.jp>
# All rights reserved.
# 
# 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.
# 
# 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.

# author: Daichi GOTO (daichi@ongs.co.jp)
# author: Masanori OZAWA (ozawa@ongs.co.jp)
# first edition: Wed Jun 19 13:52:17 JST 2002
# last modified: $Date: 2006/07/13 14:50:35 $
# version: $Revision: 1.5 $

# glsd-doc2text
#    translation from document(glsd + document module) to text document
#
# usage
#    glsd-doc2text [-o optarg] document.xml
#    glsd-doc2text [-o optarg] document.xml document.txt
#    glsd-doc2text [-o optarg] [document.xml document.txt]...


# check_required_program
#     check that the required program exists
# usage
#     check_required_program program_name port_directory
check_required_program()
{
    type "$1" > /dev/null || {
        echo 'error:'
        echo "    you need $1."
        echo "    install it from $2."
        ! :
    } 1>&2
}

# usage
#    print usage
# usage
#    usage_msg='usage message'
#    usage
usage()
{
    echo "$usage_msg" 1>&2
}

# version
#    print version
# usage
#    version '$Revision: 1.5 $'
version()
{
    ver=$1
    ver=${ver#* }
    echo ${ver% $}
}

usage_msg='usage:
    glsd-doc2text [-o optarg] document.xml
    glsd-doc2text [-o optarg] document.xml document.txt
    glsd-doc2text [-o optarg] [document.xml document.txt]...
     -o             additional option
     -h             print help message
     -v             print version'

# check the option arguments
#
xdtp_addoption=""
while getopts o:hv option
do
    case "$option" in
    o)
    	xdtp_addoption="${xdtp_addoption} -o ${OPTARG}"
    	;;
    h|\?)
        usage
        exit 0
        ;;
    v)
        version '$Revision: 1.5 $'
        exit 0
        ;;
    esac
done
shift $(($OPTIND - 1))

# create command list
#
cmdlist=""

while true
do 
    if [ "$#" = "0" ]; then
	break
    fi

    if [ "${1##*.}" != "xml" ]; then
        usage; exit 1
    fi
    target=$1
    cmdlist="${cmdlist} -s $target"
    shift

    if [ "${1##*.}" = "xml" -o "${1}" = "" ]; then
        output=${target%.*}.txt
    else
        output=$1
	shift
    fi
    cmdlist="${cmdlist} -f $output"
done

check_required_program xdtp /usr/ports/textproc/xdtp/ || exit 1

# set valiable
#
xdtp=/usr/local/bin/xdtp
glsd=/usr/local/share/xdtp/toTEXT/GLSD.xsl

# do translation
#
exec ${xdtp} -t text -v 1 ${xdtp_addoption} -e getlocale -X ${glsd} \
	-m res:GLSDModule -m res:GLSDDocumentModule ${cmdlist}

exit 1


syntax highlighted by Code2HTML, v. 0.9.1