#!/bin/sh
#
# Copyright (c) 2011 Alexandre Rostovtsev <tetromino@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
#
###
#
# Process the requested compressed source nroff file and output groff
# intermediate format.
#

filename=$1

if [ -z ${filename} ] ; then
    echo "Usage: yelp-groff [FILE]" >&2
    echo "Process a man FILE and output groff intermediate format."
    exit 1
fi

# If "man -Z -Tutf8 -EUTF-8" works (i.e. if man is man-db), use that.
man -Z -Tutf8 -EUTF-8 ${filename} 2>/dev/null && exit 0

# Otherwise, manually uncompress the file ...
cat="cat"
case ${filename} in
    *.bz2)      cat="bzip2 -c -d" ;;
    *.gz)       cat="gunzip -c" ;;
    *.lzma)     cat="unlzma -c -d" ;;
    *.xz)       cat="unxz -c" ;;
    *.Z)        cat="zcat" ;;
esac

# ... and run groff to get the intermediate format; preprocess with tbl
# unless MANROFFSEQ is defined.
${cat} ${filename} | groff -${MANROFFSEQ:-t} -man -Z -Tutf8
