#!/bin/csh
echo '################################################################'
echo '                kaos installation script'
echo ''
echo ' written by Swan Kim 5/1989, modified by Swan Kim 8/89,1/90'
echo '################################################################'
echo ' The existence of the file "installcap" is important.'
echo ' Do NOT edit this file unless you are quite sure of'
echo ' how installcap works.'
echo ' The configuration in the file "installcap" should be'
echo ' consistent with configuration in the kaos source.'
echo ' NOTE: The directory $KAOSHOME/bin should be included into'
echo ' your search path. See the "README" file in the directory'
echo ' $KAOSHOME and follow instructions there.'
echo ''
echo '   Step 1: install a kaos home directory'
echo '   Step 2: install a version number of SunOS'
echo '   Step 3: install a local printer name'
echo '   Step 4: install a floating point coprocessor type'
echo '   Step 5: install a math library type'
echo '   Step 6: make kaos programs'
echo ''
echo "Answers should be given by either of [[y,yes]]/n,no/s,skip"
echo "where the default is enclosed in [[ ]]."
echo ''

set olduserhome = `grep USERHOME installcap | awk '{print $2}'`
set oldkaoshome = `grep KAOSHOME installcap | awk '{print $2}'`

set oldsunos = `grep SUNOS installcap | awk '{print $2}'`
set oldprintername = `grep PRINTER installcap | awk '{print $2}'`
set printername = laser
set oldfpflags = `grep FPFLAGS installcap | awk '{print $2}'`
set fpflags = ${oldfpflags}
set oldmathlibs = `grep MATHLIBS installcap | awk '{print $2}'`
set mathlibs = -lm

set makefiles = `ls ${oldkaoshome}/Makefile ${oldkaoshome}/*lib/Makefile`
set makefile =

set kaoshome =

echo '---------------------------------------------------------------'
echo 'StepI: INSTALLATION OF KAOS HOME DIRECTORY'
echo '---------------------------------------------------------------'
echo 'OLD KAOS HOME DIRECTORY:'
echo "   old login directory: ${olduserhome}"
echo "   old kaos home directory: ${oldkaoshome}"

echo 'DEFAULT KAOS HOME DIRECTORY:'
echo "   new login directory: ${HOME}"
echo "   new kaos home directory: ${KAOSHOME}"

if (${KAOSHOME} == '') then
	echo ' WARNING: The environmental variable KAOSHOME is not set.'
	echo ' See the "README" file in the directory'
	echo ' follow instructions there.'
	echo ' Refer to the installation manual for details.'
	exit -1
endif

start_kaoshome_loop:
echo "Do you want to use a new configuration?"
echo "   Yes, go ahead./No, enter manually./Skip. Do nothing."
echo -n "   [[[y]]/n/s]?? "
set answer = $<
switch ($answer)
case 's*':
	echo ''
	echo Skipping this step ...
	set kaoshome = ${oldkaoshome}
	goto skip_kaoshome_loop
	breaksw
case '':
case y*:
	set kaoshome = $KAOSHOME
	breaksw
case n*:
	echo ''
	echo -n "kaos home directory: [$KAOSHOME] "
	set answer = $<
	switch ($answer)
	case '':
		breaksw
	default:
		set kaoshome = $answer
		breaksw
	endsw
	breaksw
default:
	echo Error: Incorrect answer!
	goto start_kaoshome_loop
	breaksw
endsw

echo ''
echo Installing user and kaos home directories...

set oldtop = `echo ${oldkaoshome} | sed 's/\//\\\//g'`
set top = `echo ${kaoshome} | sed 's/\//\\\//g'`

(cd ${kaoshome}; set libs = `ls -d *lib`; set lib = ; cd ..)

${kaoshome}/bin/kedg "${oldtop}" "${top}" ${kaoshome}/include/main_kaos_init.h 

set makefiles = `ls ${kaoshome}/Makefile ${kaoshome}/*lib/Makefile`
#foreach makefile ($makefiles)
#	${kaoshome}/bin/kedg "${oldtop}" "${top}" ${makefile}
#end

