#!/bin/sh

set -e

sh_which () {
  ( IFS=: ; for p in $PATH ; do [ -x "$p/$1" ] && echo "$p/$1" ; done )
  return 0
}


db2man="`sh_which docbook2man`"
if [ "$db2man" ] ; then
    $db2man "$1"

    # Correct dash/hyphen escaping.
    if [ -f "$2" ] ; then
	sed 's/\([^\\]\)-/\1\\-/g' < "$2" > "$2.tmp"
	mv -f "$2.tmp" "$2"
    fi
    exit 0
fi

db2man="`sh_which docbook-to-man`"
if [ "$db2man" ] ; then
    $db2man "$1" > "$2"

    # Correct dash/hyphen escaping.
    if [ -f "$2" ] ; then
	sed 's/\([^\\]\)-/\1\\-/g' < "$2" > "$2.tmp"
	mv -f "$2.tmp" "$2"
    fi
    exit 0
fi

# Hmm, no docbook formatters found. Let's at least create an empty file to
# avoid problems.
touch "$2"

