#! /bin/sh
# sdump - dump screen image(s) to file or printer
#
# (c) 1987-2006, G.R. Detillieux, Spinal Cord Research Centre,
# The University of Manitoba.  All Rights Reserved.
#
# usage:
#    sdump [-res dpi] [-rot deg] [-inv] [-noinv] [-lines n] \
#				[-save fname] [-nosave] [file] ...
#
# If files are specified, they are assumed to be saved screens, and are spooled
# otherwise, current screen is grabbed and is either saved in a file, or spooled
# based on SDUMPSAVE variable, or -save option
#
# sdumpopt - set and save sdump options
#
# usage:
#    sdumpopt [-res dpi] [-rot deg] [-inv] [-noinv] [-lines n] \
#				[-save fname] [-nosave]
#
# The specified options are added to the user's ~/.sdumprc, which is created if
# it doesn't exist.

NEURODIR=${NEURODIR:-/usr/neuro}
if [ -x $NEURODIR/bin/lpvialpr ]
then
	lp=lpvialpr
else
	lp=lp
fi
LPOPT=
RCFILE="$HOME/.sdumprc"

# Accept SDUMP* variables from environment, or from ~/.sdumprc, if found
if [ -r $RCFILE ]
then	# get options from .sdumprc file
	. $RCFILE
fi
OLDSAVE="$SDUMPSAVE"

argc=$#
while [ $# -gt 0 ]
do
	case "$1" in
	-res)	SDUMPRES="$2";   shift ;;
	-rot)	SDUMPROT="$2";   shift ;;
	-inv)	SDUMPINV="1"     ;;
	-noinv)	SDUMPINV="0"     ;;
	-lines)	SDUMPLINES="$2"; shift ;;
	-save)	SDUMPSAVE="$2";  shift ;;
	-nosave) SDUMPSAVE=      ;;
	-)	break ;;		# end of options
	-*)	LPOPT="$LPOPT $1" ;;	# other options passed to lp
	*)	break ;;		# end of options
	esac
	shift
done

case "$0" in
*sdumpopt)
	if [ $# -gt 0 -o "$LPOPT" != "" ]
	then	# files or invalid options -> show usage
		echo "Usage:  $0 [-res dpi] [-rot deg] [-inv] [-noinv]" \
			"[-lines n] [-save fname] [-nosave]" >&2
		exit 1
	fi
	if [ $argc -gt 0 ]
	then	# save options in .sdumprc
		echo ": sdump options:
 SDUMPRES='$SDUMPRES'
 SDUMPROT='$SDUMPROT'
 SDUMPINV='$SDUMPINV'
 SDUMPLINES='$SDUMPLINES'
 SDUMPSAVE='$SDUMPSAVE'" > $RCFILE
	else	# show current options
		LINE=
		if [ "$SDUMPRES" != "" ]; then LINE="-res $SDUMPRES "; fi
		if [ "$SDUMPROT" != "" ]; then LINE="$LINE-rot $SDUMPROT "; fi
		case "$SDUMPINV" in
		0)	LINE="$LINE-noinv "	;;
		[1-9])	LINE="$LINE-inv "	;;
		esac
		if [ "$SDUMPLINES" != "" ]; then
			LINE="$LINE-lines $SDUMPLINES "
		fi
		if [ "$SDUMPSAVE" != "" ]; then
			LINE="$LINE-save $SDUMPSAVE"
		else
			LINE="$LINE-nosave"
		fi
		echo "$0 $LINE"
	fi
	exit
	;;
esac

if [ "$SDUMPRES" != "" ]; then LPOPT="$LPOPT -ores=$SDUMPRES"; fi
if [ "$SDUMPROT" != "" ]; then LPOPT="$LPOPT -orot=$SDUMPROT"; fi
if [ "$SDUMPINV" != "" ]; then LPOPT="$LPOPT -oinv=$SDUMPINV"; fi
# SDUMPLINES=${SDUMPLINES:-560}		# default: ignore last 40 lines

if [ $# -gt 0 ]
then	# dump files via lp
	exec $lp -onb -ogpsras $LPOPT $*
fi
case "$SDUMPSAVE" in
"")	# dump current screen via lp
	scrgrab $SDUMPLINES | $lp -s -onb -ogpsras $LPOPT
	;;
