#

if ($# == 0) then
	echo "USAGE:  unravelWC raw/subdir/line_ShipName.all"
	echo "raw data must be in raw directory, sorted into subdirectories"
	echo "following subdirs are auto-generated:"
	echo "\t\t\t\tmerged"
	echo "\t\t\t\tdecnav"
	echo "\t\t\t\tmerged/subdir"
	echo "\t\t\t\tdecnav/subdir"
	exit(0)
endif

if (! (-e raw)) then
	echo "No raw directory!"
	exit(0)
endif

set FILE_COUNT = 0

foreach FILE ($*)
        set SUBDIR = `echo $FILE | sed -e 's/\// /g' | awk '{print $2}' `
        set LINE_NUMBER = `echo $FILE | sed -e 's/\// /g' | awk '{print $3}' | sed -e 's/\.all//' -e 's/\.gz//' `
        set LINE_NAME = `echo $FILE | sed -e 's/\// /g' | awk '{print $3}' | sed -e 's/\.gz//' `

	echo "Parsed" $SUBDIR " and " $LINE_NAME " and " $LINE_NUMBER "from arg " $FILE

	if (! (-e raw/$SUBDIR) ) then
		echo "No raw subdir by that name!"
		echo "raw data must be in path: raw/subdir/"
		echo `date` $SUBDIR $LINE_NUMBER "...No raw subdir by that name!" >> error.log
		continue
	endif
	
	set FOUND_ZIPPED_RAW = 0
	
	if (! (-e raw/$SUBDIR/$LINE_NAME) ) then
		echo "No raw file by that name...checking for gzip'd raw"
		if (-e raw/$SUBDIR/$LINE_NAME.gz) then
			echo "Found gzip'd file..."
			set FOUND_ZIPPED_RAW = 1
			gunzip raw/$SUBDIR/$LINE_NAME.gz
		else
			echo "No raw (or gzip'd raw) found by that name"
			echo "raw data must be in path: raw/subdir/"
			echo `date` $SUBDIR $LINE_NUMBER "...No raw (or gzip'd raw) found by that name!" >> error.log
			continue
		endif
	endif
	
	if (! (-e merged) ) then
		echo "Autogenerating merged directory..."
		mkdir merged
	endif
	
	if (! (-e decnav) ) then
		echo "Autogenerating decnav directory..."
		mkdir decnav
	endif
	
	if (! (-e ss) ) then
		echo "Autogenerating ss directory..."
		mkdir ss
	endif
	
	if (! (-e merged/$SUBDIR) ) then
		echo "Autogenerating subdirectory in merged..."
		mkdir merged/$SUBDIR
	endif
	
	if (! (-e decnav/$SUBDIR) ) then
		echo "Autogenerating subdirectory in decnav..."
		mkdir decnav/$SUBDIR
	endif
	
	if (! (-e ss/$SUBDIR) ) then
		echo "Autogenerating subdirectory in ss..."
		mkdir ss/$SUBDIR
	endif
	
	# in case you already unravelled and swathed'ed it...
	# then we dump flags from previous cleaning (and load
	# them below)
	if (-e merged/$SUBDIR/$LINE_NUMBER.merged) then
		dumpFlags merged/$SUBDIR/$LINE_NUMBER
	endif
	
	# Convert raw to OMG format...
	# Important for re-raytracing:  -v4
#		-check \
	RT \
		-v4  \
		-unpack_watercol \
		-force_swap \
		-em3002d \
		-prefix raw/$SUBDIR/ \
		-suffix .all \
		-line $LINE_NUMBER \
		-out merged/$SUBDIR/ \
		-background \
		-suppress_sk
	
	if ($?) then
		echo `date` $SUBDIR $LINE_NUMBER "...puked on RT..." >> error.log
		continue
	endif

	
	if ($FOUND_ZIPPED_RAW == 1) then
		echo "\nZipping the raw file back up!\n"
		gzip raw/$SUBDIR/$LINE_NAME
	endif
	
	# and if you had dumped the Flags, this loads the puppies back up...
	if (-e merged/$SUBDIR/$LINE_NUMBER.flag_bk) then
		dumpFlags -load merged/$SUBDIR/$LINE_NUMBER
	endif
	
	# Use nav source 1 as default navigation
	cp merged/$SUBDIR/$LINE_NUMBER.nav_1 merged/$SUBDIR/$LINE_NUMBER.nav
	
	# decimate the nav file 
	appendNav -skip 10 -comp decnav/$SUBDIR/$LINE_NUMBER.decnav merged/$SUBDIR/$LINE_NUMBER.nav
	if ($?) then
		echo `date` $SUBDIR $LINE_NUMBER "...puked on appendNav..." >> error.log
		continue
	endif
	
	# merge's attitude information, but does not raytrace
	mergeAtt merged/$SUBDIR/$LINE_NUMBER
	
	if ($?) then
		echo `date` $SUBDIR $LINE_NUMBER "...puked on mergeAtt..." >> error.log
		continue
	endif
	
	# Duh
	mergeNav \
		-ahead 0.0 \
		-right 0.0 \
		-below 0.0 \
		-declin 0.0 \
		-delay 0.0 \
		merged/$SUBDIR/$LINE_NUMBER
	
#	swathed -wide_WC merged/$SUBDIR/$LINE_NUMBER.merged &
	
	getBounds merged/$SUBDIR/$LINE_NUMBER.merged
	
#	makeSS \
#		-pixel 0.25 \
#		merged/$SUBDIR/$LINE_NUMBER.merged \
#		ss/$SUBDIR/$LINE_NUMBER.temp
		
#	if ($?) then
#		echo `date` $SUBDIR $LINE_NUMBER "...puked on makess..." >> error.log
#		continue	
#	endif
	
#	glfill \
#		ss/$SUBDIR/$LINE_NUMBER.temp \
#		ss/$SUBDIR/$LINE_NUMBER.ss
	
#	/bin/rm ss/$SUBDIR/$LINE_NUMBER.temp
	
#	jview -gl -initstr 150 230 ss/$SUBDIR/$LINE_NUMBER.ss &

#redoss $SUBDIR $LINE_NUMBER
	
	echo "Done" $SUBDIR $LINE_NUMBER "!\n"

	@ FILE_COUNT = $FILE_COUNT + 1

end

echo "\n\nUnravelled" $FILE_COUNT "of" $# "files...\n\n"

exit(0)
