#!/bin/sh
# Emulate nroff with groff.

prog="$0"
# Default device.
# First try the "locale charmap" command, because it's most reliable.
# On systems where it doesn't exist, look at the environment variables.
case "`locale charmap 2>/dev/null`" in
  UTF-8)
    T=-Tutf8 ;;
  ISO-8859-1)
    T=-Tlatin1 ;;
  IBM-1047)
    T=-Tcp1047 ;;
  EUC-JP)
    T=-Tnippon ;;
  *)
    case "${LC_ALL-${LC_CTYPE-${LANG}}}" in
      *.UTF-8)
        T=-Tutf8 ;;
      iso_8859_1 | *.ISO-8859-1)
        T=-Tlatin1 ;;
      *.IBM-1047)
        T=-Tcp1047 ;;
      ja_JP.ujis | ja_JP.eucJP)
	T=-Tnippon ;;
      *)
        case "$LESSCHARSET" in
          utf-8)
            T=-Tutf8 ;;
          latin1)
            T=-Tlatin1 ;;
          cp1047)
            T=-Tcp1047 ;;
          japanese)
	    T=-Tnippon ;;
          *)
            T=-Tascii8 ;;
          esac ;;
     esac ;;
esac
opts=

# `for i; do' doesn't work with some versions of sh

for i
  do
  case $1 in
    -c)
      opts="$opts -P-c" ;;
    -h)
      opts="$opts -P-h" ;;
    -[eq] | -s*)
      # ignore these options
      ;;
    -[mrnoT])
      echo "$prog: option $1 requires an argument" >&2
      exit 1 ;;
    -[iptSUC] | -[mrno]*)
      opts="$opts $1" ;;
    -Tascii | -Tlatin1 | -Tutf8 | -Tcp1047 | -Tascii8 | -Tnippon)
      T=$1 ;;
    -T*)
      # ignore other devices
      ;;
    -u*)
      # Solaris 2.2 `man' uses -u0; ignore it,
      # since `less' and `more' can use the emboldening info.
      ;;
    -v | --version)
      echo "GNU nroff (groff) version 1.18.1"
      exit 0 ;;
    --help)
      echo "usage: nroff [-cChipt] [-mNAME] [-nNUM] [-oLIST] [-rCN] [-Tname] [FILE...]"
      exit 0 ;;
    --)
      shift
      break ;;
    -)
      break ;;
    -*)
      echo "$prog: invalid option $1" >&2
      exit 1 ;;
    *)
      break ;;
  esac
  shift
done

# special hack to display Japanese and Czech man pages correctly in UTF-8 locale:
ICONV="cat"
case "`locale charmap 2>/dev/null`" in
    UTF-8)
    case "${LANGUAGE-${LC_ALL-${LC_MESSAGES-${LANG}}}}" in
        ja*)
        # Japanese man page in UTF-8 locale, special case!
        # force the device 'nippon' to run groff in ja_JP.eucJP locale
        # and convert the result to UTF-8 using iconv:
        T=-Tnippon
        export LC_ALL=ja_JP.eucJP
        ICONV="iconv -f EUC-JP -t UTF-8"
        ;;
        cs*|hu*|hr*|pl*)
        # Czech, Hungarian, ... in UTF-8 seem to need special treatment as well:
        T=-Tascii8
        export LC_ALL=cs_CZ.ISO-8859-2
        ICONV="iconv -f ISO-8859-2 -t UTF-8"
        ;;
        ru_RU*)
        # Russian man page sources are in KOI8-R
        T=-Tascii8
        export LC_ALL=ru_RU.KOI8-R
        ICONV="iconv -f KOI8-R -t UTF-8"
        ;;
        # make 'man iso-8859-15' display correctly in UTF-8 locales using Euro
        ca_ES*|de_AT*|de_BE*|de_DE*|de_LU*|en_BE*|en_IE*|es_ES*|eu_ES*|fi_FI*|fr_BE*|fr_FR*|fr_LU*|ga_IE*|gl_ES*|it_IT*|nl_BE*|nl_NL*|pt_PT*|sv_FI*|wa_BE*)
        T=-Tascii8
        export LC_ALL=de_DE@euro
        ICONV="iconv -f ISO-8859-15 -t UTF-8"
        ;;
    esac
    ;;
esac

# This shell script is intended for use with man, so warnings are
# probably not wanted.  Also load nroff-style character definitions.

: ${GROFF_BIN_PATH=/usr/bin}
export GROFF_BIN_PATH
PATH=$GROFF_BIN_PATH:$PATH groff -mtty-char $T $opts ${1+"$@"} | $ICONV

# eof
