#!/bin/bash
#
# BASH shell script to print an image via quadtone rip printer.
which identify > /dev/null
if [ $? = "1" ]; then
    echo "'identify' command not in path. Cannot execute script."
    exit 1
fi

IMAGE=$1
QTRPRINTER=QUAD2200
PPIS="240 360 720 1440"
ORIENTATIONS="portrait landscape"
SIZES="Letter SuperB"
POSITIONS="center top-left"
BLENDS="100 90 80 70 60 50 40 30 20 10"
RESOLUTIONS="360x180sw 360sw 360swuni 720x360sw 720x360swuni 720sw 720swuni 720hq 720hquni 720hq2 1440x720sw 1440x720swuni 1440x720hq2 2880x720sw 2880x720swuni 1440x1440sw 1440x1440hq2 2880x1440sw"
ALGORITHMS="Adaptive EvenTone Ordered Fast VeryFast Floyd"
MEDIATYPES="Matte Plain PlainFast Postcard GlossyFilm Transparency Envelope BackFilm Inkjet Coated Photo GlossyPhoto Luster GlossyPaper Ilford ColorLife Other"
COOLCURVE=EEM_2200-coolSe
WARMCURVE=EEM_2200-warm
PWID="8.5"
PHGT="11.0"

#
# The file to print must exist.
#

if [ ! -e "$IMAGE" ]; then
    echo "Usage: $0 <image file>"
    exit
fi

echo "Querying image using 'identify'; please wait, this can take a few seconds."

#
# Fetch relevant image data by running identify on it.
# By passing in a format string, we can choose which data is
# returned. "man ImageMagick" provides a list of format controls
# under the "-comment" section.
#

ALLDATA=`identify -format "%w %h %x %q" $IMAGE`
WID=$( echo $ALLDATA | awk '{print $1}' )
HGT=$( echo $ALLDATA | awk '{print $2}' )
RES=$( echo $ALLDATA | awk '{print $3}' )
DEPTH=$( echo $ALLDATA | awk '{print $4}' )

# Compute "natural" width and height.
NWID=$( echo "scale=2; $WID.0 / $RES.0" | bc )
NHGT=$( echo "scale=2; $HGT.0 / $RES.0" | bc )

GOOD=

until [ $GOOD ]; do
echo "Image dimensions (w x h): "
echo "   pixels: $WID x $HGT"
echo "   inches: $NWID x $NHGT"
echo "Image resolution is $RES dpi"
echo "Image depth is $DEPTH bits"

if [ ! $DEPTH==8 ]; then
    echo "QuadToneRIP only works on 8 bit images."
    echo "*** Exiting ***"
    exit
fi

echo ""
PS3="Select a paper size:"
select size in $SIZES; do
    if [ $size = Letter ]; then
	PWID="8.5";PHGT="11.0"
	break
    elif [ $size = SuperB ]; then
	PWID="13";PHGT="19.0"
	break;
    fi
done

echo ""
PS3="Select an orientation:"
select orientation in $ORIENTATIONS; do
# orientation 4 = landscape
#             3 = portrait
    if [ $orientation = portrait ]; then
	orientation=3
	break
    elif [ $orientation = landscape ]; then
	orientation=4
	tmp=$PWID
	PWID=$PHGT
	PHGT=$tmp
	break
    else 
	echo "Invalid choice! Try again."
    fi
done

echo ""
PS3="Select a ppi (image is at $RES ppi):"
select ppi in $PPIS; do
    break
done

FWID=$( echo "scale=2; $WID.0 / $ppi.0" | bc )
FHGT=$( echo "scale=2; $HGT.0 / $ppi.0" | bc )
echo ""
echo "Paper size    ( w x h ) = $PWID x $PHGT inches."
echo "Printed image ( w x h ) = $FWID x $FHGT inches."

TOOWIDE=$( echo "$FWID > $PWID" | bc )
TOOTALL=$( echo "$FHGT > $PHGT" | bc )
if [ $TOOWIDE = "1" ]; then
    echo ""
    echo "   *** Image too wide for page ***"
    echo ""
elif [ $TOOTALL = "1" ]; then
    echo ""
    echo "   *** Image too tall for page ***"
    echo ""
else 
    GOOD=1
fi
done

echo ""
PS3="Select a position:"
select position in $POSITIONS; do
    break
done

echo ""
PS3="Select cool/warm blend:"
select blend in $BLENDS; do
    break
done

echo ""
PS3="Select a print resolution (720x1440hq2 is typical):"
select resolution in $RESOLUTIONS; do
    break
done

echo ""
PS3="Select a dithering algorithm:"
select algorithm in $ALGORITHMS; do
    break
done

echo ""
PS3="Select a media type:"
select mediatype in $MEDIATYPES; do
    break
done


COMMAND="lpr -P $QTRPRINTER \
-o job-sheets=none,none \
-o MediaType=$mediatype \
-o PageSize=$size
-o PageRegion=$size
-o ppi=$ppi \
-o position=$position \
-o cpi=12 \
-o lpi=7 \
-o page-top=0 \
-o page-bottom=0 \
-o page-left=0 \
-o page-right=0 \
-o ripBlend=$blend
-o ripCurve1=$COOLCURVE \
-o ripCurve2=$WARMCURVE \
-o Resolution=$resolution \
-o stpImageType=Continuous \
-o stpDither=$algorithm \
-o orientation-requested=$orientation \
$IMAGE"

echo ""
echo "Command line will be:"
echo $COMMAND

echo ""
echo -n "Continue (y/n)?"
read answer

if [ $answer = y ]; then
   echo "Print job is being spooled..."
   $COMMAND
   echo "Command status was $?"
else
    echo "*** No print job submitted ***"
fi






