#!/bin/csh -f
# usage: fitstogif [-size size] [*.fts]

set tmpdir = /tmp/$$.tmp
mkdir $tmpdir
set tmpfts = $tmpdir/tmp.fts

onintr cleanup

if ($#argv == 0) then
    fitswindow > $tmpfts
    fitstopnm -quiet $tmpfts | ppmtogif -quiet
else
    if ("$1" == "-help") then
	echo "Usage: $0 [-size size] [*.ft[sh]]"
	exit 1
    endif

    if ("$1" == "-size") then
	set size = "$2"
	shift; shift
    else
	set size = 0
    endif

    foreach f ($argv)
	set gifname = $f:r.gif

	if ($f =~ *h) then
	    cp $f $tmpdir/x.fth
	    fdecompress -r $tmpdir/x.fth
	    set fitsf = $tmpdir/x.fts
	else
	    set fitsf = $f
	endif

	fitswindow < $fitsf > $tmpfts
	if ($size > 0) then
	    fitstopnm -quiet $tmpfts | pnmscale -xy $size $size | ppmtogif \
							    -quiet > $gifname
	else
	    fitstopnm -quiet $tmpfts | ppmtogif -quiet > $gifname
	endif
    end
endif

cleanup:

rm -fr $tmpdir
