#!/bin/bash

# Version 1.0.58f

export PATH=/usr/local/bin:/home/system/disks/s/theox/command:$PATH

while [ $# -gt 0 ]; do
    case $1 in
    --class=*)
        class="${1#*=}"
         ;;
    --in=*)
        file="${1#*=}"
        ;;
    -d)
        printer=$2
        shift
        ;;
    -t)
    	title="$2"
        shift
        ;;
    --title=*)
    	title="$2"
    	;;
    --tray1 | --tray2)
   		tray="$1"
        ;;
    --1side | --long | --short)
    	side="$1"
    	;;
    --help)
    	echo "txlp --class=class --in=in_file [--title=title] [-t title] [--tray1|--tray2] [--1side|--long|--short] -d linux_printer [lp options]"
    	exit 0
    	;;
    *)
    	echo "Unknown option $1"
    	exit 1
    	;;
    esac
    shift
done

if [ "$class" = "" ]; then
	echo "Class name missing"
	exit 1
fi

if [ "$file" = "" ]; then
	echo "File name missing"
	exit 1
fi

if [ "$printer" = "" ]; then
	echo "Printer name missing"
	exit 1
fi

echo "$title"

if [ "$class" = "pclpdf" ]; then
	# theox -> pcl6 -> postscript -> cups_driver -> cups_backend -> printer|file
	prtemul --class=$class --in="$file" $tray $side | pcl2pdf - - | lp -d $printer -t "$title"
else
	# theox -> character emulation -> cups_raw_queue -> cups_backend -> printer
	prtemul --class=$class --in="$file" $tray $side | lp -d $printer -t "$title"
fi

exit $?
