#!/bin/bash
#
# a2ps.sh:	Simple workaround for getting a2ps handling
#		UTF-8 partly.  Partly means that iconv is
#		used to get the UTF-8 encoding into the
#		natural laint encoding of the base language
#		provided by the enviroment variable LANG.
#
# Author: Werner Fink <werner@suse.de>
#

if test "${LANG#*.}" != "UTF-8" ; then
    exec -a a2ps a2ps.bin ${1+"$@"}
fi

ENC=$(LANG=${LANG%.*}; locale charmap 2> /dev/null)
test "$ENC" = "ISO-8859-1"  && ENC=ISO-8859-15
test "${LANG%.*}" = "en_US" && ENC=ISO-8859-1

case "${ENC%-*}" in
    ISO-8859|KOI8) ;;
    *) exec -a a2ps a2ps.bin -X $ENC ${1+"$@"}
esac

#
# All long options of a2ps
#
LONG="version,help,guess,which,glob,list:,quiet,silent,verbose::,user-option:,debug,define:,\
medium:,landscape,portrait,columns:,rows:,major:,file-align:borders,margin::,line-numbers:,\
font-size:,lines-per-page:,chars-per-line:,catman,tabsize:,non-printable-format:,\
no-header,header::,underlay::,center-title::,left-title::,right-title::,left-footer::,\
footer::,right-footer::,pages::,truncate-lines,interpret,end-of-line:,encoding:,\
title:,stdin:,print-anyway,delegate,toc::,pretty-print::,highlight-level:,strip-level:,\
output:,version-control:,suffix:,printer:,prologue:,ppd::,copies:,sides:,setpagedevice:,\
statusdict:,page-prefeed,no-page-prefeed"

#
# All normal options of a2ps
#
SHORT="qv::=:D:M:,r,R,1,2,3,4,5,6,7,8,9,A:,j,C,f:,L:,l:,m,T:,B,b:,u:,a::,\
c,i,X:,t:,Z,E::,g,o:,P:,d,n:,s:,S:,k,K"

#
# We need the file names provided on the command line
# or the information if we read from stdin.
#
CMDLINE=$(getopt -o $SHORT -l $LONG -s bash -q -- ${1+"$@"})

if test $? -ne 0 ; then
   # Let a2ps do the error message
   exec -a a2ps a2ps.bin ${1+"$@"}
fi

#
# Why sed? Just to get the `=' back instead of ` ' the
# empty space which are inserted by getopt(1)
#
CMDLINE=$(echo "$CMDLINE" | LC_ALL=POSIX sed "s|--\([a-z-]\+\) \?\('\)|--\1=\2|g")

#
# Just for encoding given on command line:
# allow the user to overwrite autodetection
#
case "${CMDLINE}" in
   *-X\ \'UTF-8\'*|*--encoding=\'UTF-8\'*)
	;; # handle this below
   *-X*|*--encoding=*|*--version*|*--help*|*--which*|*--glob*|*--list=*)
	exec -a a2ps a2ps.bin ${1+"$@"}
esac

#
# To not fool the bash with eval here
#
  FILES=$(echo "$CMDLINE" | LC_ALL=POSIX sed  "s|.* -- \?\(.*\)|\1|")
CMDLINE=$(echo "$CMDLINE" | LC_ALL=POSIX sed  "s|\(.*\) -- \?.*|\1|")

test -z "$FILES" && FILES="'-'"

if test "$FILES" != "'-'" ; then
    #
    # We have real files, maybe with spaces in their path name
    #
    eval set -- "$CMDLINE"
    eval fa=("$FILES")
    for f in "${fa[@]}" ; do
	case "$(file -b "$f" 2> /dev/null)" in
	    ISO-8859*text)
		iconv -c -f ISO-8859-1 -t $ENC < "$f" | a2ps.bin -X $ENC --stdin="$f" ${1+"$@"}
		stat=$?
		;;
	    UTF-8*text)
		iconv -c -f UTF-8      -t $ENC < "$f" | a2ps.bin -X $ENC --stdin="$f" ${1+"$@"}
		stat=$?
		;;
	    ASCII*text|*)		
		a2ps.bin -X $ENC ${1+"$@"} "$f"
		stat=$?
		;;
	esac
	test $stat -eq 0 || exit $stat
    done
    exit 0
fi

#
# Just handle stdin at last but not least
#
cat - | iconv -c -f UTF-8 -t $ENC | a2ps.bin -X $ENC ${1+"$@"}
exit $?
