#!/bin/csh

set license = $1
set system = $2
set compiler = $3
set library = $4
set change_options = $5

###########################################################################
#	COPYRIGHT (C) 1996-1997.  All Rights Reserved.
###########################################################################

# This is a C Shell script file.  If you are having problems running this
# script, make sure you have the file /bin/csh (see install).

# Determine system type (may have been set by calling process).
if (! $?os_name || ! $?os_version) then
	setenv os_name `/bin/uname`
	setenv os_version `/bin/uname -r`
endif

alias e echo
alias E echo -n
set nl = ""
alias blank 'echo " "'
set et = ''
set input_style = new

if (! -e src && ! -e include) then
	e "install can only be run from the root of the CF directory tree."
	goto abort
endif


# Platform differences
# SCO: csh's internal echo command can't handle -n or \c; can't handle $< input.
# SunOS 4's internal and external echos can't handle \c.
# Linux doesn't support older `line` input
if ($os_name == 'SCO_SV') then
	# SCO Open Server
	alias E '/bin/echo -n'
	set input_style = old
endif	

###########################################################################
#	Introduction.
###########################################################################

if ($license == "y") goto choose_system

clear
e "ChartFolio Installation:"
blank
e "    This program configures the source and makefiles so you can compile"
e "    ChartFolio.  Once initialized, you can type 'make' from this directory"
e "    to compile the library source code."
blank
e "    To continue, enter 'y'. Otherwise, enter 'n' and the"
E "    installation will end.  [y, n, enter = quit]: "

if ($input_style == "new") then
	set tmp = $<
else
	set tmp = `line`
endif



###########################################################################
#	Determine the ZAF root path.
###########################################################################

clear
E "Enter the ZAF 5.1 root path: "
if ($input_style == "new") then
	set zaf_root = $<
else
	set zaf_root = `line`
endif
blank



###########################################################################
#	Determine which system.
###########################################################################

choose_system:
if ($system != "") goto choose_compiler

clear
e "Which system are you installing to:"
blank
e " 1)  Digital UNIX 3.2"
e " 2)  Hewlett Packard HP-UX 9.0"
e " 3)  Hewlett Packard HP-UX 10.20"
e " 4)  IBM RS/6000 AIX 4.1"
e " 5)  Red Hat Linux 4.x / GCC 2.7.2.1"
e " 6)  Red Hat Linux 5.0 / GCC 2.7.2.3"
e " 7)  SCO UNIX"
e " 8)  SGI IRIX 6.2"
e " 9)  Sun Solaris 2.X"
e " 10)  SunOS 4.1.3"
e " 11)  None of the above"
blank

choose_system_again:
E "[1..10, q = quit]: "$et
if ($input_style == "new") then
	set tmp = $<
else
	set tmp = `line`
endif

switch ($tmp)
	case 1:
	case osf1:
		set system = osf1
		breaksw

	case 2:
	case hpux9:
		set system = hpux9
		breaksw
	
	case 3:	
	case hpux10:
		set system = hpux10
		breaksw
	
	case 4:
	case aix4:
		set system = aix4
		set library = archive
		breaksw

	case 5:
	case linux4:
		set system = linux4
		set compiler = gnu
		breaksw

	case 6:
	case linux5:
		set system = linux5
		set compiler = gnu
		breaksw

	case 7:
	case sco:
		set system = sco
#		set compiler = native
		set library = archive
		breaksw

	case 8:
	case irix6:
		set system = irix6
		breaksw

	case 9:
	case sun5:
		set system = sun5
		breaksw

	case 10:
	case sun4:
		set system = sun4
		breaksw

	case 11:
		set system = none
		breaksw

	case q:
		goto abort

	default:
		# e "Invalid entry please choose again"
		# blank  
		goto choose_system_again
endsw

###########################################################################
#	Determine which compiler type.
###########################################################################

choose_compiler:
if ($compiler != "") goto choose_library

clear
e "Are you using the system's native compiler or gnu?"
blank

choose_compiler_again:
e " 1)  native"
e " 2)  gnu"
blank
E "[1..2, return = native]: " $nl

if ($input_style == "new") then
	set tmp = $<
else
	set tmp = `line`
endif

switch ($tmp)
	case 1:
	case "":
	case native:
		set compiler = native
		breaksw

	case 2:
	case gnu:
		set compiler = gnu
		breaksw

	case q:
		goto abort

	default:
		# e "Invalid entry please choose again"
		# blank
		goto choose_compiler_again
