#!/bin/bash
## Copyright (C) 2003-2004 Red Hat, Inc.
## Copyright (C) 2003-2004 Tim Waugh <twaugh@redhat.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., 675 Mass Ave, Cambridge, MA 02139, USA.

# 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`

MPAGE_PARAMS=-m18lr36bt
if [ "x$LANG" == "xja_JP" ]; then
  mpage -b$PAPERSIZE -1 -o $MPAGE_PARAMS -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 $MPAGE_PARAMS -P- "$INPUT" | bg5ps -en gb2312 -nps y
elif [ "x$LANG" == "xzh_TW" ]; then
  mpage -b$PAPERSIZE -1 -o $MPAGE_PARAMS -P- "$INPUT" | bg5ps -en big5 -nps y
else
  # unknown locale
  /usr/lib/cups/filter/texttops "$@"
fi
