#! /bin/csh -f

# An apparent workaround for the striping bug...
# setenv host '/tucana/iraf/unix/'

# Usage:  imttodmd [-image|-debug|-nocrt] snapfile [dparfile]
# options:
#		<blank>	- make a solitaire, delete all files
#		-image	- make a solitaire, leaving the snap image
#		-nocrt	- make the image, but not the solitaire
#		-debug	- do everything except the solitaire, leave all files

# Shell file to make imtool solitaires.  This should be called directly
# by imtool.  For example, place these lines in your .login:
#	setenv R_RASTERFILE 'snap.%d'
#	setenv R_DISPOSE    '/ursa/iraf/extern/nlocal/lib/imttodmd %s'
# to send the snaps directly to the solitaire queue, leaving no
# intermediate files.  The necessary files will twinkle in and out of
# existence in the cwd of the imtool, typically your login directory.
# Don't use a /tmp directory or you invite name collisions with other
# users!  The R_DISPOSE code uses a system(3) call to issue a shell
# command - this would permit clever command substitution, _except_
# that the R_RASTERFILE code simply makes a file of the given format...

# Note that we don't know what the parent shell is (though typically it
# is the login shell) so we have no good place to send messages (the
# console notwithstanding).  Discard "normal" terminal output (e.g.,
# from crtpict), allow abnormal output to land where it may.
# Do we need a verbose mode?  How about a mode to leave the tmp files?

# Should probably unalias or specify the exact path for mach, etc.

# may want to pick these up from the environment
# can I source irafuser.csh in here?
set	iraf	= "/ursa/iraf/iraf"
set	nlocal	= "/ursa/iraf/extern/nlocal"

set	arch	= `mach | grep -v mc68020 || echo f68881`
set	cdpar	= "$nlocal/lib/crtpict.dpar"

# this is read by sysid in crtpict to label the bottom of the print
setenv	userid	$USER

# next best thing to a unique name, need this for wcs file management
setenv	uparm	"/tmp/$userid.$$."

set	ibin	= "$iraf/bin.$arch"
set	lbin	= "$nlocal/bin.$arch"

# delay the expansion of crtpict until it's actually used
alias	suntoiraf	"$lbin/x_nlocal.e suntoiraf"
alias	crtpict		'$ibin/x_plot.e crtpict @$cdpar device=solitaire'
alias	naskern		"$lbin/naskern.e naskern device=solitaire generic=yes"
alias	imdelete	"$ibin/x_images.e imdelete verify=no"

	switch ($1)
	case '-debug':
	    set debug
	    shift
	    breaksw
	case '-nocrt':
	    set nocrtpict	# fall through
	case '-image':
	    set nodelete
	    shift
	endsw

	switch ($#argv)
	case 2:
	    set cdpar = $2	# fall through
	case 1:
	    set snap = $1
	    breaksw
	default:
	    exit
	endsw

	set image = `echo $snap | sed 's+.ras$++'`

	if ($?debug) then
	    suntoiraf names=$snap apply_lut=yes delete=no \
		verbose=yes listonly=no yflip=no
	    crtpict input=$image'[*,-*]' output=$image.gki
	else
	    suntoiraf names=$snap apply_lut=yes delete=yes \
		verbose=no listonly=no yflip=no

	    if (! $?nocrtpict) then
		crtpict input=$image'[*,-*]' output=$image.gki >& /dev/null
		naskern input=$image.gki
		/bin/rm -f ${uparm}solitaire
		/bin/rm -f $image.gki
	    endif

	    if (! $?nodelete) then
		imdelete images=$image
	    endif
	endif
exit