endsw

###########################################################################
#	Determine which library type.
###########################################################################

choose_library:
if ($library != "") goto macro_initialization

clear
e "What type of library would you like to make:"
blank
e " 1)  archive"
e " 2)  shared"
blank

choose_library_again:
E "[1..2, return = archive ]: " $nl

if ($input_style == "new") then
	set tmp = $<
else
	set tmp = `line`
endif

switch ($tmp)
	case 1:
	case "":
	case archive:
		set library = archive
		breaksw

	case 2:
	case shared:
		set library = shared
		breaksw

	case q:
		goto abort

	default:
		# e "Invalid entry please choose again"
		# blank
		goto choose_library_again
endsw

###########################################################################
#   Make macro initialization.
###########################################################################

macro_initialization:

set ZAF_ROOT_DIR 			= 1
set CC            			= 2
set LINK          			= 3
set CC_FLAGS      			= 4
set LINK_FLAGS    			= 5
set CPP_EXTENSION 			= 6
set LIB_EXTENSION			= 7
set INCLUDE_DIRS  			= 8
set LIB_DIRS      			= 9
set GUI_LIBS      			= 10
set LIBS          			= 11
set LIBRARIAN     			= 12
set AR_FLAGS      			= 13
set RANLIB        			= 14
set SHARED_LINK    			= 15
set SHARED_LINK_FLAGS		= 16
set SHARED_LIB_EXTENSION	= 17

if ($library == shared) then
	set last_user_variable = 17
else
	set last_user_variable = 14
endif

set variables = 			\
(							\
	ZAF_ROOT_DIR			\
	CC						\
	LINK					\
	CC_FLAGS				\
	LINK_FLAGS				\
	CPP_EXTENSION			\
	LIB_EXTENSION			\
	INCLUDE_DIRS			\
	LIB_DIRS				\
	GUI_LIBS				\
	LIBS					\
	LIBRARIAN				\
	AR_FLAGS				\
	RANLIB					\
	SHARED_LINK				\
	SHARED_LINK_FLAGS		\
	SHARED_LIB_EXTENSION	\
)

set meanings =						\
(									\
	"ZAF root (installed) path"		\
	"Compiler name" 				\
	"Linker name"					\
	"Compiler options"				\
	"Linker options"				\
	"File extension"				\
	"Library extension"				\
	"Include search paths"			\
	"Library search paths"			\
	"Graphical libraries"			\
	"Other libraries"				\
	"Archiver/librarian name"		\
	"Archiver/librarian options"	\
	"Ranlib command"				\
	"Shared linker name"			\
	"Shared link options"			\
	"Shared library extension"		\
)

###########################################################################
#	Initialize macro values for Motif systems.
###########################################################################

switch ($system)

#	----- Digital UNIX 3.2 -----
	case osf1:
		set values = 				\
		(							\
			".."					\
			"cxx"					\
			"cxx"					\
			""						\
			"-s"					\
			".cxx"					\
			".a"					\
			""						\
			""						\
			"-lXpm -lXm -lXt -lX11"		\
			"-ldnet_stub -lm"		\
			"ar"					\
			"r"						\
			""						\
			"ld"					\
			"-shared -G 0"			\
			".so"					\
		)
		if ($library == shared) then
			set values[$LINK_FLAGS] = "-s -call_shared"
			set values[$LIB_EXTENSION] = ".so"
		endif
		if ($compiler == gnu) then
			set values[$CC] = "g++"
			set values[$LINK] = "g++"
			set values[$SHARED_LINK] = "ld"
			set values[$SHARED_LINK_FLAGS] = "-fPIC -shared -G 0"
			if ($library == shared) then
				set values[$CC_FLAGS] = "-Wall -fPIC"
			else
				set values[$CC_FLAGS] = "-Wall"
			endif
		endif
		breaksw

