#!/bin/csh -f
# Change the background gray/image via pft
# C.D.lane (lane@sumex-aim.stanford.edu) 2/13/91

# Modified by Gilles Detillieux (gilles@scrc.UManitoba.CA) Jan 9-11/91:
# - fix BoundingBox handling to allow negative origin, handle missing space
# - add "0 setgray" before file copied
# - add -debug option to see generated code
# Feb 11/91:
# - change window handling so it works under 2.0

if ( $#argv == 0 ) then
	cat << EOT; exit 0
Usage background -reset
      background -gray GrayShade
      background -{file,center,scale,minscale,tile,debug} FileName

0.0 <= GrayShade <= 1.0
FileName = Encapsulated PostScript file.

Options can be shortened to just their first letter, eg. 'background -g 0.666'
The 'gray' option can also be combined with 'file', 'center' and 'minscale'
EOT
endif

while ( $#argv > 0 )
	switch ( $1 )
		case '-debug':
		case '-d' : 
			set debug; goto noarg
		case '-reset':
		case '-r':
			set mode = 'reset'; goto noarg
		case '-gray':
		case '-g':
			set gray = $2; breaksw
		case '-file':
		case '-f':
			set mode = 'file'; set file = $2; breaksw
		case '-center':
		case '-c':
			set mode = 'center'; set file = $2; breaksw
		case '-minscale':
		case '-m':
			set mode = 'minscale'; set file = $2; breaksw
		case '-scale':
		case '-s':
			set mode = 'scale'; set file = $2; breaksw
		case '-tile':
		case '-t':
			set mode = 'tile'; set file = $2; breaksw
		default:
			echo 'Unknown option:' $1; exit 1
	endsw # switch
	if ( $#argv < 2 ) then
		echo 'Missing data for option:' $1; exit 1
	endif
	shift
noarg:
	shift
end # while

if ( $?file ) then
	if ( ! -e $file || ! -r $file ) then
		echo 'Unable to find/open file:' $file; exit 1
	endif
endif

onintr exit

set noglob
set program = $0
set temp = "/tmp/$program:t.${user}.$$"; unset program
set window = 'MyBackgroundWindow'
set defaultgray = '0.333'

# GRD-feb/91:
echo "nextdict /$window known { $window 0 ne { $window termwindow nextdict /$window 0 put } if } if" > $temp

if ( $?mode ) then
	if ( $mode == 'reset' ) goto connect
endif

cat << EOT >> $temp
0 currentwindowbounds Retained window windowdeviceround
Above workspaceWindow currentwindow orderwindow
EOT

if ( ! $?gray ) set gray = $defaultgray
# GRD-feb/91:
echo "nextdict /$window currentwindow put 0 currentwindow setowner $gray setgray clippath fill" >> $temp
unset gray

if ( ! $?mode ) goto connect

# GRD-jan/91:
echo "0 setgray" >> $temp

if ( $mode == 'file' ) goto copy

# GRD-jan/91:
set bounds = `grep -e '%%BoundingBox:' $file | sed 's/^%%BoundingBox: *\(.* .* .* .*\) *$/\1/'`
echo "/xoffset $bounds[1] neg def /yoffset $bounds[2] neg def" >> $temp
echo "/width $bounds[3] xoffset add def /height $bounds[4] yoffset add def" >> $temp; unset bounds

cat << EOT >> $temp
currentwindow currentwindowbounds 4 2 roll pop pop /screenheight exch def /screenwidth exch def
EOT

if ( $mode == 'center' ) then
	echo 'screenwidth width sub 2 div screenheight height sub 2 div translate' >> $temp
else if ( $mode == 'scale' ) then
	echo 'screenwidth width div screenheight height div scale' >> $temp
else if ( $mode == 'minscale' ) then
	cat << EOT >> $temp
/min { 2 copy lt { pop } { exch pop } ifelse } def
/minscale screenwidth width div screenheight height div min def
screenwidth minscale width mul sub 2 div minscale div
screenheight minscale height mul sub 2 div minscale div
minscale dup scale translate
EOT
endif
# GRD-jan/91:
echo 'xoffset yoffset translate' >> $temp

copy:

cat $file >> $temp; unset file

if ( $mode == 'tile' ) cat << EOT >> $temp
width dup screenwidth { 0 0 width height null 6 -1 roll 0 Copy composite } for
height dup screenheight { 0 0 screenwidth height null 0 7 -1 roll Copy composite } for
EOT

connect:

# GRD-jan/91:
if ( $?debug ) then
	echo "pft -s -f $temp"; echo "Contents of '$temp':"
	cat $temp; unset debug
	goto exit
endif

pft -s -f $temp >> /dev/null

exit:

rm -f $temp; unset temp

unset noglob mode defaultgray

exit 0