echo -n Writing a new \"installcap\" file...
cat > "${kaoshome}/installcap" <<END
USERHOME ${HOME}
KAOSHOME ${kaoshome}
PRINTER ${oldprintername}
SUNOS ${oldsunos}
FPFLAGS ${oldfpflags}
MATHLIBS ${oldmathlibs}
END
echo ...Writing Done!

skip_kaoshome_loop:

echo ''
echo '---------------------------------------------------------------'
echo Step2: Installation of the version number of SunOS...
echo '---------------------------------------------------------------'
echo "	OLD SUNOS VERSION NUMBER: ${oldsunos}."
echo "Trying to find a version number of SunOS from /etc/motd..."
set motdline = (`grep SunOS /etc/motd`)
echo "Found the following:"
echo "   ${motdline}"
if (${motdline[1]} == '') then
	echo ''
	echo Error: Version number of SunOS not found! Specify manually!
	goto manual_sunos_loop
else
	set next = off
	foreach word ($motdline)
		if ( $next == 'on' ) then
			set sunos = $word
			break
		endif	
		if ( $word == 'Release' ) then
			set next = on
		endif
	end
endif

if( $oldsunos == $sunos) then
	echo The new SunOS version number is the same as the old one.
	echo Skipping this step...
	goto skip_sunos_loop
endif

start_sunos_loop:

echo "	NEW VERSION NUMBER OF SUNOS: ${sunos}."
switch(${sunos})
case '3.2*':
	echo kaos can not run under SunOS 3.2.
	echo Update the operating system to SunOS 4.0 or higher!
case '3.5*':
	echo Warning: kaos can be run under SunOS 3.5 only after
	echo modest efforts. Evern after successful installation,
	echo its performance is severely limited. The best solution is
	echo updating SunOS to 4.0 or later.
	echo -n "Do you still want to go ahead with installation??"
	set answer = $<
	switch($answer)
	case 'y*':
		breaksw
	case 'n*':
		exit 0
		breaksw
	endsw
	cat > "${kaoshome}/include/sunos_kaos_def.h" <<END
#define SUNOS4 0
END
	breaksw
case '':
case '4.0*':
	cat > "${kaoshome}/include/sunos_kaos_def.h" <<END
#define SUNOS4 1
END
	breaksw
case 's':
	echo Skipping this step...
	set sunos = ${oldsunos}
	goto skip_sunos_loop
	breaksw
default:

manual_sunos_loop:
	echo This version number of SunOS is not recognized.
	echo -n "Enter the correct version number of SunOS:[s/3.2/3.5/[[4.0]]/4.0.1/4.0.3/4.0.3c]??"
	set sunos = $<
	if($sunos == 's' ) then
		set sunos = ${oldsunos}
	endif
	goto start_sunos_loop
	breaksw
endsw

echo The SunOS version number is recognized and installed!

skip_sunos_loop:

echo ''
echo '---------------------------------------------------------------'
echo 'Step3: Installing laser printer name'
echo '---------------------------------------------------------------'
echo "	OLD LASER PRINTER NAME: ${oldprintername}"
echo "	NEW LASER PRINTER NAME: ${printername}"

start_laser_loop:
echo "Do you want to select a new laser printer name?"
echo "   Yes, go ahead./No, enter manually./Skip. Retain old."
echo -n "   [y/n/[[s]]]?? "
set answer = $<
switch ($answer)
case '':
case 's*':
	set printername = ${oldprintername}
	echo Skipping this step...
	goto skip_laser_loop
	breaksw
case 'y*':
	${kaoshome}/bin/kedg "-P${oldprintername}" "-P${printername}" ${kaoshome}/include/main_kaos_init.h
	breaksw
