#!/bin/sh
# Displays an UTF-8 encoded man page on an UTF-8 text terminal.
# Like "groff", except that it also works for UTF-8 encoded man pages and
# supports a wide range of Unicode characters.
# Prerequisites:
#   - Requires groff-1.18.1 or newer.
# Limitations:
#   - The only supported output devices are "utf8" and "html".
#   - For CJK languages, groff outputs lots of "Can't break line" error
#     messages. This is because groff breaks only at spaces, and Chinese,
#     Japanese, Korean, Thai etc. are written mostly without spaces.
#   - No bidi for Hebrew, Arabic and Farsi is done.
#   - No rendering for Indic languages is done.
# Notes:
#   - For Vietnamese: When using 'less'-381 for viewing, make your terminal
#     window wider than 80 columns. Otherwise 'less' will break lines too
#     early (apparently because it considers many zero-width characters as
#     being of width 1).
#   - For Vietnamese: In xterm, characters with 2 accents are rendered as a
#     hollow box when bold. As a workaround, you can use rxvt, which uses
#     inverse instead of bold.

device=
next_is_device=
for arg
do
  if test -n "$next_is_device"; then
    device=$arg
  else
    case $arg in
      -T) next_is_device=true;;
      -T*) device=`echo "X$arg" | sed -e 's,^X-T,,'`;;
    esac
  fi
  next_is_device=
done

case "$device" in
  "") echo "$0: missing device" 1>&2; exit 1;;
  utf8 | html) ;;
  *) echo "$0: unsupported device '$device'" 1>&2; exit 1;;
esac

GROFF_COMMAND_PREFIX=u8_${device}_
export GROFF_COMMAND_PREFIX
# We use -s (invocation of soelim), because it's easier to stuff the u8_prep
# pass into soelim than into troff, because the command-line option parsing of
# soelim is simpler.
groff -s "$@" | u8_${device}_postp