*)	# dump current screen to save file
	EXT=`echo "x$SDUMPSAVE" | sed -n -e 's,^.*\(\.[^./][^./]*\)$,\1,p'`
	text=`echo "$DISPLAY/$EXT" | sed 's,^..*/,y/,' | tr '[A-Z]' '[a-z]'`
	PSOPT=
	if [ "$SDUMPRES" != "" ]; then PSOPT="-dpi $SDUMPRES -equalpixels"; fi
	case "$text" in
	y/.gif)	xwd ${WINDOWID+"-id"} $WINDOWID | xwdtopnm | ppmquant 256 |
			ppmtogif > "$SDUMPSAVE" ;;
	y/.tif*) xwd ${WINDOWID+"-id"} $WINDOWID | xwdtopnm |
			pnmtotiff > "$SDUMPSAVE" ;;
	y/.ppm)	xwd ${WINDOWID+"-id"} $WINDOWID | xwdtopnm > "$SDUMPSAVE" ;;
	y/.png)	xwd ${WINDOWID+"-id"} $WINDOWID | xwdtopnm |
			pnmtopng > "$SDUMPSAVE" ;;
	y/.bmp)	xwd ${WINDOWID+"-id"} $WINDOWID | xwdtopnm |
			ppmtobmp > "$SDUMPSAVE" ;;
	y/.xpm)	xwd ${WINDOWID+"-id"} $WINDOWID | xwdtopnm |
			ppmtoxpm > "$SDUMPSAVE" ;;
	y/.jpg|y/.jpeg)	xwd ${WINDOWID+"-id"} $WINDOWID | xwdtopnm |
				pnmtojpeg > "$SDUMPSAVE" ;;
	y/.pdf)	xwd ${WINDOWID+"-id"} $WINDOWID | xwdtopnm | pnmtops $PSOPT |
			gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=- -c save pop - > "$SDUMPSAVE" ;;
	y/.ps|y/.eps)	xwd ${WINDOWID+"-id"} $WINDOWID | xwdtopnm |
				pnmtops $PSOPT > "$SDUMPSAVE" ;;
	y/.psprinter)	xwd ${WINDOWID+"-id"} $WINDOWID | xwdtopnm |
				pnmtops $PSOPT | $lp -s -onb -oraw $LPOPT
			exit ;;
	*/.gif)	scrgrab $SDUMPLINES | ras2xbm | xbmtopbm | ppmquant 256 |
			ppmtogif > "$SDUMPSAVE" ;;
	*/.tif*) scrgrab $SDUMPLINES | ras2xbm | xbmtopbm |
			pnmtotiff > "$SDUMPSAVE" ;;
	*/.ppm)	scrgrab $SDUMPLINES | ras2xbm | xbmtopbm |
			ppmquant 256 > "$SDUMPSAVE" ;;
	*/.p?m)	scrgrab $SDUMPLINES | ras2xbm | xbmtopbm > "$SDUMPSAVE" ;;
	*/.png)	scrgrab $SDUMPLINES | ras2xbm | xbmtopbm |
			pnmtopng > "$SDUMPSAVE" ;;
	*/.bmp)	scrgrab $SDUMPLINES | ras2xbm | xbmtopbm |
			ppmtobmp > "$SDUMPSAVE" ;;
	*/.xpm)	scrgrab $SDUMPLINES | ras2xbm | xbmtopbm |
			ppmtoxpm > "$SDUMPSAVE" ;;
	*/.xbm)	scrgrab $SDUMPLINES | ras2xbm > "$SDUMPSAVE" ;;
	*/.jpg|*/.jpeg)	scrgrab $SDUMPLINES | ras2xbm | xbmtopbm |
				pnmtojpeg > "$SDUMPSAVE" ;;
	*/.pdf)	scrgrab $SDUMPLINES | ras2xbm | xbmtopbm | pnmtops $PSOPT |
			gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=- -c save pop - > "$SDUMPSAVE" ;;
	*/.pdf)	scrgrab $SDUMPLINES | ras2xbm | xbmtopbm | pnmtops $PSOPT |
			gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=- -c save pop - > "$SDUMPSAVE" ;;
	*/.ps|*/.eps)	scrgrab $SDUMPLINES | ras2xbm | xbmtopbm |
				pnmtops $PSOPT > "$SDUMPSAVE" ;;
	*/.psprinter)	scrgrab $SDUMPLINES | ras2xbm | xbmtopbm |
				pnmtops $PSOPT | $lp -s -onb -oraw $LPOPT
			exit ;;
	*)	scrgrab $SDUMPLINES > "$SDUMPSAVE" ;;
	esac
	if [ "$?" -eq 0 ]
	then
	    case "$SDUMPSAVE" in
	    "$OLDSAVE")
		# increment file name, if taken from .sdumprc
		FILE=$SDUMPSAVE
		if [ "$EXT" != "" ]; then FILE=`expr "$FILE" : '\(.*\)\\'"$EXT"`; fi
		NUM=`expr '(' "x$FILE" : '.*[^0-9]0*\([1-9][0-9]*\)$' '|' \
			0 ')' + 1`
		FILE=`echo "x$FILE" | sed -e 's/\(.*[^0-9]0*\)09*$/\1/' \
					-e 's/\(.*[^0-9]0*\)[1-9][0-9]*$/\1/' \
					-e 's/^x//'`
		echo "SDUMPSAVE='$FILE$NUM$EXT'" >> $RCFILE
		;;
	    esac
	fi
	;;
esac