#	----- Hewlett Packard HP-UX 9.0 -----
	case hpux9:
	case hpux10:
		set values = 								\
		(											\
			".."									\
			"CC"									\
			"CC"									\
			""										\
			"-s"									\
			".cpp"									\
			".a"									\
			""										\
			"-L/usr/lib/Motif1.2 -L/usr/lib/X11R5"	\
			"-lXpm -lXm -lXt -lX11"					\
			"-lm"									\
			"ar"									\
			"r"										\
			""										\
			"CC"									\
			"-b"									\
			".sl"									\
		)
		if ($library == shared) then
			set values[$CC_FLAGS] = "+z"
			set values[$LINK_FLAGS] = "-s -Wl,+s"
			set values[$LIB_EXTENSION] = ".sl"
		endif
		if ($system == hpux10) then
			set values[$LIB_DIRS] = "-L/usr/lib/Motif1.2 -L/usr/lib/X11R6"
		endif
		if ($compiler == gnu) then
			set values[$CC] = "g++"
			set values[$LINK] = "g++"
			set values[$SHARED_LINK] = "g++"
			set values[$LINK_FLAGS] = ""
			set values[$SHARED_LINK_FLAGS] = "-fPIC -shared"
			if ($library == shared) then
				set values[$CC_FLAGS] = "-Wall -fPIC"
			else
				set values[$CC_FLAGS] = "-Wall"
			endif
		endif
		breaksw
	
#	----- IBM RS/6000 AIX 4.1 -----
	case aix4:
		set values = 				\
		(							\
			".."					\
			"xlC"					\
			"xlC"					\
			"-D_IBM_RS6000"			\
			""						\
			".C"					\
			".a"					\
			""						\
			""						\
			"-lXpm -lXm -lXt -lX11"	\
			"-lm"					\
			"ar"					\
			"r"						\
			""						\
			"xlC"					\
			""						\
			".so"					\
		)
		if ($library == shared) then
			set values[$LIB_EXTENSION] = ".so"
		endif
		if ($compiler == gnu) then
			set values[$CC] = "g++"
			set values[$LINK] = "g++"
			set values[$SHARED_LINK] = "g++"
			if ($library == shared) then
				set values[$CC_FLAGS] = "-Wall -fPIC -D_IBM_RS6000"
			else
				set values[$CC_FLAGS] = "-Wall -D_IBM_RS6000"
			endif
		endif
		breaksw
		
#	----- Red Hat Linux -----
	case linux4:
	case linux5:
		set values = 								\
		(											\
			".."									\
			"g++"									\
			"g++"									\
			"-Wall"									\
			""										\
			".cpp"									\
			".a"									\
			"-I/usr/X11R6/include"					\
			"-L/usr/X11R6/lib"						\
			"-lXpm -lXm -lXt -lXext -lX11"			\
			"-lm"									\
			"ar"									\
			"r"										\
			""										\
			"g++"									\
			"-shared"								\
			".so"									\
		)
		if (-e /usr/X11R6/lib/libXp.so) then
			set values[$GUI_LIBS] = "-lXpm -lXm -lXp -lXt -lXext -lX11"
		endif
		if ($system == linux5 && $library != shared) then
			set values[$CC_FLAGS] = "-Wall -DGCC_2_7_2_3"
		else if ($system == linux5 && $library == shared) then
			set values[$CC_FLAGS] = "-Wall -fPIC -DGCC_2_7_2_3"
			set values[$LIB_EXTENSION] = ".so"
			set values[$LINK_FLAGS] = ""
		else if ($library == shared) then
			set values[$CC_FLAGS] = "-Wall -fPIC"
			set values[$LIB_EXTENSION] = ".so"
			set values[$LINK_FLAGS] = ""
		endif
		breaksw
		
#	----- SCO UNIX 3.2 -----
	case sco:
		set values = 				\
		(							\
			".."					\
			"CC"					\
			"CC"					\
			"-DSCO_UNIX"			\
			"-s"					\
			".C"					\
			".a"					\
			""						\
			""						\
			"-lXpm -lXm -lXt -lX11"	\
			"-lsocket -lintl -lm"	\
			"ar"					\
			"r"						\
			""						\
			"CC"					\
			"-shared"				\
			".so"					\
		)
		if ($library == shared) then
			set values[$LIB_EXTENSION] = ".so"
			set values[$LINK_FLAGS] = ""
		endif
		if ($compiler == gnu) then
			set values[$CC] = "g++"
			set values[$LINK] = "g++"
			set values[$SHARED_LINK] = "g++"
			set values[$CC_FLAGS] = "-Wall -DSCO_UNIX"
		endif
		breaksw