case 'n*':
	echo -n "Enter the name of the printer: [${oldprintername}]??"
	set printername = $<
	switch ($printername)
	case '':
		set printername = $oldprintername
		breaksw
	default:
		breaksw
	endsw
	${kaoshome}/bin/kedg "-P${oldprintername}" "-P${printername}" ${kaoshome}/include/main_kaos_init.h
	breaksw
default:
	echo Incorrect answer!
	goto start_laser_loop
	breaksw
endsw

echo Laser printer name installation done!

skip_laser_loop:

echo ''
echo '---------------------------------------------------------------'
echo Step4: Installation of the floating point accelerator type
echo '---------------------------------------------------------------'

echo "	OLD FLOATING POINT FLAG: ${oldfpflags}"

set floating =

start_fp_loop:
echo 'Choosing "automatic" determines a floating point flag'
echo 'for a given architecture by using "foption."'
echo "Do you want to install automatically?"
echo "   Yes, go ahead./No, enter manually./Skip. Retain old."
echo -n "   [y/n/[[s]]]??"
set answer = $<
switch($answer)
case '':
case 's*':
	echo Skipping this step...
	set fpflags = ${oldfpflags}
	goto skip_fp_loop
	breaksw
case 'y*':
	set floating = `/usr/etc/foption`
	echo "foption is ${floating}"
	switch($floating)
	case '68881':
	case 'soft':
	case 'fpa':
		echo 'Installing floating point flag...'
		set fpflags = -f${floating}
		foreach makefile ($makefiles)
			${kaoshome}/bin/kedg "${oldfpflags}" "${fpflags}" ${makefile}
		end	
		goto end_fp_loop
		breaksw
	case 'sun4':
		echo 'Installing floating point flag...'
		set fpflags = -${floating}
		foreach makefile ($makefiles)
			${kaoshome}/bin/kedg "${oldfpflags}" "${fpflags}" ${makefile}
		end	
		goto end_fp_loop
		breaksw
	case 'foption':
		echo "foption is not found. Specify the floating point coprocessor type."
		goto manual_fp_option
		breaksw
	endsw
	echo "foption is unable to recognize this floating point accelerator type ${floating}."
	echo Specify the floating pointe coprocessor type.
	goto manual_fp_option
	breaksw
case 'n*':
	manual_fp_option:
	echo Enter the floating point accelerator type:
	echo -n "[[${oldfpflags}]/-f68881/-ffpa/-fsoft/-sun4]??"
	set fpflags = $<
	switch($fpflags)
	case '':
		set fpflags = ${oldfpflags}
		foreach makefile ($makefiles)
			${kaoshome}/bin/kedg "${oldfpflags}" "${fpflags}" ${makefile}
		end	
		goto end_fp_loop
		breaksw
	case '-f68881':
	case '-fsoft':
	case '-ffpa':
	case '-sun4':
		foreach makefile ($makefiles)
			${kaoshome}/bin/kedg "${oldfpflags}" "${fpflags}" ${makefile}
		end	
		goto end_fp_loop
		breaksw
	endsw
	echo "This floating point flag ${fpflags} is not recognized."
	goto manual_fp_option
	breaksw
default:
	echo Incorrect answer!
	goto start_fp_loop
	breaksw
endsw

end_fp_loop:
echo ...Installation Done!

skip_fp_loop:

echo ''
echo '---------------------------------------------------------------'
echo 'Step5: math library flag installation...'
echo '---------------------------------------------------------------'

set sunosoldil = /usr/lib/f68881.il
set sunosnewil = /usr/lib/f68881/libm.il
switch($sunos)
case '3.*':
	set sunosil = ${sunosoldil}
	breaksw
case '4.*':
	set sunosil = ${sunosnewil}
	breaksw
default:
	echo "This version number of SunOS is not correct!"
	exit -1
endsw

echo 'The inline math library increase the performance'
echo "at almost no cost: ${sunosil} is available."

echo "	OLD MATH LIBRARY FLAG: ${oldmathlibs}"
echo "	NEW MATH LIBRARY FLAG: ${mathlibs}"

