#!/bin/bash

# Delegate to texttops unless we need CJK support.
grep -q '^\*% CJK' "$PPD" >/dev/null 2>&1 || \
  exec /usr/lib/cups/filter/texttops "$@"

INPUT=-
if [ $# == 6 ]
then
  INPUT=$6
fi

# for test
LANG=`echo $LANG | sed -e 's/\..*$//' -e 's/@.*$//'`

# Extract the papersize
PAPERSIZE=`grep '^\*DefaultPaperDimension' "$PPD" | cut -d\  -f2`

if [ "x$LANG" == "xja_JP" ]; then
  mpage -b$PAPERSIZE -1 -o -P- "$INPUT"
elif [ "x$LANG" == "xko_KR" ]; then
  h2ps -q -1 -p -D "$INPUT"
elif [ "x$LANG" == "xzh_CN" ]; then
  mpage -b$PAPERSIZE -1 -o -P- "$INPUT" | bg5ps -en gb2312 -nps y
elif [ "x$LANG" == "xzh_TW" ]; then
  mpage -b$PAPERSIZE -1 -o -P- "$INPUT" | bg5ps -en big5 -nps y
else
  # unknown locale
  /usr/lib/cups/filter/texttops "$@"
fi
