#! /bin/sh
# Emulate nroff with groff.
#
# Copyright (C) 1992-2018 Free Software Foundation, Inc.
#
# Written by James Clark.

# This file is of 'groff'.

# 'groff' is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License (GPL) as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# 'groff' 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, see <http://www.gnu.org/licenses/>.

prog="$0"

# Default device.

# Check the GROFF_TYPESETTER environment variable.
Tenv=$GROFF_TYPESETTER

# Try the 'locale charmap' command first because it is most reliable.
# On systems where it doesn't exist, look at the environment variables.
case "`exec 2>/dev/null ; locale charmap`" in
  UTF-8)
    Tloc=utf8 ;;
  ISO-8859-1 | ISO-8859-15)
    Tloc=latin1 ;;
  IBM-1047)
    Tloc=cp1047 ;;
  *)
    case "${LC_ALL-${LC_CTYPE-${LANG}}}" in
      *.UTF-8)
        Tloc=utf8 ;;
      iso_8859_1 | *.ISO-8859-1 | *.ISO8859-1 | \
      iso_8859_15 | *.ISO-8859-15 | *.ISO8859-15)
        Tloc=latin1 ;;
      *.IBM-1047)
        Tloc=cp1047 ;;
      *)
        case "$LESSCHARSET" in
          utf-8)
            Tloc=utf8 ;;
          latin1)
            Tloc=latin1 ;;
          cp1047)
            Tloc=cp1047 ;;
          *)
            Tloc=ascii ;;
        esac ;;
    esac ;;
esac

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

Topt=
opts=
for i
  do
  case $1 in
    -c)
      opts="$opts -P-c" ;;
    -h)
      opts="$opts -P-h" ;;
    -[eq] | -s*)
      # ignore these options
      ;;
    -[dMmrnoTwW])
      echo "$prog: option $1 requires an argument" >&2
      exit 1 ;;
    -[iptSUC] | -[dMmrnowW]*)
      opts="$opts $1" ;;
    -T*)
      Topt=$1 ;;
    -u*)
      # Solaris 2.2 through at least Solaris 9 'man' invokes
      # 'nroff -u0 ... | col -x'.  Ignore the -u0,
      # since 'less' and 'more' can use the emboldening info.
      # However, disable SGR, since Solaris 'col' mishandles it.
      opts="$opts -P-c" ;;
    -v | --version)
      echo "GNU nroff (groff) version 1.22.4"
      exit 0 ;;
    --help)
      echo "usage: nroff [-CchipStUv] [-dCS] [-MDIR] [-mNAME] [-nNUM] [-oLIST]"
      echo "             [-rCN] [-Tname] [-WNAME] [-wNAME] [FILE...]"
      exit 0 ;;
    --)
      shift
      break ;;
    -)
      break ;;
    -*)
      echo "$prog: invalid option $1" >&2
      exit 1 ;;
    *)
      break ;;
  esac
  shift
done

if test "x$Topt" != x ; then
  T=$Topt
else
  if test "x$Tenv" != x ; then
    T=-T$Tenv
  fi
fi

case $T in
  -Tascii | -Tlatin1 | -Tutf8 | -Tcp1047)
    ;;
  *)
    # ignore other devices and use locale fallback
    T=-T$Tloc ;;
esac

# copy the man page to a temp file
TMPFILE=$(mktemp /tmp/man.XXXXXX)
trap "rm -f $TMPFILE" 0 1 2 3 15
cat ${1+"$@"} >| ${TMPFILE}

# test the charset encoding of the man page
if iconv -f utf-8 -t utf-8 -o /dev/null ${TMPFILE} 2>/dev/null
then
  charset_in=utf-8
else
  # non utf-8 encoding
  case "${LANGUAGE-${LC_CTYPE-${LANG}}}" in
    cs*|hr*|hu*|pl*|ro*|sk*|sl*) charset_in=iso-8859-2 ;;
    el*) charset_in=iso-8859-7 ;;
    tr*) charset_in=iso-8859-9 ;;
    bg*) charset_in=cp1251 ;;
    ja*) charset_in=euc-jp ;;
    ko*) charset_in=euc-kr ;;
    ru*) charset_in=koi8-r ;;
    uk*) charset_in=koi8-u ;;
    zh_TW*|zh_HK*) charset_in=big5 ;;
    zh_CN*|zh*) charset_in=gb2312 ;;
    *) charset_in=iso-8859-1 ;;
  esac
fi


# Set up the 'GROFF_BIN_PATH' variable
# to be exported in the current 'GROFF_RUNTIME' environment.

GROFF_RUNTIME="${GROFF_BIN_PATH=/usr/bin}:"
export GROFF_BIN_PATH

# Load nroff-style character definitions too.

PATH="$GROFF_RUNTIME$PATH" cat $TMPFILE | groff -K $charset_in -mtty-char $T $opts 2>/dev/null

# eof