start_math_loop:
echo "Do you want to select the new math library flag?"
echo "   Yes, go ahead./No, enter manually./Skip. Retain old."
echo -n "   [y/n/[[s]]]?? "
set answer = $<
switch($answer)
case '':
case 's*':
	echo Skipping this step...
	set mathlibs = ${oldmathlibs}
	goto skip_math_loop
	breaksw
case 'n*':
	breaksw
case 'y*':
	manual_math_option:
	echo -n "Enter the math libary type:[[${oldmathlibs}]/[-lm]/[il,${sunosil}]]?? "
	set mathlibs = $<
	breaksw
default:
	echo Incorrect answer!
	goto start_math_loop
	breaksw
endsw

switch($mathlibs)
case '':
	set mathlibs = $oldmathlibs
	breaksw
case '-lm':
	set oldmath = `echo ${oldmathlibs} | sed 's/\//\\\\\\\//g'`
	set math = `echo ${mathlibs} | sed 's/\//\\\\\\\//g'`
	foreach makefile ($makefiles)
		${kaoshome}/bin/kedg "${oldmath}" "${math}" ${makefile}
	end	
	breaksw
case 'il':
case '${sunosil}':
	set oldmath = `echo ${oldmathlibs} | sed 's/\//\\\\\\\//g'`
	set math = `echo ${sunosil} | sed 's/\//\\\\\\\//g'`
	foreach makefile ($makefiles)
		${kaoshome}/bin/kedg "${oldmath}" "${math}" ${makefile}
	end	

	# leave the math option for binsrclib to be -lm
	set math = `echo ${sunosil} | sed 's/\//\\\//g'`
	${kaoshome}/bin/kedg "${math}" "-lm" ${kaoshome}/binsrclib/Makefile
	breaksw
default:
	echo "Error: This math library ${mathlibs} is not recognized."
	goto manaual_math_option
	breaksw
endsw

echo ...math flag installation done!

skip_math_loop:

echo -n Writing a new \"installcap\" file...
cat > "${kaoshome}/installcap" <<END
USERHOME ${HOME}
KAOSHOME ${kaoshome}
PRINTER ${printername}
SUNOS ${sunos}
FPFLAGS ${fpflags}
MATHLIBS ${mathlibs}
END
echo ... Done!

echo ''
echo '---------------------------------------------------------------'
echo Final Step: MAKING KAOS AND COMPANION PROGRAMS
echo '---------------------------------------------------------------'

start_compile_loop:
echo 'Is this first time installation? Make all executables for kaos?'
echo "   Yes, make all./No, make main programs only./Skip."
echo -n "   [y/n/[[s]]]?? "
set answer = $<
switch($answer)
case '':
case 's*':
	echo Skipping compilation...
	goto skip_compile_loop
	breaksw
case 'n*':
	set makeprogram = kaos
	breaksw
case 'y*':
	set makeprogram = all
	breaksw
default:
	echo Error: Incorrect answer!
	goto start_compile_loop
	breaksw
endsw

switch($sunos)
case '3.*':
	if ( $sunos != $oldsunos || '$fpflags' != '$oldfpflags' || '$mathlibs' != '$oldmathlibs' ) then
		echo 'compiling all modules for libraries in the subdirectories...'
		echo 'After initial installation, if SunOS, floating point, math library flags do not change'
		echo "use make kaos to compile only necessary modules"
		${kaoshome}/bin/kmake
	endif
	(cd ${kaoshome}; make ${makeprogram})
	breaksw
case '4.*':
	(cd ${kaoshome}; make ${makeprogram})
	breaksw
default:
	echo The version number of SunOS is not acceptable!
	echo Try to reinstall the version number of SunOS.
	exit -1
endsw

skip_compile_loop:
echo ''
echo "NOTE: kaos is installed in the directory ${kaoshome}"
echo "If the compilation step is not skipped or there are no errors"
echo "during compilation, try to run kaos by moving to"
echo "the directory ${kaoshome} and typing kaos."
