#!/bin/sh
#$Id: simsys_make_bin,v 1.44 2023/03/14 02:36:34 marcum Exp $

#*******************************************************************************
# Create a bin directory for SimSys files.
#*******************************************************************************

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

script_path="`readlink $0`"

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

	script_path="$0"
fi

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

. "`dirname ${script_path}`/script_functions/set_current_arch_type"
. "`dirname ${script_path}`/script_functions/output_arch_type"
. "`dirname ${script_path}`/script_functions/set_sys_dir"
. "`dirname ${script_path}`/script_functions/set_version_dir"

#===============================================================================
# Help function
#===============================================================================

my_list_usage ()
{
	echo "
Usage:

`basename $0` [-h,-help] [-copy,-link] [-rm] [-clean] [-cwd,-dir dir_name] [-sbin] [-base base_arch_type] [-v version_name] [-sudo]

Copy and/or link SimSys executables, dynamic libraries and sbin script files to
to a specified directory. Dynamic libraries (if any) are included only in copy
mode. By default copies and/or links of the files are put in a bin directory
named simsys_home/bin/ within the installed SimSys home directory, simsys_home/.
The bin directory is created if it does not exist. If a directory is specified
(see -dir option) or the current working directory is specified (see -cwd
option) then that directory is used as the destination for all files.

   -h			: List usage.
   -help		: List usage.
   -copy		: Copy files, including dynamic libraries, if any. If
			  -sbin is used then also create links to developer
                          scripts.
   -link		: Create links only to all files. Note that in this mode
			  the dynamic library directory is not copied or linked,
			  as it is not needed.
   -rm			: Remove all files in destination directory. By default
			  only existing files with the same name as those copied
			  or linked are removed.
   -clean		: Remove all SimSys files (known by this script) in the
			  destination directory (whether they are links or
			  copies) and then exit. If -rm is also specified then
			  all files in the destination are removed.
   -cwd			: Use the current working directory to create copies
			  and/or links. The default is to create or use the
			  directory ${sys_dir}/bin.
   -dir dir_name	: Use existing directory \"dir_name\" to create copies
			  and/or links. The default is to create or use the
			  directory ${sys_dir}/bin.
   -sbin		: Create links to SimSys sbin script files. The default
			  is to only copy or link script files required to run
			  the executables or view documentation.
   -base base_arch_type	: Use executables and dynamic libraries for
			  \"base_arch_type.\" Valid base architectures include
			  `output_arch_type -ab`)
			  The default base architecture is `output_arch_type -b`.
   -v version_name	: Use \"version_name\" system directory as the source
			  for all files to be copied or linked. This option does
			  does not change the destination directory. The default
			  system directory is ${sys_dir}.
   -sudo		: Run file creation/deletion/management portions of this
			  script as the \"root\" user. The default is to run as
			  user ${USER}.

Note that either -copy or -link is required as an option to do anything other
than list usage.
"
}

#===============================================================================
# Function to set list of script files in destination directory
#===============================================================================

set_script_names ()
{
	script_names=

	names="${from_sys_dir}/sbin/*"

	for name in ${names}; do

		if [ ! -d "${name}" ] && [ -e "${name}" ]; then

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

				name="`basename ${name}`"

				script_names="${script_names} ${name}"
			fi
		fi
	done
}

#===============================================================================
# Function to set list of executable files in destination directory
# Loop over 32-bit and 64-bit (if it is the default) architecture types
#===============================================================================

set_exe_names ()
{
	exe_names=

	names="${from_sys_dir}/${arch_type}/bin/*.exe"

	for name in ${names}; do

		if [ ! -d "${name}" ] && [ -x "${name}" ]; then

			name="`basename ${name} | sed s/\.exe//`"

			if  [ ! "`echo ${name} | grep _dll`" ]; then

				exe_names="${exe_names} ${name}"
			fi
		fi
	done
}

#===============================================================================
# Function to set list of dynamic library files in destination directory
# Loop over 32-bit and 64-bit (if it is the default) architecture types
#===============================================================================

set_dlib_names ()
{
	dlib_names=

	names="${from_sys_dir}/${arch_type}/bin/*.dylib ${from_sys_dir}/${arch_type}/bin/*.so"

	for dlib_name in ${names}; do

		if [ ! -d "${dlib_name}" ] && [ -e "${dlib_name}" ]; then

			name="`basename ${dlib_name}`"

			if [ ! "`echo ${name} | grep _g`" ]; then

				dlib_names="${dlib_names} ${name}"
			fi
		fi
	done
}

#===============================================================================
# Function to remove files in destination directory
#===============================================================================

remove_files ()
{
	if [ "$1" = "-" ]; then

		suffix=
	else
		suffix=$1/
	fi

	shift 1

	names="$*"

	for name in ${names}; do

		if [ -e "${to_dir}/${name}" ]; then

			file_names="${to_dir}/${name}*"

			for file_name in ${file_names}; do

				echo "removing ${file_name}${suffix}"

				${sudo_command} /bin/rm -rf ${file_name}${suffix}
			done
		fi
	done
}

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

