#!/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: Tue Jun  4 12:07:22 2002
# last modified: $Date: 2006/07/13 14:50:35 $
# version: $Revision: 1.5 $

# banbun2xhtml
#    translation from banbun to xhtml document
#
# usage
#    banbun2xhtml [-o optarg] banbun.xml
#    banbun2xhtml [-o optarg] banbun.xml banbun.xhtml
#    banbun2xhtml [-o optarg] [banbun.xml banbun.xhtml]...


# 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:
    banbun2xhtml [-o optarg] banbun.xml
    banbun2xhtml [-o optarg] banbun.xml banbun.xhtml
    banbun2xhtml [-o optarg] [banbun.xml banbun.xhtml]...
     -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%.*}.xhtml
    else
        output=$1
	shift
    fi
    cmdlist="${cmdlist} -f $output"
done

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

# set valiable
#
xdtp=%PREFIX%/bin/xdtp
glsd=%XSLDIR%/toXHTML/BanBun.xsl

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

exit 1


syntax highlighted by Code2HTML, v. 0.9.1