#	----- SGI IRIX 5.3 -----
	case irix6:
		set values = 				\
		(							\
			".."					\
			"CC"					\
			"CC"					\
			""			\
			""						\
			".cc"					\
			".a"					\
			""						\
			""						\
			"-lXpm -lXm -lXt -lX11"	\
			"-lm"					\
			"ar"					\
			"r"						\
			""						\
			"CC"					\
			"-shared"				\
			".so"					\
		)
		if ($library == shared) then
			set values[$LINK_FLAGS] = "-call_shared"
			set values[$LIB_EXTENSION] = ".so"
		endif
		if ($compiler == gnu) then
			set values[$CC] = "g++"
			set values[$LINK] = "g++"
			set values[$SHARED_LINK] = "g++"
			if ($library == shared) then
				set values[$CC_FLAGS] = "-Wall -fPIC"
			else
				set values[$CC_FLAGS] = "-Wall"
			endif
		endif
		breaksw

#	----- Siemens/Nixdorf SINIX -----
#	Not tested for ZAF 5
	case sinix:
		set values = 				\
		(							\
			".."					\
			"CC"					\
			"CC"					\
			"-D_SINIX"				\
			"-s"					\
			".C"					\
			".a"					\
			""						\
			""						\
			"-lXpm -lXm -lXt -lX11"	\
			"-lnsl -lgen -lm"		\
			"ar"					\
			"r"						\
			""						\
			"CC"					\
			"-shared"				\
			".so"					\
		)
		if ($library == shared) then
			set values[$LIB_EXTENSION] = ".so"
		endif
		breaksw
		
#	----- Sun Solaris 2.X -----
	case sun5:
		set values = 									\
		(												\
			".."										\
			"CC"										\
			"CC"										\
			""											\
			"-s"										\
			".cc"										\
			".a"										\
			"-I/usr/openwin/include -I/usr/dt/include"	\
			"-L/usr/openwin/lib -L/usr/dt/lib"			\
			"-lXpm -lXm -lXt -lX11"						\
			"-lsocket -lgen -lnsl -lm"					\
			"ar"										\
			"r"											\
			""											\
			"CC"										\
			"-G"										\
			".so"										\
		)
		if ($library == shared) then
			set values[$CC_FLAGS] = "-PIC"
			set values[$LINK_FLAGS] = "-s"
			set values[$LIB_EXTENSION] = ".so"
		endif
		if ($compiler == gnu) then
			set values[$CC] = "g++"
			set values[$LINK] = "g++"
			set values[$LINK_FLAGS] = ""
			set values[$SHARED_LINK] = "g++"
			set values[$SHARED_LINK_FLAGS] = "-fPIC -shared"
			if ($library == shared) then
				set values[$CC_FLAGS] = "-Wall -fPIC"
			else
				set values[$CC_FLAGS] = "-Wall"
			endif
		endif
		breaksw

#	----- Sun Solaris X86 -----
#	Not tested for ZAF 5
	case x86:
		set values = 									\
		(												\
			".."										\
			"CC"										\
			"CC"										\
			""											\
			"-s"										\
			".cc"										\
			".a"										\
			"-I/usr/openwin/include -I/usr/dt/include"	\
			"-L/usr/openwin/lib -L/usr/dt/lib"			\
			"-lXpm -lXm -lXt -lX11"						\
			"-lsocket -lgen -lnsl -lm"					\
			"ar"										\
			"r"											\
			""											\
			"CC"										\
			"-G"										\
			".so"										\
		)
		if ($library == shared) then
			set values[$CC_FLAGS] = "-PIC"
			set values[$LIB_EXTENSION] = ".so"
		endif
		breaksw
	
