#!/bin/csh
# Installation c-shell script written by Swan Kim 5/1989
# The file "installcap" should exist in the current working directory
#
set curdir = `pwd`

set oldhomedir = `grep HOMEDIR installcap | awk '{print $2}'`
set oldtopdir = `grep TOPDIR installcap | awk '{print $2}'`
set oldpackagedir = `grep PACKAGEDIR installcap | awk '{print $2}'`
set oldpackagename = `grep PACKAGENAME installcap | awk '{print $2}'`
set oldpackagename = `grep PACKAGENAME installcap | awk '{print $2}'`
set oldprintername = `grep PRINTERNAME installcap | awk '{print $2}'`
set oldsunos = `grep SUNOS installcap | awk '{print $2}'`
set oldfpflags = `grep FPFLAGS installcap | awk '{print $2}'`
set oldmathlibs = `grep MATHLIBS installcap | awk '{print $2}'`

echo '------------------------------------------------------------------------------'
echo "A c-shell script for installing dynamical system classes for ${oldpackagename}"
echo "written by Swan Kim 5/1989, revised 8/1989"
echo '------------------------------------------------------------------------------'
echo ''
echo '------------------------------------------------------------------------------'
echo "current home directory: ${oldhomedir}"
echo "current top directory: ${oldtopdir}"
echo "current package name: ${oldpackagename}"
echo "current printer name: ${oldprintername}"
echo "current sun operation system: ${oldsunos}"
echo "current floating point flag: ${oldfpflags}"
echo "current math library flag: ${oldmathlibs}"
echo '------------------------------------------------------------------------------'
echo ''
echo 'NOTE: IF these are NOT what you want, please run ${oldpackagename}_install'
echo 'first and run this shell script afterwards'
echo ''

# dynamical system class related variables
set dsclasses = `grep DSCLASSES installcap`
set oldclass = `grep CURCLASS installcap| awk '{print $2}'`
set newclass = ${oldclass}
set copyclass = ${oldclass}
#a class dependent
set classfile =
#a set of suffix for class dependent files
set classfiles = (_f.c _func.c _choose_algorithm.c _${oldpackagename}_init.h _${oldpackagename}_def.h)
#header name of temporary class dependent files
set tmpname = class

echo "write temporary installcap file"
cp installcap installcap.tmp

echo ''
echo '------------------------------------------------------------------------------'
echo "Currently available dynamical system classes: ${dsclasses[2-]}"
echo "Currently selected: ${oldclass}."
echo '------------------------------------------------------------------------------'
echo ''

echo '------------------------------------------------------------------------------'
echo "Enter q or quit to quit the program at any stage"
echo '------------------------------------------------------------------------------'
echo "Enter 1 to create a new dynamical system class"
echo "Enter 2 to remove an existing dynamical system class"
echo "Enter 3 to change a currently selected dynamical system class"
echo '------------------------------------------------------------------------------'
echo ''
echo -n "What do you want? [[q]/1/2/3] "
set task = $<
echo ''
switch(${task})
	case '1':
		goto start_new_loop
		breaksw
	case '2':
		goto start_remove_loop
		breaksw
	case '3':
		goto start_choose_loop
		breaksw
	case 'q':
	case 'quit':
	default:
		goto task_done
		breaksw
endsw

start_new_loop:
	set newclass = ${oldclass}

echo -n "Create a new class with the name of: " 
set newclass = $<
switch(${newclass})
case '':
	echo "Empty name\!"
	goto start_new_loop
	breaksw
case 'q':
case 'quit':
	goto task_done
	breaksw
default:
	set found = no
	foreach tmpclass (${dsclasses[2-]})
		if($newclass == $tmpclass) then
			set found = yes
			break
		endif
	end
	if($found == 'yes') then
		echo ''
		echo "This class already exists."
		echo 'Try again.'
		echo ''
		goto start_new_loop
	else
	start_copy_loop:
		echo ''
		echo -n "Create a new class as a copy of: "
		set copyclass = $<
		set found = no
		foreach tmpclass (${dsclasses[2-]})
			if($copyclass == $tmpclass) then
				set found = yes
				break
			endif
		end
		if($found == 'yes') then
			foreach classfile ($classfiles)
				cp ${oldtopdir}/modellib/${copyclass}${classfile} ${oldtopdir}/modellib/${newclass}${classfile}
				cp ${oldtopdir}/modellib/${newclass}${classfile} ${oldtopdir}/modellib/${tmpname}${classfile}
			end
			echo ''
			echo -n "Copy done\! Writing installcap file..."
			cat > "${oldtopdir}/installcap.tmp" <<END
HOMEDIR ${oldhomedir}
PACKAGEDIR ${oldpackagedir}
TOPDIR ${oldtopdir}
PACKAGENAME ${oldpackagename}
PRINTERNAME ${oldprintername}
SUNOS ${oldsunos}
FPFLAGS ${oldfpflags}
MATHLIBS ${oldmathlibs}
DSCLASSES ${dsclasses[2-]} ${newclass}
CURCLASS ${newclass}
END
echo Done!
echo ''

		else
			echo ''
			echo "This class does not exist."
			echo 'Try again.'
			echo ''
			goto start_copy_loop
		endif
	endif
	breaksw
endsw

end_new_loop:
goto end_remove_loop


start_remove_loop:
echo -n "Remove a dynamical system class with the name of: " 
set newclass = $<
switch(${newclass})
case '':
	echo 'Empty name\!'
	goto start_remove_loop
	breaksw
case 'q':
case 'quit':
	goto task_done
	breaksw
