#!/bin/sh
#$Id: simsys_compile,v 1.14 2025/10/07 01:22:10 marcum Exp $

# ******************************************************************************
# Compile SimSys code.
# ******************************************************************************

# ==============================================================================
# Required definitions
# ==============================================================================

script_path="`readlink $0`"

if [ "${script_path}" = "" ]; then

	script_path="$0"
fi

# ==============================================================================
# Include required functions
# ==============================================================================

. "`dirname ${script_path}`/script_functions/set_tmp_dir"
. "`dirname ${script_path}`/script_functions/set_current_arch_type"
. "`dirname ${script_path}`/script_functions/set_compiler_flags"
. "`dirname ${script_path}`/script_functions/set_mpi_dirs"
. "`dirname ${script_path}`/script_functions/set_X11_dirs"
. "`dirname ${script_path}`/script_functions/set_version_dir"
. "`dirname ${script_path}`/script_functions/set_sys_dir"
. "`dirname ${script_path}`/script_functions/make_prog_lib"
. "`dirname ${script_path}`/script_functions/make_win_dll"

set_dylib_dep="`dirname ${script_path}`/script_functions/set_dylib_dep"

# ==============================================================================
# Function to set documentation for user options
# ==============================================================================

set_user_options_documentation ()
{
	if [ "${user_options}" != "" ]; then

		ifs_def="$IFS"

		for user_option in ${user_options}; do

			IFS=":"

			set -- ${user_option}

			user_option_flag="$1"

			user_option_type_and_items="$2"

			IFS=";"

			set -- ${user_option_type_and_items}

			user_option_type="$1"

			list1="$2"

			list2="$3"

			IFS=","

			if [ "${list1}" != "" ]; then

				set -- ${list1}

				user_option_list=$1

				shift 1

				for option in $*; do

					user_option_list="${user_option_list} ${option}"
				done

				if [ "${user_option_list}" = "NULL" ]; then

					user_option_list=
				fi
			else

				user_option_list=
			fi

			if [ "${list2}" != "" ]; then

				set -- ${list2}

				user_option_items=$1

				shift 1

				for item in $*; do

					user_option_items="${user_option_items} ${item}"
				done
			else

				user_option_items=
			fi

			IFS="${ifs_def}"

			if [ "${user_options_usage_doc}" = "" ]; then

				user_options_usage_doc="
		[${user_option_flag}"
			else
				user_options_usage_doc="${user_options_usage_doc},${user_option_flag}"
			fi

			if [ "${user_option_type}" = "prog_list" ]; then

				user_options_summary_doc="${user_options_summary_doc}
${user_option_flag}		: compile specified list of program/library names"

				user_options_full_doc="${user_options_full_doc}
${user_option_flag}		: Compile program/library names listed below.
		  ${user_option_items}"
			fi

			if [ "${user_option_list}" != "" ]; then

				user_options_full_doc="${user_options_full_doc}
		  This option also automatically sets the options listed below.
		  ${user_option_list}"
			fi

			IFS="${ifs_def}"
		done

		user_options_usage_doc="${user_options_usage_doc}]"
	fi
}

# ==============================================================================
# Function to list usage documentation
# ==============================================================================

my_list_usage ()
{
	set_user_options_documentation

	if [ "${base_arch_type}" = "WIN" ]; then
		win_options="
		[-make_win_dll]"
	fi

	echo "
`basename $0`	[-h,-help]
		[-v name]
		[-nodep,-nover]
		[-clean,-exe,-lib,-shared,-sys]
		[--Dvariable,-Dvariable,-Uvariable]
		[-Idir]
		[-llib]
		[-Ldir]
		[-no_cgns,-no_libmesh,-no_egads]
		[-x]
		[-g,-debug,-64,-int64,-prof,-x86]
		[-debug_code]
		[-static]${win_options}${user_options_usage_doc}
		[program/library name(s)]

`basename $0` -h	list summary of documentation
`basename $0` -help	list full documentation
"
}

# ==============================================================================
# Function to list summary documentation
# ==============================================================================

my_list_summary ()
{
	set_user_options_documentation

	if [ "${base_arch_type}" = "WIN" ]; then
		win_options="
-make_win_dll	: make Windows DLL files after compiling programs"
	fi

	echo "
`basename $0` [option flags] program/library name(s)

-h		: list summary of documentation
-help		: list full documentation
-v name		: use name for the location of all files
-nodep		: do not update dependent libraries
-nover		: do not update version routines
-clean		: re-compile routines and create \"clean\" versions of libraries
-exe		: create an executable and do not update libraries
-lib		: update libraries and do not create an executable
-shared		: create shared libraries and create an executable using them
-sys		: include current SimSys path in include/library search paths
--Dvariable	: compile routines (only newer ones) with \"variable\" defined
-Dvariable	: compile routines (of any age) with \"variable\" defined
-Uvariable	: compile routines (of any age) with \"variable\" undefined
-Idir		: compile routines with additional include directories
-llib		: compile executables with additional libraries
-Ldir		: compile executables with additional library directories
-debug_code	: compile routines with the DEBUG_CODE define flag set
-no_cgns	: compile routines with CGNS file type disabled
-no_libmesh	: compile routines with MESH file type library disabled
-no_egads	: compile routines with the EGADS library disabled
-x		: compile routines with X-Window plotting, if available
-g,-debug	: compile routines using the -g and sanitize compiler flag
-64,-int64	: compile routines for 64-bit int's
-prof		: create a \"gprof\" version of the executable
-x86		: compile routines for x86-64 architecture type (only on arm64)
-static		: use only static system libraries${win_options}${user_options_summary_doc}
"
}

# ==============================================================================
# Function to list full documentation
# ==============================================================================

my_list_full ()
{
	set_user_options_documentation

	if [ "${base_arch_type}" = "WIN" ]; then
		win_options="
-make_win_dll	: Make Windows DLL files after compiling programs."
	fi

	echo "
----------------------------------------------------------------------------
`basename $0` :	compile routines and update executable and libraries
			for a SimSys program or library
----------------------------------------------------------------------------

Usage

`basename $0` [option flags] program/library name(s)

-h		: List summary documentation.
-help		: List full documentation.
-v name		: Use \"name\" for the location of all source code, libraries
		  and executables. The default location is
		  ${src_sys_dir}.
-nodep		: Do not update dependent libraries. By default all dependent
		  libraries are updated.
-nover		: Do not update version routines. By default all version
		  routines are updated.
-clean		: Re-compile routines and create \"clean\" versions of libraries.
-exe		: Create an executable (if applicable) and do not update
		  libraries.
-lib		: Update libraries and do not create an executable (if
		  applicable).
-shared		: Create shared libraries and create an executable (if
		  applicable) using shared libraries.
		  The default is to use static archive versions of the
		  libraries.
		  This option flag automatically deselects the \"-static\"
		  option flag.
-sys		: include current SimSys directory path in include file search
		  path (-I...) and in library search path (-L...)
--Dvariable	: Compile routines with \"variable\" defined. Only routines with
		  the variable that are newer than the library object code are
		  recompiled.
-Dvariable	: Compile routines with \"variable\" defined. All routines with
		  the variable are recompiled regarless of age.
-Uvariable	: Compile routines with \"variable\" undefined. All routines
		  with the variable are recompiled regarless of age.
-Idir		: Compile routines with additional include directories.
-llib		: Compile executables with additional libraries.
-Ldir		: Compile executables with additional library directories.
-debug_code	: Compile routines with debug code enabled.
		  The default is to disable debug code.
-no_cgns	: Compile routines with the CGNS file type disabled.
		  The default is to enable the CGNS file type.
-no_libmesh	: Compile routines with the MESH file type disabled.
		  The default is to enable the MESH file type.
-no_egads	: Compile routines with the EGADS library disabled.
		  The default is to enable the EGADS library, if applicable.
-x		: Compile routines with X-Window plotting (if available).
		  The default is to disable X-Window plotting.
-g,-debug	: Compile routines using the -g and sanitize compiler flag.
		  The default is to not use the -g compiler flag.
		  Library files created with this option are named
		  ${lib_prefix}xxx_gs.${static_lib_suffix} (or ${lib_prefix}xxx_g.${static_lib_suffix} on Linux only).
		  ${lib_prefix}xxx.${static_lib_suffix} is the default name and \"xxx\" is the library name.
		  Executable files are named xxx_gs.exe (or xxx_g.exe on Linux
		  only). xxx.exe is the default name and \"xxx\" is the
		  program/system name.
-64,-int64	: Compile routines for 64-bit int's using the
		  -D_UG_LONG_LONG_INT_ flag. The default is to use 32-bit int's.
		  Library files created with this option are named ${lib_prefix}xxx_l.${static_lib_suffix}.
		  ${lib_prefix}xxx.${static_lib_suffix} is the default name and \"xxx\" is the library name.
		  Executable files are named xxx_l.exe. xxx.exe is the default
		  name and \"xxx\" is the  program/system name.
-prof		: Compile routines for \"gprof\".
		  The default is to not use the -prof compiler flag.
		  Library files created with this option are named ${lib_prefix}xxx_p.${static_lib_suffix}.
		  ${lib_prefix}xxx.${static_lib_suffix} is the default name and \"xxx\" is the library name.
		  Executable files are named xxx_p.exe. xxx.exe is the default
		  name and \"xxx\" is the program/system name).
-x86		: Compile routines for MacOSX-x86-64 architecture type. Only
		  applicable on MacOSX-arm64,
		  The default is to compile for MacOSX-arm64 on MacOSX-arm64.
-static		: Use only static system libraries.
		  The default is to use dynamic system libraries.${win_options}${user_options_full_doc}

Notes

1) Program/library name(s) must be specified.

2) Options are set in the order that they appear.