#	----- SunOS 4.1.3 -----
	case sun4:
		set values = 											\
		(														\
			".."												\
			"CC"												\
			"CC"												\
			"-D_SUNOS4"											\
			"-s"												\
			".cc"												\
			".a"												\
			"-I/opt/IXImd12x/include -I/usr/openwin/include"	\
			"-L/opt/IXImd12x/lib -L/usr/openwin/lib"			\
			"-lXpm -lXm -lXt -lX11"								\
			"-lm"												\
			"ar"												\
			"r"													\
			'/bin/ranlib $@'									\
			"CC"												\
			"-G"												\
			".so"												\
		)
		if ($library == shared) then
			set values[$CC_FLAGS] = "-D_SUNOS4  -PIC"
			set values[$LIB_EXTENSION] = ".so"
		endif
		if ($compiler == gnu) then
			set values[$CC] = "g++"
			set values[$LINK] = "g++"
			set values[$SHARED_LINK] = "g++"
			set values[$SHARED_LINK_FLAGS] = "-fPIC -shared"
			if ($library == shared) then
				set values[$SHARED_CC_FLAGS] = "-D_SUNOS4 -Wall -fPIC"
			else
				set values[$SHARED_CC_FLAGS] = "-D_SUNOS4 -Wall"
			endif
		endif
		breaksw

	default:
		set values = 				\
		(							\
			".."					\
			"CC"					\
			"CC"					\
			""						\
			""						\
			".cpp"					\
			".a"					\
			""						\
			""						\
			"-lXpm -lXm -lXt -lX11"	\
			"-lm"					\
			"ar"					\
			"r"						\
			""						\
			"CC"					\
			"-shared" 				\
			".so"					\
		)

		if ($system != none) then
			e "Unknown system type: '$system'."
			E "Continue [q = quit, return = continue]: "$et
			if ($input_style == "new") then
				set tmp = $<
			else
				set tmp = `line`
			endif
			if ($tmp == "q") goto abort
		endif
		blank
endsw

set values[$ZAF_ROOT_DIR] = $zaf_root

###########################################################################
#	Interactivly display values and maybe let them change them.
###########################################################################

display_variables:
if ($change_options == "n") goto done_assigning

clear
e "Are the following options correct?"
blank
@ i = 1
while ( $i <= $last_user_variable )
	e " " $meanings[$i]":" $values[$i]
	@ i++
end
blank
E "[y, n, q, return = y]: "$et
if ($input_style == "new") then
	set tmp = $<
else
	set tmp = `line`
endif

if ("$tmp" == q) goto abort
if ("$tmp" != n) goto done_assigning

###########################################################################
#	Show each variable, and let the user change it.
###########################################################################

clear
e "Modify the following options:"
blank
@ i = 1
while ($i <= $last_user_variable)
	E "  "$meanings[$i]" "$et
	if ("$values[$i]" == "") then
		E \(none\)$et
	else
		E \($values[$i]\)$et
	endif
	E ": "$et
	if ($input_style == "new") then
		if ($os_name == 'Linux') then
			set tmp = $<:q
		else
			set tmp = $<
		endif
	else
		set tmp = `line`
	endif
	if ("$tmp" == q) goto abort
	if ("$tmp" != "") then
		set values[$i]="$tmp"
		if ($i == $LIB_EXTENSION && $library == shared) then
			set values[$SHARED_LIB_EXTENSION]="$tmp"
		endif
	endif
	@ i++
end
goto display_variables

###########################################################################
#	Create an awk program file to perform the changes.
###########################################################################

done_assigning:

set tmpawkfile=/tmp/tmpAwkFile.$$
set newline="\n"
echo "{ matched = 0 }" > $tmpawkfile
@ i = 1
while ( $i <= $#meanings )
	echo \$1 == \"$variables[$i]\" { printf \"%s = %s\", \$1, \"$values[$i]\" \; matched = 1 \; print \"\" } >> $tmpawkfile
	@ i++
end

echo "matched == 0 { print }" >> $tmpawkfile

###########################################################################
#	Run awk program to edit each Makefile in the current tree.
###########################################################################

blank
e "Configuring makefiles..."
set tmpmakefile=/tmp/tmpmakefile.$$
if (-e /bin/nawk) then
	set awk=/bin/nawk
else
	set awk=awk
endif

set startdir = `pwd`
foreach i (`find $startdir -name posix.mak -print`)
	cd $i:h
	$awk -f $tmpawkfile posix.mak > $tmpmakefile
	mv $tmpmakefile Makefile
end
cd $startdir
rm -f $tmpawkfile

###########################################################################
#	Change source file suffixes
###########################################################################

if (-x ./suffixes && $values[$CPP_EXTENSION] != ".cpp") then
	if (-x /bin/tr || -x /usr/bin/tr) then
		# Strip the '.' from the extension.
		set ext=`echo $values[$CPP_EXTENSION] | tr "." " "`
		e "Checking and changing source file extensions..."
		./suffixes cpp $ext > /dev/null
	else
		e "Unable to change source file extensions.  Please run \'suffixes\'."
	endif
endif

exit_point:
e "ChartFolio configuration is complete."
blank
exit (0)

abort:
blank
e "Aborting the ChartFolio installation program."
blank