case ${oldclass}:
	echo "To remove the currently selected dynamical system,"
	echo "change the current selection to something else and rerun this shell script"
	echo -n "Do you really want to remove the currently installed synamical system class? [yes/[no]] "
	set answer = $<		
	switch($answer)
		case 'y':
		case 'yes':
			goto start_choose_loop
			breaksw
		case '':
		case 'n':
		case 'no':
			goto start_remove_loop
			breaksw
		endsw
breaksw

default:
	set found = no
	foreach tmpclass (${dsclasses[2-]})
		if($newclass == $tmpclass) then
			set found = yes
			break
		endif
	end
	if($found == 'yes') then
		echo -n "Do you really want to remove this dynamical system class? [yes/[no]] "
		set answer = $<		
		switch($answer)
			case 'y':
			case 'yes':
				breaksw
			case '':	
			case 'n':
			case 'no':
				goto start_remove_loop
				breaksw
		endsw
		unalias rm
		rm -f ${oldtopdir}/*${newclass} ${oldtopdir}/*lib/*${newclass}
		alias rm rm -i
		cat > "${oldtopdir}/installcap.tmp" <<END
HOMEDIR ${oldhomedir}
PACKAGEDIR ${oldpackagedir}
TOPDIR ${oldtopdir}
PACKAGENAME ${oldpackagename}
PRINTERNAME ${oldprintername}
SUNOS ${oldsunos}
FPFLAGS ${oldfpflags}
MATHLIBS ${oldmathlibs}
DSCLASSES ${dsclasses[2-]}
CURCLASS ${oldclass}
END
cat installcap.tmp | sed -e "s/${newclass}//" > installcap
cp installcap installcap.tmp
echo Done!
echo ''
		goto end_remove_loop
	else
		echo "This class does not exist. Try again\!"
		echo ''
		echo '------------------------------------------------------------------------------'
		echo "Currently available dynamical system classes: ${dsclasses[2-]}"
echo '------------------------------------------------------------------------------'
		goto start_remove_loop
	endif
	breaksw
endsw

end_remove_loop:

echo ''
echo -n "Do you want to change the current selection of dynamical system classes? [yes/[no]] "
set answer = $<
switch($answer)
case 'y':
case 'yes':
	breaksw
case 'n':
case 'no':
default:
	set newclass = ${oldclass}
	goto end_choose_loop
	breaksw
endsw

#------------------------------------------------------------------------
set dsclasses = `grep DSCLASSES installcap.tmp`
echo ''
echo ''
echo "Currently avaiable dynamical system classes: ${dsclasses[2-]}"
echo ''

start_choose_loop:
echo ''
echo -n "Choose a desired class to be installed: "
set newclass = $<
switch($newclass)
default:
	set found = no
	foreach tmpclass (${dsclasses[2-]})
		if($newclass == $tmpclass) then
			set found = yes
			break
		endif
	end
	if($found == 'yes') then
		echo "Copying files..."
		foreach classfile ($classfiles)
			cp ${oldtopdir}/modellib/${copyclass}${classfile} ${oldtopdir}/modellib/${newclass}${classfile}
			cp ${oldtopdir}/modellib/${newclass}${classfile} ${oldtopdir}/modellib/${tmpname}${classfile}
		end
	else
		echo ''
		echo "This class is not found in the list."
		echo 'Try again.'
		echo ''
		goto start_choose_loop
	endif
	breaksw
endsw

echo ''
echo -n "Writing installcap file..."
cat > "${oldtopdir}/installcap.tmp" <<END
HOMEDIR ${oldhomedir}
PACKAGEDIR ${oldpackagedir}
TOPDIR ${oldtopdir}
PACKAGENAME ${oldpackagename}
PRINTERNAME ${oldprintername}
SUNOS ${oldsunos}
FPFLAGS ${oldfpflags}
MATHLIBS ${oldmathlibs}
CURCLASS ${newclass}
DSCLASSES ${dsclasses[2-]}
END
echo Done!

end_choose_loop:


echo ''
if (${newclass} == ${oldclass}) then
	echo "Newly selected class ${newclass} is the same as the old one."
	echo -n "Do you still want to compile and install the new class? [yes/[no]] "
else
	echo "Newly selected class: ${newclass} (Yet to be compiled)"
	echo "Old class: ${oldclass}   (Already compiled)"
	echo ''
	echo -n "Do you want to compile and install the new class? [yes/[no]] "
endif

set answer = $<
switch($answer)
case 'y':
case 'yes':
	breaksw
case 'n':
case 'no':
default:
	goto end_make_loop
	breaksw
endsw

start_install_loop:
echo ''
echo -n "Install the program with the name of: "
set newpackagename = $<
switch($newpackagename)
case '':
	echo 'Empty name\!'
	goto start_install_loop
	breaksw
default:

#
# acutal compilation and installation of the new dynamical system class
#
	echo ''
	echo "${newpackagename} is being installed..."
	echo ''
	echo ''
	unalias mv
	cd ${oldtopdir}
	if( -f ${oldpackagename}) then
		mv -f ${oldpackagename} ${oldpackagename}.tmp
		make ${oldpackagename}
		mv -f ${oldpackagename} ${newpackagename}
		mv -f ${oldpackagename}.tmp ${oldpackagename})
	else
		make ${oldpackagename}
		mv -f ${oldpackagename} ${newpackagename}
	endif
	cd ${curdir}
	alias mv mv -i
	breaksw
endsw

echo ''
echo Done!
echo ''

end_make_loop:

#
# move the temporary file to installcap
#
unalias mv
mv -f installcap.tmp installcap
alias mv mv -i

task_done:
echo ''
echo 'Exiting the shell script...'
echo ''