3) Routines are only compiled if the source is newer than the object file in the
   corresponding static archive library.

4) Options can appear before and/or after the program/library name.

Examples

`basename $0` aflr2

   Compile routines and update libraries used by aflr2 and create a new aflr2
   executable. aflr2 is a SimSys program.

`basename $0` aflr2 aflr3

   Compile routines and update libraries used by aflr2 and aflr3 and create new
   aflr2 and aflr3 executables. aflr2 and aflr3 are SimSys programs.

`basename $0` aflr3 -lib

   Compile routines and update libraries used by aflr3. aflr3 is a SimSys
   program.

`basename $0` aflr3 -exe

   Create a new aflr3 executable. aflr3 is a SimSys program.
"
}

#===============================================================================
# Function to check if cgns is required
#===============================================================================

check_cgns ()
{
	if [ "`echo ${compile_lib_names} | grep cgns`" = "" ]; then

		cgns_define_flag=

	elif [ ! "${cgns_define_flag}" ]; then

		list=

		for compile_lib_name in ${compile_lib_names}; do

			if [ "${compile_lib_name}" != "cgns" ]; then

				list="${list} ${compile_lib_name}"
			fi
		done

		compile_lib_names="${list}"
	else

		opt_define_flags="${opt_define_flags} ${cgns_define_flag}"
	fi
}

#===============================================================================
# Function to check if EGADS is available
#===============================================================================