set_sys_dir

#-------------------------------------------------------------------------------
# Set options
#-------------------------------------------------------------------------------

if [ "$1" = ""  ]; then

	echo "*** one of the options -copy or -link is required ***"
	my_list_usage
	exit
fi

arch_option_base_flag=
clean_option=
from_sys_dir="${sys_dir}"
link_option=
rm_option=
sbin_option=
to_dir="${sys_dir}/bin"
create_to_dir=yes
sudo_command=

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

	case "$1"
	in

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

		-h|-help)	my_list_usage
				exit
				;;

		-copy)		link_option=no
				shift 1
				;;

		-link)		link_option=yes
				shift 1
				;;

		-rm)		rm_option=yes
				shift 1
				;;

		-clean)		clean_option=yes
				shift 1
				;;

		-cwd)		to_dir="`pwd`"
				create_to_dir=
				shift 1
				;;

		-dir)		if [ "$2" = "" ]; then
					echo ""
					echo "*** option $1 also requires a directory name ***"
					my_list_usage
					exit
				fi
				create_to_dir=
				to_dir="$2"
				shift 2
				;;

		-sbin)		sbin_option=yes
				shift 1
				;;

		-base)		if [ "$2" = "" ]; then
					echo ""
					echo "*** option $1 also requires a base architecture type ***"
					my_list_usage
					exit
				fi
				base_arch_type="$2"
				arch_option_base_flag="-base ${base_arch_type}"
				shift 2
				;;

		-v)		if [ "$2" = "" ]; then
					echo ""
					echo "*** option $1 also requires a version name ***"
					my_list_usage
					exit
				fi
				set_version_dir $2
				from_sys_dir="${version_dir}"
				if [ ! -d "${version_dir}" ]; then
					echo "*** version directory for $2 not found ***"
					exit
				fi
				shift 2
				;;

		-sudo)		sudo_command=sudo
				shift 1
				;;

		*)		echo ""
				echo "*** invalid option $1 ***"
				my_list_usage
				exit
				;;
	esac
done

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

	echo "*** one of the options -copy or -link is required ***"
	my_list_usage
	exit
fi

#-------------------------------------------------------------------------------
# Create bin directory
#-------------------------------------------------------------------------------

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

	if [ "${create_to_dir}" ]; then

		${sudo_command} mkdir ${to_dir}
		${sudo_command} chmod g+rxs,o+rx ${to_dir}
	else

		echo "*** unable to find directory ${to_dir} ***"
		exit
	fi
fi

#-------------------------------------------------------------------------------
# Set command to copy or link mode
#-------------------------------------------------------------------------------


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

	link_command="${sudo_command} ln -sfv"
else
	link_command="${sudo_command} rsync -vptlr"
fi

#-------------------------------------------------------------------------------
# Set 32-bit and 64-bit (if it is the default) architecture types
#-------------------------------------------------------------------------------

set_current_arch_type ${arch_option_base_flag}

#-------------------------------------------------------------------------------
# Remove script, executable, and dynamic library files in destination directory
#-------------------------------------------------------------------------------

if [ "${rm_option}" ]; then

	file_names="${to_dir}/*"

	for file_name in ${file_names}; do

		echo "removing ${file_name}"

		${sudo_command} /bin/rm -rf ${file_name}
	done
else
	set_script_names

	remove_files - ${script_names}

	set_current_arch_type ${arch_option_base_flag}

	set_exe_names

	remove_files - ${exe_names}
	remove_files .exe ${exe_names}

	set_dlib_names

	remove_files - ${dlib_names}
fi

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

	exit
fi

#-------------------------------------------------------------------------------
# Copy or link script files
#-------------------------------------------------------------------------------

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

	${link_command} ${from_sys_dir}/sbin/simsys_doc ${to_dir}
else
	${link_command} ${from_sys_dir}/sbin/simsys_doc.bat ${to_dir}
fi

if [ "${sbin_option}" ]; then  2> /dev/null

	set_script_names

	for script_name in ${script_names}; do

		if [ "${script_name}" != "simsys_www" ] || [ "${USER}" = "marcum" ]; then

			${link_command} ${from_sys_dir}/sbin/${script_name} ${to_dir}
		fi
	done
fi

#-------------------------------------------------------------------------------
# Copy or link executables
#-------------------------------------------------------------------------------

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

	exe_suffix=.exe
else
	exe_suffix=
fi

set_current_arch_type ${arch_option_base_flag}

set_exe_names

for exe_name in ${exe_names}; do

	${link_command} ${from_sys_dir}/${arch_type}/bin/${exe_name}.exe ${to_dir}/${exe_name}${exe_suffix}
done

#-------------------------------------------------------------------------------
# Copy dynamic libraries
#-------------------------------------------------------------------------------

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

	set_current_arch_type ${arch_option_base_flag} ${arch_option_flag}

	set_dlib_names

	for dlib_name in ${dlib_names}; do

		${link_command} ${from_sys_dir}/${arch_type}/bin/${dlib_name} ${to_dir}/
	done
fi
