#!/bin/csh
###################################################################
#		kaos tar archive script
###################################################################
#
# Tar a part or all of kaos files  onto a file:	kar [-s] [-i]
# Copy a part or all of kaos files to a directory:   kar -c
# Do the above updating c flow files:   kar [-c] -f
#
# The environmental variables $KAOSHOME and $AUTOHOME 
# should be set in .login and .cshrc before executing this program

#
# set the modes
#
# CHECK if KAOSHOME and AUTOHOME are defined !!.
#

set arg =
set copy =
set flow =

foreach arg ($argv)
	switch($arg)
		case -c*:
			set copy = on
			breaksw
		case -f*:
			set flow = on
			breaksw
		case -s*:
			set copy =
			breaksw
		default:
			breaksw
	endsw
end

if ($copy != 'on') then
	echo -n "Enter a tar file name: "
	set tarfile = $< 
	echo "Saving a part or all of kaos files onto $tarfile ..."
else
	echo -n "Enter directory to copy kaos from: "
	set fromdir = $< 
	echo -n "Enter directory to copy kaos to: "
	set todir = $< 
	if ( -d $fromdir) then
		if (-d $todir) then
			echo "Copying kaos files in directory $fromdir to directory $todir ..."
		else
			echo "$todir does not exists or is not a directory."
		endif
	else
		echo "$fromdir does not exists or is not a directory."
	endif	
endif

#
# absolute core files
#
set cfiles = (*.c *lib/*.c)
set headers = (*.h include/*.h include/*.icon *lib/*.h)
set colormaps = (cmaplib/*.128* userlib/*.128*)
#  set makefiles = (Makefile *lib/Makefile colormap/Makefile)              #   mrm (2/6/90)
set makefiles = (Makefile *lib/Makefile)
#  set scripts = (installcap kaos_install ds_install modellib/kwrite_*)    #   mrm (2/6/90)
set scripts = (installcap kaos_install dsclass_install modellib/kwrite_*)
set binfiles = (bin)
set helpfiles = (README* *lib/README* helplib/online_help*)

#
# additional files
#
set autos = (${AUTOHOME}/AUTO86)
set objs = (*.o *lib/*.o)
set libs = (*lib/*.a)
set bins = (kaos kaos_batch)
set infos = (kaos.files kaos.lines kaos.cflow*)
# temporary fix: get demonstartion files only from examples
set examples = (examples/demo.*)
set documents = (doc)
set datafiles = (data)
set psfiles = (PS)
set rasfiles = (RAS)
set archives = (archive)

start_over:
#
# groups
#
set group1 = ($cfiles $headers $colormaps $makefiles $scripts $helpfiles $binfiles)
set group2 = (${group1} $examples)
set group3 = (${group1} $examples $rasfiles)
set group4 = (${group1} $documents $infos)
set group5 = (${group1} $documents $infos $examples)
set group6 = (${group1} $documents $infos $examples $psfiles $rasfiles)
set group7 = (${group1} $objs $libs $bins)
set group8 = (${group6}  $objs $libs $bins)
set group9 = (${group6}  $objs $libs $bins)
set group10 = (${autos})
echo ""
echo "Available options are:"
echo "0: Interactive mode (Not installed yet)"
echo "1: ABSOLUTE minimum copy of source files:"
echo "2: Minimum + examples:"
echo "3: Minimum + examples,rasters:"
echo "4: Minimum + documents,info:"
echo "5: Minimum + examples,documents,info"
echo "6: Minimum + examples,documents,info,rasters,postscripts"
echo "7: Minimum + objects,libraries,binaries"	
echo "8: Minimum + examples,documents,info,rasters,postscripts"
echo "             objects,libraries,binaries"	
echo "9: Minimum + examples,documents,info,rasters,postscripts"
echo "             objects,libraries,binaries,data,archive files"	
echo "10: AUTO86"
echo ""
echo -n "Which one of the above options would you like to choose? Type number: "
set answer = $<
switch($answer)
	case 0:
		# echo start with kar -i for an interactive mode.
		echo Not installed yet. Start over.
		exit 0;
		breaksw
	case 1:
		set group = ($group1)
		breaksw
	case 2:
		set group = ($group2)
		breaksw
	case 3:
		set group = ($group3)
		breaksw
	case 4:
		set group = ($group4)
		breaksw
	case 5:
		set group = ($group5)
		breaksw
	case 6:
		set group = ($group6)
		breaksw
	case 7:
		set group = ($group7)
		breaksw
	case 8:
		set group = ($group8)
		breaksw
	default:
		echo "Incorrect answer. Try again."
		goto start_over
		breaksw
endsw

# generate flow tree in three different formats

switch($answer)
	case [4-9]:
		if ($flow == 'on') then
			echo "Generating c source flow..."
			cflow $cfiles > kaos.cflow.source
			echo "Generating reversed source flow..."
			cflow -r $cfiles > kaos.cflow.reversed
			echo "Generating object flow..."
			cflow $objs > kaos.cflow.object
			echo "Generating a files with a size of kaos and its file organizagion..."
			cat $group1 | wc > kaos.files
			ls $group1 >> kaos.files
		else
			echo "Warning: C flow chart is not updated! Rerun with -f option to update."
		endif
		breaksw	
	default:
		if ($flow == 'on') then
			echo "Updating c flow chart is not necessary fo this option."
		endif
		breaksw
endsw

if ($copy != 'on') then
	if ( -d ${KAOSHOME}) then
		cd ${KAOSHOME}
		tar cvf $tarfile $group
	else
		echo "${KAOSHOME} either is not a directory or does not exist. Exit."
		exit -1
	endif
	echo ""
	echo "Successfully saved kaos onto $tarfile."
	echo ""
else
	if ( -d ${fromdir}) then
		cd ${fromdir}; tar cf - $group | (cd $todir; tar xvfpB -)
	else
		echo "${fromdir} either is not a directory or does not exist. Exit."
		exit -1
	endif
	echo ""
	echo "Successfully saved kaos files in $fromdir onto $todir."
	echo ""
endif