check_egads ()
{
	if [ "`echo ${compile_lib_names} | grep egads_aflr4`" = "" ] && [ "${egads_lite_define_flag}" = "" ]; then

		egads_define_flag=
		return
	fi

	if [ "${egads_define_flag}" ]; then

		if [ "${egads_lite_define_flag}" = "" ]; then
			egads_lib_base_name="egads"
		else
			egads_lib_base_name="egadslite"
		fi

		possible_include_dirs="${EGADS_INCLUDE_DIR} ${src_dir}/egads_aflr4/egads_include ${src_dir}/egads ${src_dir}/egads_include"

		EGADS_INCLUDE_DIR=

		for dir_name in ${possible_include_dirs}; do

			if [ -r "${dir_name}/egads.h" ]; then

				opt_include_dirs="${opt_include_dirs} ${dir_name}"

				EGADS_INCLUDE_DIR="${dir_name}"

				break
			fi
		done

		if [ "${EGADS_INCLUDE_DIR}" ]; then

			possible_lib_dirs="${EGADS_LIB_DIR} ${sys_shared_lib_dir}"

			EGADS_LIB_DIR=

			for dir_name in ${possible_lib_dirs}; do

				for file in ${dir_name}/*${egads_lib_base_name}.*; do 

					if [ -e "${file}" ]; then

						EGADS_LIB_DIR="${dir_name}"

						break 2
					fi
				done
			done
		else

			EGADS_LIB_DIR=
		fi

		if [ "${EGADS_INCLUDE_DIR}" ] && [ "${EGADS_LIB_DIR}" ]; then

			if [ "`echo ${base_arch_type} | grep WIN`" != "" ]; then

				other_libs="${other_libs} ${EGADS_LIB_DIR}/${egads_lib_base_name}.dll"
			else
				opt_lib_dirs="${opt_lib_dirs} -L${EGADS_LIB_DIR}"
				other_libs="${other_libs} -l${egads_lib_base_name}"

				if [ "`uname -s`" = "Darwin" ]; then

					opt_lib_dirs="${opt_lib_dirs} -rpath ${EGADS_LIB_DIR}"
				fi
			fi

			no_Bstatic_flag=no
		else

			egads_define_flag=
		fi
	fi

	if [ ! "${egads_define_flag}" ]; then

		list=

		for compile_lib_name in ${compile_lib_names}; do

			if [ "`echo ${compile_lib_name} | grep egads`" = "" ]; then

				list="${list} ${compile_lib_name}"
			fi
		done

		compile_lib_names="${list}"
	else

		opt_define_flags="${opt_define_flags} ${egads_define_flag} ${egads_lite_define_flag}"
	fi
}

#===============================================================================
# Function to check if libmesh is required
#===============================================================================

check_libmesh ()
{
	if [ "`echo ${compile_lib_names} | grep mesh_ug_io`" = "" ]; then

		libmesh_define_flag=

	elif [ ! "${libmesh_define_flag}" ]; then

		list=

		for compile_lib_name in ${compile_lib_names}; do

			if [ "${compile_lib_name}" != "mesh_ug_io" ]; then

				list="${list} ${compile_lib_name}"
			fi
		done

		compile_lib_names="${list}"
	else

		opt_define_flags="${opt_define_flags} ${libmesh_define_flag}"
	fi
}

#===============================================================================
# Function to check if libmpi is required
#===============================================================================

check_mpi ()
{
	if [ "${mpi_code}" = "no" ] || [ "`echo ${compile_lib_names} | grep ug_mpi`" = "" ]; then
		return
	fi

	set_mpi_dirs

	if [ "${mpi_include_dir}" ] && [ "${mpi_libs}" ]; then

		mpi_code=yes

		opt_define_flags="${opt_define_flags} ${mpi_define_flag}"
	else
		mpi_code=no
	fi

	if [ "${mpi_code}" = "no" ]; then

		list=

		for compile_lib_name in ${compile_lib_names}; do

			if [ "${compile_lib_name}" != "ug_mpi" ]; then

				list="${list} ${compile_lib_name}"
			fi
		done

		compile_lib_names="${list}"
	fi
}

#===============================================================================
# Function to check if X11 is required
#===============================================================================

check_X11 ()
{
	if [ "`echo ${compile_lib_names} | grep xplt2`" = "" ] && [ "`echo ${compile_lib_names} | grep XW`" = "" ]; then

		X11_code=no
		return
	fi

	if [ "${X11_code}" != "no" ]; then

		set_X11_dirs

		if [ "${X11_include_dir}" ] && [ "${X11_libs}" ]; then

			X11_code=yes

			opt_define_flags="${opt_define_flags} ${X11_define_flag}"
		else
			X11_code=no
		fi
	fi

	if [ "${X11_code}" = "no" ]; then

		list=

		for compile_lib_name in ${compile_lib_names}; do

			if [ "${compile_lib_name}" != "xplt2" ] && [ "${compile_lib_name}" != "XW" ]; then

				list="${list} ${compile_lib_name}"
			fi
		done

		compile_lib_names="${list}"
	fi
}

#===============================================================================
# Function to get variable name or value from string name=value
#===============================================================================

get_variable_name_or_value ()
{
	variable_option=$1
	variable_string=$2

	ifs_def="$IFS"

	IFS="="

	set -- ${variable_string}

	variable_name=$1
	variable_value=$2

	IFS="${ifs_def}"

	if [ "${variable_option}" = "name" ]; then

		echo "${variable_name}"
	else
		echo "${variable_value}"
	fi
}

#===============================================================================
# Function to compile executable
#===============================================================================

compile_executable ()
{
	if [ "${program_name}" = "xplt2" ] && [ "${X11_code}" = "no" ]; then
		return
	fi

# ----------------------
# Save current directory
# ----------------------

	org_dir_name=`pwd`

# --------------------------------
# Change to main program directory
# --------------------------------

	cd ${main_dir}

#---------------------
# Compile main program
#---------------------

	if [ "${f_main}" != "" ]; then

		compiler="${f_compiler_opt0}"

		main_program="${main_program_base_name}${f_file_suffix}"

	elif [ "${cpp_main}" != "" ]; then

		compiler="${cpp_compiler_opt0}"

		main_program="${main_program_base_name}${cpp_file_suffix}"
	else

		compiler="${c_compiler_opt0}"

		main_program="${main_program_base_name}${c_file_suffix}"
	fi


	if [ ! -r "${main_program}" ]; then

		echo "*** unable to find main program ${main_program} ***"
		echo "*** in directory ${main_dir} ***"
		exit
	fi

# -----------------------------------
# Create tmp dir and tmp main program
# -----------------------------------

	if [ "${main_program_files}" ]; then

		tmp_dir="${TMPDIR}/${USER}_`basename $0`_$$"

		if [ -d "${tmp_dir}" ]; then

			/bin/rm -rf ${tmp_dir}
		fi

		mkdir ${tmp_dir}

		main_program_tmp_code="${tmp_dir}/${main_program}"

		cat ${main_program} ${main_program_files} > ${main_program_tmp_code}
	else
		main_program_tmp_code="${main_program}"
	fi

# --------------------
# Compile main program
# --------------------

	echo "${compiler} ${other_compiler_flags} ${main_program_tmp_code} -o ${main_program_base_name}.o"
	${compiler} ${other_compiler_flags} ${main_program_tmp_code} -o ${main_program_base_name}.o 

# --------------------
# Delete tmp directory
# --------------------

	if [ "${main_program_files}" ]; then

		/bin/rm -rf ${tmp_dir}
	fi

# -------------------------------
# Check for executable directory
# -------------------------------

	if [ ! -d "${sys_exe_dir}" ]; then

		echo "CREATING DIRECTORY ${sys_exe_dir}"
		mkdir ${sys_exe_dir}
	fi

# ------------------------------------
# Change to executable file directory
# ------------------------------------

	cd ${sys_exe_dir}

#--------------------------
# Set executable file name
#--------------------------

	if [ "`echo ${main_program_base_name} | grep _i`" != "" ]; then
		if [ "${main_program_base_name}" = "aflr2_i" ]; then
			exe_name=${exe_base_name}_i${compile_suffix}.exe
			dyn_exe_name=${exe_base_name}_i_dyn${compile_suffix}.exe
		else
			exe_name=${main_program_base_name}${compile_suffix}.exe
			dyn_exe_name=${main_program_base_name}_dyn${compile_suffix}.exe
		fi
	else
		exe_name=${exe_base_name}${compile_suffix}.exe
	fi

#----------------
# Link executable
#----------------

	if [ "${f_linker_flag}" != "" ]; then

		echo "${f_linker} ${main_dir}/${main_program_base_name}.o ${opt_libs} ${opt_lib_dirs} ${libs} ${other_libs} ${f_other_libs} -o ${exe_name}"

		${f_linker} ${main_dir}/${main_program_base_name}.o ${opt_libs} ${opt_lib_dirs} ${libs} ${other_libs} ${f_other_libs} -o ${exe_name}

	elif [ "${cpp_linker_flag}" != "" ]; then

		echo "${cpp_linker} ${main_dir}/${main_program_base_name}.o ${opt_libs} ${opt_lib_dirs} ${libs} ${other_libs} ${cpp_other_libs} -o ${exe_name}"
		${cpp_linker} ${main_dir}/${main_program_base_name}.o ${opt_libs} ${opt_lib_dirs} ${libs} ${other_libs} ${cpp_other_libs} -o ${exe_name}

	else

		if [ "${main_program_base_name}" = "aflr2_i" ] || [ "${main_program_base_name}" = "aflr3_i" ] || [ "${main_program_base_name}" = "aflr4_i" ] || [ "${main_program_base_name}" = "aflr43_i" ]; then

			if [ "${main_program_base_name}" = "aflr43_i" ]; then
				all_libs="${sys_lib_dir}/${lib_prefix}aflr_all${compile_suffix}.${static_lib_suffix}"
				all_dyn_libs="${sys_shared_lib_dir}/${lib_prefix}aflr_all.${shared_lib_suffix}"
			else
				all_libs="${sys_lib_dir}/${lib_prefix}${program_name}_all${compile_suffix}.${static_lib_suffix}"
				all_dyn_libs="${sys_shared_lib_dir}/${lib_prefix}${program_name}_all.${shared_lib_suffix}"
			fi

			echo "${c_linker} ${main_dir}/${main_program_base_name}.o ${opt_libs} ${opt_lib_dirs} ${all_libs} ${other_libs} ${c_other_libs} -o ${exe_name}"
			${c_linker} ${main_dir}/${main_program_base_name}.o ${opt_libs} ${opt_lib_dirs} ${all_libs} ${other_libs} ${c_other_libs} -o ${exe_name}

			chmod ugo+rx ${exe_name}
			chmod go-w ${exe_name}

			if [ "${debug_flag}" = "" ]; then

				echo "${c_linker_shared} ${main_dir}/${main_program_base_name}.o ${opt_libs} ${opt_lib_dirs} ${all_libs} ${other_libs} ${c_other_libs} -o ${dyn_exe_name}"
				${c_linker_shared} ${main_dir}/${main_program_base_name}.o ${opt_libs} ${opt_lib_dirs} ${all_libs} ${other_libs} ${c_other_libs} -o ${dyn_exe_name}

				chmod ugo+rx ${dyn_exe_name}
				chmod go-w ${dyn_exe_name}
			fi
		else
			echo "${c_linker} ${main_dir}/${main_program_base_name}.o ${opt_libs} ${opt_lib_dirs} ${libs} ${other_libs} ${c_other_libs} -o ${exe_name}"
			${c_linker} ${main_dir}/${main_program_base_name}.o ${opt_libs} ${opt_lib_dirs} ${libs} ${other_libs} ${c_other_libs} -o ${exe_name}

			chmod ugo+rx ${exe_name}
			chmod go-w ${exe_name}

		fi
	fi

	/bin/rm ${main_dir}/${main_program_base_name}.o

# ----------------------------
# Change to original directory
# ----------------------------

	cd ${org_dir_name}

	error_flag=
}

#===============================================================================
# Function to create shared library
#===============================================================================

create_shared_library ()
{

# ------------------------------------------------
# Return if shared library option is not specified
# ------------------------------------------------

	if [ "${shared_lib_linker}" = "N/A" ] || [ "${shared_lib_flag}" = "" ]; then
		return
	fi

# ----------------------
# Save current directory
# ----------------------

	org_dir=`pwd`

# ----------------------------
# Set full shared library name
# ----------------------------

	shared_lib_full_name=${lib_prefix}${lib_base_name}${compile_suffix}.${shared_lib_suffix}

# --------------
# Create tmp dir
# --------------

	tmp_dir="${TMPDIR}/${USER}_`basename $0`_$$"

	if [ -d "${tmp_dir}" ]; then

		/bin/rm -rf ${tmp_dir}
	fi

	mkdir ${tmp_dir}

# -----------------------------------------
# Extract object code from object libraries
# -----------------------------------------

	cd ${tmp_dir}

	ar x ${lib_full_name}

# ----------------------------------
# Check for shared library directory
# ----------------------------------

	if [ ! -d "${sys_shared_lib_dir}" ]; then

		echo "CREATING DIRECTORY ${sys_shared_lib_dir}"
		mkdir ${sys_shared_lib_dir}
	fi

# ---------------------
# Create shared library
# ---------------------

	cd ${sys_shared_lib_dir}

	if [ -f "${shared_lib_full_name}" ]; then

		/bin/rm ${shared_lib_full_name}
	fi

	echo "${shared_lib_linker} ${tmp_dir}/*.o -o ${shared_lib_full_name}"
	${shared_lib_linker} ${tmp_dir}/*.o -o ${shared_lib_full_name}

	chmod ugo+rx ${shared_lib_full_name}
	chmod go-w ${shared_lib_full_name}

	if [ -f so_locations ]; then

		/bin/rm so_locations
	fi

# -----------------------------------------------------
# Set ID and dependency names with loader relative path
# -----------------------------------------------------

	${set_dylib_dep} ${shared_lib_full_name}

# --------------------
# Delete tmp directory
# --------------------

	/bin/rm -rf ${tmp_dir}

# ----------------------------
# Change to original directory
# ----------------------------

	cd ${org_dir}
}

#===============================================================================
# Function to update special file and library
#===============================================================================

update_special_file ()
{
# ----------------------
# Set file name and type
# ----------------------

	file_name=$1
	update_option=$2

	tmp_file_name="${TMPDIR}/${USER}_`basename $0`_$$_${file_name}"

# ----------------------------------------------------------
# Change version file compile date in temporary compile file
# ----------------------------------------------------------

	if [ "${update_option}" = "UPDATE_COMPILE_DATE" ]; then

		compile_date="`date \"+%m/%d/%y @ %I:%M%p\"`"

		compile_os="`uname -mrs`"

		sed_command1="/strncpy (Compile_Date,/c\\
  strncpy (Compile_Date, \"${compile_date}\", 40);
"
		sed_command2="/strncpy (Compile_OS,/c\\
  strncpy (Compile_OS, \"${compile_os}\", 40);
"

		cat ${file_name} | sed \
-e "${sed_command1}" \
-e "${sed_command2}" \
> ${tmp_file_name}

# ----------------------------------------
# For other cases don't use temporary file
# ----------------------------------------

	else

		cp ${file_name} ${tmp_file_name}
	fi

# -------------------------------
# Compile file and update library
# -------------------------------

	make_update_flag=yes

	base_file_name=`basename ${file_name} ${file_suffix}`

	echo "${compiler} ${other_compiler_flags} ${tmp_file_name} -o ${base_file_name}.o"
	${compiler} ${other_compiler_flags} ${tmp_file_name} -o ${base_file_name}.o

	ar ${ar_flags} ${lib_full_name} ${base_file_name}.o

	/bin/rm -f ${base_file_name}.o

# -----------------------------------------------------------
# Remove temporary compile file
# -----------------------------------------------------------

	/bin/rm -f ${tmp_file_name}
}

#===============================================================================
# Function to update library
#===============================================================================

make_lib ()
{
	if [ "${source_files}" != "" ]; then

# -------------------------------------------
# Use makedepend and make to update libraries
# -------------------------------------------

		dep_list=
		lib_files=

		for file_name in ${source_files}; do

			if [ -r "${file_name}" ]; then

				base_file_name=`basename ${file_name} ${file_suffix}`

				if [ "${base_file_name}" != "${file_name}" ]; then

					dep_list="${dep_list}
${lib_full_name}(${base_file_name}.o): ${file_name}"
					lib_files="${lib_files} ${lib_full_name}(${base_file_name}.o)"
				fi
			fi
		done

		if [ "${lib_files}" != "" ]; then

			Makefile=${TMPDIR}/${USER}_`basename $0`_Make_File_$$
			Makelog=${TMPDIR}/${USER}_`basename $0`_Make_File_Log_$$

			/bin/rm -f ${Makefile}
			/bin/rm -f ${Makelog}

			echo "
OBJS = ${lib_files}
${lib_full_name}: \$(OBJS)
\$(OBJS):
	${compiler} ${other_compiler_flags} \$*${file_suffix}
	ar ${ar_flags} ${lib_full_name} \$*.o
	/bin/rm \$*.o
	@echo \"\$*${file_suffix} \" >> ${Makelog}
# DO NOT DELETE
" > ${Makefile}

			makedepend ${define_flags} -o.o -f ${Makefile} ${source_files} 2> /dev/null

			/bin/cp -f ${Makefile} ${Makefile}_tmp

			cat ${Makefile} | \
sed "/\.o: /s/:/):/" | \
sed "/\.o): /s!^!${lib_full_name}(!" \
> ${Makefile}_tmp

			echo "${dep_list}" > ${Makefile}_dep

			cat ${Makefile}_tmp ${Makefile}_dep > ${Makefile}

			make -f ${Makefile}

			/bin/rm -f ${Makefile}*

			if [ -r ${Makelog} ]; then

				make_update_flag=yes

				/bin/rm -f ${Makelog}
			fi

			error_flag=

			chmod ugo+r ${lib_full_name} 
			chmod ugo-x ${lib_full_name} 
			chmod go-w ${lib_full_name} 
		fi

# -----------------------------------------------------------------------
# Compile routines with variables defined or undefined and update library
# -----------------------------------------------------------------------

		for file_name in ${source_files}; do

			if [ -r "${file_name}" ]; then

				for define_flag_string in ${define_flag_list}; do

					define_flag_name="`get_variable_name_or_value name ${define_flag_string}`"

					if [ "`grep ${define_flag_name} ${file_name}`" != "" ]; then

						update_special_file ${file_name} ${define_flag_name}
					fi
				done
			fi
		done
	fi
}

#===============================================================================
# Function to compile library
#===============================================================================

compile_library ()
{
# ---------------------
# Set full library name
# ---------------------

	lib_full_name=${sys_lib_dir}/${lib_name}

# ------------------------------------------------------------------------
# Move object library from MS .lib file on WIN64 to a UNIX lib*.a
# ------------------------------------------------------------------------

	if [ "`echo ${base_arch_type} | grep WIN`" != "" ]; then

		win_lib_prefix=${lib_prefix}
		win_static_lib_suffix=${static_lib_suffix}

		win_lib_full_name=${lib_full_name}

		lib_prefix=lib
		static_lib_suffix=a

		lib_full_name=${sys_lib_dir}/${lib_prefix}${lib_base_name}${compile_suffix}.${static_lib_suffix}

		if [ -f "${win_lib_full_name}" ]; then

			/bin/mv -f ${win_lib_full_name} ${lib_full_name}
		fi
	fi

# -----------------------------------------
# Remove library if clean version requested
# -----------------------------------------

	if [ "${clean_lib_flag}" != "" ]; then

		clean_flag=yes

		for clean_lib in ${clean_lib_list}; do

			if [ "${clean_lib}" = "${lib_base_name}" ]; then

				clean_flag=

				break
			fi
		done

		if [ "${clean_flag}" != "" ] && [ -f "${lib_full_name}" ]; then

			/bin/rm ${lib_full_name}

			clean_lib_list="${clean_lib_list} ${lib_base_name}"
		fi
	fi

# ----------------------
# Save current directory
# ----------------------

	org_dir=`pwd`

# ---------------------------
# Change to source directory
# ---------------------------

	cd ${source_dir}

# ---------------------------
# Check for library directory
# ---------------------------

	if [ ! -d "${sys_lib_dir}" ]; then

		echo "CREATING DIRECTORY ${sys_lib_dir}"
		mkdir ${sys_lib_dir}
	fi

# -----------------------------
# Update library symbolic table
# -----------------------------

	if [ -f "${lib_full_name}" ]; then

		if [ "${symtable}" != "" ]; then

			${symtable} ${lib_full_name} > /dev/null

			chmod ugo+r ${lib_full_name} 
			chmod ugo-x ${lib_full_name} 
			chmod go-w ${lib_full_name} 
		fi
	fi

# ------------------------------------------
# Compile source routines and update library
# ------------------------------------------

	make_update_flag=

	file_suffix=${c_file_suffix}

	compiler=${c_compiler_opt0}
	source_files=${c_opt0_files}

	make_lib

	compiler=${c_compiler_opt1}
	source_files=${c_opt1_files}

	make_lib

	compiler=${c_compiler_opt2}
	source_files=${c_opt2_files}

	make_lib

	file_suffix=${cpp_file_suffix}

	compiler=${cpp_compiler_opt0}
	source_files=${cpp_opt0_files}

	make_lib

	compiler=${cpp_compiler_opt1}
	source_files=${cpp_opt1_files}

	make_lib

	compiler=${cpp_compiler_opt2}
	source_files=${cpp_opt2_files}

	make_lib

	file_suffix=${f_file_suffix}

	compiler=${f_compiler_opt0}
	source_files=${f_opt0_files}

	make_lib

	compiler=${f_compiler_opt1}
	source_files=${f_opt1_files}

	make_lib

	compiler=${f_compiler_opt2}
	source_files=${f_opt2_files}

	make_lib

# ------------------------------------------
# Compile version routine and update library
# ------------------------------------------

	if [ "${version_flag}" = "yes" ] && [ "${make_update_flag}" = "yes" ]; then

		source_files="${c_opt0_files} ${c_opt1_files} ${c_opt2_files} ${cpp_opt0_files} ${cpp_opt1_files} ${cpp_opt2_files}"

		for file_name in ${source_files}; do

			if [ -r "${file_name}" ]; then

				if [ "`echo ${file_name} | grep _version${cpp_file_suffix}`" != "" ]; then

					update_special_file_name=${file_name}

					file_suffix=${cpp_file_suffix}

					compiler=${cpp_compiler_opt0}

					update_special_file ${file_name} UPDATE_COMPILE_DATE

				elif [ "`echo ${file_name} | grep _version${c_file_suffix}`" != "" ]; then

					update_special_file_name=${file_name}

					file_suffix=${c_file_suffix}

					compiler=${c_compiler_opt0}

					update_special_file ${file_name} UPDATE_COMPILE_DATE
				fi
			fi
		done
	fi

# -----------------------------
# Update library symbolic table
# -----------------------------

	if [ "${symtable}" != "" ]; then

		${symtable} ${lib_full_name} > /dev/null

		chmod ugo+r ${lib_full_name} 
		chmod ugo-x ${lib_full_name} 
		chmod go-w ${lib_full_name} 
	fi

# ------------------------------------------------------------------------
# Move object library from UNIX lib*.a to a MS .lib file on WIN64
# ------------------------------------------------------------------------

	if [ "`echo ${base_arch_type} | grep WIN`" != "" ]; then

		/bin/mv -f ${lib_full_name} ${win_lib_full_name}

		lib_prefix=${win_lib_prefix}
		static_lib_suffix=${win_static_lib_suffix}
		lib_full_name=${win_lib_full_name}
	fi

# ----------------------------
# Change to original directory
# ----------------------------

	cd ${org_dir}

# ---------------------
# Create shared library
# ---------------------

	create_shared_library
}

# ==============================================================================
# Function to compile program and required libraries
# ==============================================================================

compile_program ()
{
	def_sys_dir=$1
	src_sys_dir=$2
	program_name=$3

	shift 3

	set_current_arch_type

#--------------------------------------
# Check and set options for second call
#--------------------------------------

	arch_mode=
	cgns_define_flag="-D_ENABLE_CGNS_LIB_"
	clean_lib_flag=
	clean_lib_list=
	compile_lib_flag=yes
	compile_exe_flag=yes
	debug_flag=
	debug_code_flag="_ENABLE_DEBUG_CODE_"
	debug_code_define_flag=
	define_flag_list=
	dep_lib_flag=yes
	egads_define_flag="-D_ENABLE_EGADS_"
	egads_lite_define_flag=
	int64_flag=
	libmesh_define_flag="-D_ENABLE_MESH_LIB_"
	mpi_code=
	mpi_define_flag="-D_ENABLE_UG_MP_MPI_"
	opt_define_flags=
	opt_include_dirs=
	opt_libs=
	opt_lib_dirs=
	prof_flag=
	sanitize_flag=
	shared_lib_flag=
	static_flag=
	use_def_sys_dir=
        version_flag=yes
	X11_code="no"
	X11_define_flag=

	error_flag=

	while [ X"$1" != X-- ]; do

		case "$1"
		in

			"")		set -- -- $@
					;;

			-nodep)		dep_lib_flag=
					shift 1
					;;

			-nover)		version_flag=
					shift 1
					;;

			-clean)		clean_lib_flag=yes
					shift 1
					;;

			-exe)		compile_lib_flag=
					shift 1
					;;

			-lib)		compile_exe_flag=
					shift 1
					;;

			-shared)	shared_lib_flag=yes
					static_flag=
					shift 1
					;;

			-sys)		use_def_sys_dir=yes
					shift 1
					;;

			--D*)		opt_define_flags="${opt_define_flags} `echo $1| sed s/--D/-D/`"
					shift 1
					;;

			-D*)		define_flag_list="${define_flag_list} `echo $1| sed s/-D//`"
					opt_define_flags="${opt_define_flags} $1"
					shift 1
					;;

			-U*)		define_flag_list="${define_flag_list} `echo $1| sed s/-U//`"
					opt_define_flags="${opt_define_flags} $1"
					shift 1
					;;

			-I*)		opt_include_dirs="${opt_include_dirs} `echo $1| sed s/-I//`"
					shift 1
					;;

			-l*)		opt_libs="${opt_libs} $1"
					shift 1
					;;

			-L*)		opt_lib_dirs="${opt_lib_dirs} $1"
					shift 1
					;;

			-debug_code)	define_flag_list="${define_flag_list} ${debug_code_flag}"
					debug_code_define_flag="-D${debug_code_flag}"
					shift 1
					;;

			-no_cgns)	cgns_define_flag=
					shift 1
					;;

			-no_libmesh)	libmesh_define_flag=
					shift 1
					;;

			-no_egads)	egads_define_flag=
					shift 1
					;;

			-x)		X11_code=
					X11_define_flag="-D_ENABLE_XPLT2_"
					shift 1
					;;

			-static)	static_flag=yes
					shift 1
					;;

			-g|-debug)	debug_flag=yes
					if [ "${OS_type}" = "Darwin" ]; then
						sanitize_flag=yes
					fi
					shift 1
					;;

			-64|-int64)	define_flag_list="${define_flag_list} _UG_LONG_LONG_INT_"
					opt_define_flags="${opt_define_flags} -D_UG_LONG_LONG_INT_"
					int64_flag=yes
					shift 1
					;;

			-prof)		prof_flag=yes
					shift 1
					;;

			-x86)		arch_mode=$1
					shift 1
					;;

			-*)		echo "*** invalid option \"$1\" ***"
					error_flag=yes
					shift 1
					;;

			*)		echo "*** invalid option \"$1\" ***"
					error_flag=yes
					shift 1
					;;
		esac
	done

	if [ "${error_flag}" != "" ]; then

		my_list_usage
		exit
	fi

	if [ "${sanitize_flag}" = "yes" ] || [ "${prof_flag}" = "yes" ]; then

                cgns_define_flag=
		int64_flag=
	fi

	if [ "${debug_flag}" = "yes" ]; then

                cgns_define_flag=
	fi

	if [ "${int64_flag}" = "yes"  ]; then

                cgns_define_flag=
                egads_define_flag=
		X11_code=no
		X11_define_flag=
	fi

	if [ "${base_arch_type}" = "WIN" ]; then

		mpi_code=no
		X11_code=no
		X11_define_flag=
	fi

	opt_define_flags="${opt_define_flags} ${debug_code_define_flag}"

#--------------------------------------------
# End of check and set options on second call
#--------------------------------------------

	if [ "${debug_flag}" != "" ] && [ "${int64_flag}" != "" ]; then

		compile_suffix=_gl

#--------------------
# Set sanitize suffix
#--------------------

	elif [ "${sanitize_flag}" != "" ]; then

		compile_suffix=_gs

#-----------------
# Set debug suffix
#-----------------

	elif [ "${debug_flag}" != "" ]; then

		compile_suffix=_g

#-----------------
# Set int64 suffix
#-------------

	elif [ "${int64_flag}" != "" ]; then

		compile_suffix=_l

#-----------------
# Set gprof suffix
#-----------------

	elif [ "${prof_flag}" != "" ]; then

		compile_suffix=_p

#-----------------
# Set no suffix
#-----------------

	else

		compile_suffix=
	fi

# -----------------------------------
# Set system commands and directories
# -----------------------------------

	set_current_arch_type ${arch_mode}

	sys_arch_dir=${src_sys_dir}/${arch_type}
	sys_exe_dir=${src_sys_dir}/${arch_type}/bin
	sys_lib_dir=${src_sys_dir}/${arch_type}/lib
	sys_shared_lib_dir=${src_sys_dir}/${arch_type}/bin

	src_dir=${src_sys_dir}/src

	def_src_dir=${def_sys_dir}/src
	def_sys_lib_dir=${def_sys_dir}/${arch_type}/lib

# --------------------------
# Check for arch directories
# --------------------------

	if [ ! -d "${src_dir}/${program_name}" ]; then

		echo "*** ${program_name} not found in source directory ${src_dir} ***"
		exit
	fi

# --------------------------
# Check for arch directories
# --------------------------

	if [ ! -d "${sys_arch_dir}" ]; then

		echo "CREATING DIRECTORY ${sys_arch_dir}"
		mkdir ${sys_arch_dir}
	fi

# ------------------
# Set compiler flags
# ------------------

	set_compiler_flags

# ----------------------------
# Update all related libraries
# ----------------------------

	return_flag=

	compile_files="${src_dir}/${program_name}/*.compile"

	for compile_file in ${compile_files}; do

		if [ -r "${compile_file}" ]; then

			. ${compile_file}

			if [ "${return_flag}" = "yes" ]; then
				return
			fi
		fi
	done

	lib_list_files="${src_dir}/${program_name}/*.lib_list"

	lib_names_1=
	lib_names_2=
	lib_names_3=

	error_flag=yes

	other_libs=
	lib_list=

	compile_lib_names=${program_name}

	for lib_list_file in ${lib_list_files}; do

		if [ -r "${lib_list_file}" ]; then

			. ${lib_list_file}

			compile_lib_names="${lib_names_1} ${lib_names_2} ${lib_names_3}"

			break
		fi
	done

	check_cgns
	check_egads
	check_libmesh
	check_mpi
	check_X11

	base_opt_define_flags="${opt_define_flags}"

	for compile_lib_name in ${compile_lib_names}; do

		if [ "${use_def_sys_dir}" = "yes" ] && [ "${def_sys_dir}" != "${src_sys_dir}" ]; then

			if [ "${compile_lib_name}" = "${program_name}" ]; then

				compile_lib_files="${def_src_dir}/${compile_lib_name}/*.compile_lib ${def_src_dir}/${compile_lib_name}/main/*.compile_lib"
			else

				compile_lib_files="${def_src_dir}/${compile_lib_name}/${compile_lib_name}*.compile_lib"
			fi

			for compile_lib_file in ${compile_lib_files}; do

				if [ -r "${compile_lib_file}" ]; then

					. ${compile_lib_file}
				fi
			done
		fi

		if [ "${compile_lib_name}" = "${program_name}" ]; then

			compile_lib_files="${src_dir}/${compile_lib_name}/*.compile_lib ${src_dir}/${compile_lib_name}/main/*.compile_lib"
		else

			compile_lib_files="${src_dir}/${compile_lib_name}/${compile_lib_name}*.compile_lib"
		fi

		library_log_check=yes

		for compile_lib_file in ${compile_lib_files}; do

			opt_define_flags=${base_opt_define_flags}

			lib_base_name=${compile_lib_name}

			if [ -r "${compile_lib_file}" ]; then

				cpp_opt0_files=
				cpp_opt1_files=
				cpp_opt2_files=
				c_opt0_files=
				c_opt1_files=
				c_opt2_files=
				f_opt0_files=
				f_opt1_files=
				f_opt2_files=

				source_dir=${compile_lib_name}

				org_source_dir=${source_dir}

				cpp_file_suffix=.cpp
				c_file_suffix=.c
				f_file_suffix=.f

				. ${compile_lib_file}

				include_dir="${opt_include_dirs} ${src_dir}/${source_dir}"

				if [ "${source_dir}" != "${org_source_dir}" ]; then

					include_dir="${include_dir} ${src_dir}/${org_source_dir}"
				fi

				if [ "${use_def_sys_dir}" = "yes" ] && [ "${def_sys_dir}" != "${src_sys_dir}" ]; then

					include_dir="${include_dir} ${def_src_dir}/${source_dir}"

					if [ "${source_dir}" != "${org_source_dir}" ]; then

						include_dir="${include_dir} ${def_src_dir}/${org_source_dir}"
					fi
				fi

				source_dir=${src_dir}/${source_dir}

				lib_list="${lib_list} ${lib_base_name}"

				if [ "${compile_lib_flag}" != "" ] && [ -d ${source_dir} ]; then

					if [ "${dep_lib_flag}" = "" ]; then

						compile_dir=${src_dir}/${program_name}

						base_dir=${source_dir}

						if [ "${compile_dir}" = "${base_dir}" ]; then

							compile_flag=yes
						else

							base_dir=`dirname ${base_dir}`

							if [ "${compile_dir}" = "${base_dir}" ]; then

								compile_flag=yes
							else

								base_dir=`dirname ${base_dir}`

								if [ "${compile_dir}" = "${base_dir}" ]; then

									compile_flag=yes
								else

									compile_flag=
								fi
							fi
						fi
					else

						compile_flag=yes
					fi

					if [ "${compile_flag}" != "" ]; then

						set_compiler_flags

						lib_name=${lib_prefix}${lib_base_name}${compile_suffix}.${static_lib_suffix}

						if [ "${library_log_check}" = "" ] || [ "`echo ${compile_library_log} | grep ${lib_name}`" = "" ]; then

							library_log_check=

							compile_library_log="${compile_library_log} ${lib_name}"

							compile_library
						fi
					fi
				fi
			else

				lib_list="${lib_list} ${lib_base_name}"
			fi
		done
	done

# -------------
# Set libs list
# -------------

	libs=

	for lib_base_name in ${lib_list}; do

		libs="${libs} -l${lib_base_name}${compile_suffix}"
	done

	libs="-L${sys_lib_dir} ${libs} ${libs} ${libs}"

	if [ "${shared_lib_flag}" = "yes" ]; then

		libs="-L${sys_shared_lib_dir} ${libs}"
	fi

	if [ "${use_def_sys_dir}" = "yes" ] && [ "${def_sys_dir}" != "${src_sys_dir}" ]; then

		libs="${libs} -L${def_sys_lib_dir}"
	fi

# ------------------
# Create program lib
# ------------------

	if [ "${program_name}" = "aflr2c" ] || [ "${program_name}" = "aflr3" ] || [ "${program_name}" = "aflr4" ] || [ "${program_name}" = "aflr43" ]; then
		make_prog_lib ${program_name}
	fi

# ------------------------------------
# Compile program and link executable
# ------------------------------------

	base_other_libs="${other_libs}"

	for compile_file in ${compile_files}; do

		if [ -r "${compile_file}" ]; then

			opt_define_flags="${base_opt_define_flags}"

			exe_base_name=${program_name}
			main_dir=${program_name}
			main_program_base_name=${program_name}
			main_program_files=

			org_main_dir=${main_dir}

			f_main=
			cpp_main=

			cpp_file_suffix=.cpp
			c_file_suffix=.c
			f_file_suffix=.f

			other_libs="${base_other_libs}"

			. ${compile_file}

			include_dir="${opt_include_dirs} ${src_dir}/${main_dir}"

			if [ "${main_dir}" != "${org_main_dir}" ]; then

				include_dir="${include_dir} ${src_dir}/${org_main_dir}"
			fi

			if [ "${use_def_sys_dir}" = "yes" ] && [ "${def_sys_dir}" != "${src_sys_dir}" ]; then

				include_dir="${include_dir} ${def_src_dir}/${main_dir}"

				if [ "${main_dir}" != "${org_main_dir}" ]; then

					include_dir="${include_dir} ${def_src_dir}/${org_main_dir}"
				fi
			fi

			main_dir=${src_dir}/${main_dir}

			if [ "${compile_exe_flag}" != "" ]; then

				set_compiler_flags

				compile_executable

				opt_define_flags="${base_opt_define_flags}"
			fi
		else

			if [ "${lib_list}" = "" ] || [ "${error_flag}" != "" ]; then

				echo "*** no library routines or main programs found to compile ***"
			fi
		fi
	done
}

#===============================================================================
# Main script
#===============================================================================

# -----------------------------------------
# Set system directories
# -----------------------------------------

set_tmp_dir
set_sys_dir

def_sys_dir=${sys_dir}
src_sys_dir=${sys_dir}

# -----------------------------------------
# Get user options
# -----------------------------------------

set_current_arch_type

user_options=

user_option_files="${HOME}/bin/tools/`basename $0`.opt"

for user_option_file in ${user_option_files}; do

	if [ -r ${user_option_file} ]; then

		. ${user_option_file}

		break
	fi
done

# ------------------
# Set compiler flags
# ------------------

set_current_arch_type

set_compiler_flags

# ---------------------
# Check and set options
# ---------------------

make_win_dll_flag=
option_list=
other_options=
program_list=
sys_version_option=
version_dir=

while [ X"$1" != X-- ]; do

	case "$1"
	in

		"")		set -- -- $@
				;;

		-h)		my_list_summary
				exit
				;;

		-help)		my_list_full | less
				exit
				;;

		-v)		if [ "$2" = "" ]; then
					echo ""
					echo "*** option $1 requires a source directory name ***"
					my_list_usage
					exit
				fi
				set_version_dir $2
				src_sys_dir="${version_dir}"
				sys_version_option="-v $2"
				shift 2
				;;

		-make_win_dll)	make_win_dll_flag=yes
				shift 1
				;;

		*)		other_options="${other_options} $1"
				shift 1
				;;
	esac
done

if [ "${base_arch_type}" != "WIN" ]; then
	make_win_dll_flag=
fi

if [ "${user_options}" != "" ]; then

	ifs_def="$IFS"

	new_other_options=

	for other_option in ${other_options}; do

		found_flag=

		for user_option in ${user_options}; do

			IFS=":"

			set -- ${user_option}

			user_option_flag="$1"

			user_option_type_and_items="$2"

			if [ "${other_option}" = "${user_option_flag}" ]; then

				#if [ "${other_option}" = "ALL" ] && [ "${base_arch_type}" = "WIN" ]; then
					#make_win_dll_flag=yes
				#fi

				IFS=";"

				set -- ${user_option_type_and_items}

				user_option_type="$1"

				list1="$2"

				list2="$3"

				IFS=","

				if [ "${list1}" != "" ]; then

					set -- ${list1}

					user_option_list=$1

					shift 1

					for option in $*; do

						user_option_list="${user_option_list} ${option}"
					done

					if [ "${user_option_list}" = "NULL" ]; then

						user_option_list=
					fi
				else

					user_option_list=
				fi

				if [ "${list2}" != "" ]; then

					set -- ${list2}

					user_option_items=$1

					shift 1

					for item in $*; do

						user_option_items="${user_option_items} ${item}"
					done
				else

					user_option_items=
				fi

				IFS="${ifs_def}"

				if [ "${user_option_type}" = "prog_list" ]; then

					program_list="${program_list} ${user_option_items}"
				fi

				if [ "${user_option_list}" != "" ]; then

					option_list="${option_list} ${user_option_list}"
				fi

				found_flag=yes

				break
			fi

			IFS="${ifs_def}"
		done

		if [ "${found_flag}" = "" ]; then

			new_other_options="${new_other_options} ${other_option}"
		fi
	done

	other_options="${new_other_options}"
fi

set -- ${other_options}

while [ X"$1" != X-- ]; do

	case "$1"
	in

		"")		set -- -- $@
				;;

		-*)		option_list="${option_list} $1"
				shift 1
				;;

		*)		program_list="${program_list} $1"
				shift 1
				;;
	esac
done

if [ "${program_list}" = "" ]; then

	echo "*** no program or library name specified ***"
	my_list_usage
	exit
fi

# ---------------
# Update programs
# ---------------

ifs_def="$IFS"

compile_library_log=
org_src_sys_dir="${src_sys_dir}"
org_sys_version_option="${sys_version_option}"

for program_name_w_options in ${program_list}; do

	program_comment=

	IFS="+"

	set -- ${program_name_w_options}

	program_name="$1"

	shift 1

	IFS="${ifs_def}"

	program_option_list=

	for option in $*; do

		program_option_list="${program_option_list} -${option}"
	done

	program_comment="with option(s)${program_option_list}${option_list} "

	echo ""
	echo "updating ${program_name} ${program_comment}"
	echo ""

	compile_program ${def_sys_dir} ${src_sys_dir} ${program_name} ${program_option_list} ${option_list}
done

IFS="${ifs_def}"

if [ "${make_win_dll_flag}" = "yes" ]; then

	make_win_dll
fi

