#!/bin/bash

# lpr-wrapper --- a wrapper around the lpr command
# License: GNU Public License

# Original code:
# Copyright (C) 2001-2004 Peter Selinger, selinger@uottawa.ca

# Complete rewrite to switch between gtklp and lpr:
# Copyright (C) 2006, Revolution Linux, Jean-Michel Dault
# jean-michel.dault@revolutionlinux.com

TMPDIR=/tmp
NAME=`basename $0`
LPR=lpr
GUILPR=gtklp
#GUILPR=kprinter

export GTKLP_SIMPLE=1

#Remove printing options
rm -f $HOME/.lpoptions
#Recreate gtklprc
mkdir -p $HOME/.gtklp
RC=$HOME/.gtklp/gtklprc
echo -e  "$PRINTER\n0\n0\n1\n1\n0\n1\n1\n0" > $RC
echo "mozffremote $0 2>/dev/null&" >> $RC
echo "http://localhost:631/sum.html#STANDARD_OPTIONS" >> $RC
echo -e "gtklpq\n0" >> $RC
#Remove KDE default printer
mkdir -p $HOME/.kde/share/config
if [ ! -e $HOME/.kde/share/config/kprinterrc ]; then
cat << EOF > $HOME/.kde/share/config/kprinterrc
[KPrinter Settings]
ApplicationOptions=
DialogReduced=false
DocDirectory=$HOME
FilterEnabled=false
PrintCommand=
Printer=

[Notification Messages]
kdeprintAutoConvert=yes
EOF
fi
perl -pi -e "s|Printer=.*|Printer=$PRINTER|;" $HOME/.kde/share/config/kprinterrc

# ensure privacy of temporary files
umask 077

# a template for temporary filenames, containing process id to allow
# easy cleanup
TEMPLATE=lpr-wrapper-$$

# make a temporary file
tmpfile () {
    mktemp $TMPDIR/$TEMPLATE.XXXXXX
}

# clean up all temp-files on exit (even after error or SIGINT)
cleanup () {
    rm -f $TMPDIR/$TEMPLATE.??????
}

trap cleanup EXIT

# quote: concatenate arguments, then escape special bash characters.

quote () {
    echo "$@" | sed 's/\\/\\\\/g;s/\"/\\\"/g;s/\$/\\\$/g;s/(/\\(/g;s/)/\\)/g;s/~/\\~/g;'s/"'"/"\\\'"/g';s/\ /\\\ /g;s/`/\\`/g;s/</\\</g;s/>/\\>/g;s/\;/\\\;/g;s/|/\\|/g;s/?/\\?/g;s/\*/\\\*/g;s/\[/\\\[/g;s/&/\\&/g;s/^$/\"\"/'
}

# ----------------------------------------------------------------------
# part 1: read command line

LPROPTS=""
PRINTER=""
FILES=""
REMOVE=""
NUMCOPIES=1

# read options
dopts () {
    while [ $# -gt 0 ]; do
	case $1 in
	-P )
	    PRINTER="$2"
	    LPROPTS="$LPROPTS `quote $1` `quote $2`"
	    shift 2
	;;
	-h | -m | -E | -l | -p )
            LPROPTS="$LPROPTS `quote $1`"
	    shift 1
	;;
	-r )
	    REMOVE=1
	    shift 1
	;;
	-C | -J | -T | -U | -i | -w | -o )
            LPROPTS="$LPROPTS `quote $1` `quote $2`"
	    shift 2
	;;
	# because of a special quirk in lpr, the following options *must*
	# pass there arguments *without* a white space to lpr.
	-1 | -2 | -3 | -4 )
	    LPROPTS="$LPROPTS `quote $1$2`"
	    shift 2
	;;
	-# )
	    NUMCOPIES="$2"
	    shift 2
	;;
	-c | -d | -f | -g | -n | -t | -v | -s )
	    echo $NAME: Warning: option $1 ignored > /dev/stderr
	    shift 1
	;;
	-T | -i | -w )
	    echo $NAME: Warning: option $1 ignored > /dev/stderr
	    shift 2
	;;
	-- )
	    shift 1
	    break
	;;
	-* )
	    echo "$NAME: invalid option: $1" > /dev/stderr
	    exit 1
	;;
	* )   
	    # getopt must be missing and this is a filename
	    break
	;;
	esac
    done

    # done with options, now read filenames

    while [ $# -gt 0 ]; do
	FILES="$FILES `quote $1`"
	shift
    done

    # end of dopts ()
}


# if getopt is available, use it to pre-process the options. If not,
# the user has to give the options in dumb format (all options first,
# with options and arguments separated by whitespace, followed by
# optional '--' and filenames)

OPTSTRING=o:P:#:K:C:J:T:U:i:1:2:3:4:w:cdfghlnmprstvE
LONGOPTS=help,version,verbose,test,ppd:,tmpdir:

if [ "$HAVE[getopt]" ]; then
    OPTIONS=`getopt -n $NAME -s bash -l $LONGOPTS -o $OPTSTRING -- $XOPTS "$@"`
    if [ $? != 0 ]; then
	echo "Try --help for more information" > /dev/stderr
	exit 1
    fi
    eval set -- "$OPTIONS"
    dopts "$@"
else
    dopts $XOPTS "$@"
fi


if [ x"$FILES" == x ]; then
    if [ -z $PRINTER ]; then
      PRT=`mktemp /tmp/printXXXXXX`
      #gxmessage "$GUILPR $LPROPTS with stdin" &
      cat > $PRT
      (cat $PRT|$GUILPR $LPROPTS;sleep 60;rm $PRT)&
    else
      PRT=`mktemp /tmp/printXXXXXX`
      #gxmessage "lpr-cups $LPROPTS with stdin" &
      cat > $PRT
      (cat $PRT|lpr-cups $LPROPTS;sleep 60;rm $PRT)&
    fi
else
    eval set -- $FILES
    while [ $# -gt 0 ]; do
	if [ ! -e "$1" ]; then
	    echo $NAME: $1: No such file or directory > /dev/stderr
	elif [ -d "$1" ]; then
	    echo $NAME: $1: Is a directory            > /dev/stderr
	elif [ ! -r "$1" ]; then
	    echo $NAME: $1: Permission denied         > /dev/stderr
	else
	    FILENAME="$1"
    	    if [ -z $PRINTER ]; then
              PRT=`mktemp /tmp/printXXXXXX`
      	      #gxmessage "$GUILPR $LPROPTS $PRT" &
	      cat $FILENAME > $PRT
	      (cat $PRT|$GUILPR $LPROPTS $PRT;sleep 60;rm $PRT)&
	    else
              PRT=`mktemp /tmp/printXXXXXX`
      	      #gxmessage "lpr-cups $LPROPTS $PRT" &
	      cat $FILENAME > $PRT
	      (cat $PRT|lpr-cups $LPROPTS $PRT;sleep 60;rm $PRT)&
	    fi
	fi
	shift
    done
fi

