#!/bin/bash
#$Id: simsys_www,v 1.57 2025/10/05 18:17:13 marcum Exp $

#*******************************************************************************
# Create www site package archive files 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_sys_dir"
. "`dirname ${script_path}`/script_functions/set_version_dir"

set_sys_dir

dropbox_data_dir=$HOME/Dropbox
local_mirror_data_dir=$HOME/www/software
www_site_host=$USER@Web.HPC.MsState.Edu 
www_site_data_dir=/www/data/www.simcenter.msstate.edu/software

package_name_list="\
AFLR2 \
AFLR2C \
AFLR2C_LIB \
AFLR3 \
AFLR3_LIB \
AFLR4 \
AFLR4_LIB \
AFLR_LIB \
BLOOM3 \
BLOOM3_LIB \
BLOOM3_SRC \
GRID_TOOLS \
UVMAP_SRC \
ug_io \
doc \
WOLF \
"

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

my_list_usage ()
{
	echo "
Update SimSys package archive files on local web mirror, DropBox, or HPC2 web.

`basename $0` [-h,-help] [-v version_name] [-dropbox,-hpc] packages

	If neither -dropbox or -hpc are specified then local web mirror package
	archive files are updated. The local web mirror directory is
	${local_mirror_data_dir}

	-h		: List usage.
	-help		: List usage.
	-v version_name	: Use \"version_name\" system directory for all
			  files included within package (the default is to use
			  ${sys_dir}).
			  Not applicable with [-dropbox] or [-hpc] option.
	-dropbox:	  Update package archive files on Dropbox.
	-hpc		: Update package archive files on HPC2 web server.
	packages	: Update package archive files for \"packages\".
			  If nothing is specified then packages is set to ALL
			  Not applicable with [-dropbox] or [-hpc] option.

Known package Names:

	${package_name_list}
	or ALL (all predefined package archive files listed here)
"

}

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

#-------------------------------------------------------------------------------
# List documentation
#-------------------------------------------------------------------------------

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

	my_list_usage
	exit
fi

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

umask 007

input_package_list=
update_dropbox=
update_hpc=

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

	case "$1"
	in

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

		-h|-help)	my_list_usage
				exit
				;;

		-v)		if [ "$2" = "" ]; then
					echo "
*** option $1 also requires a version name ***"
					my_list_usage
					exit
				fi
				set_version_dir $2
				sys_dir="${version_dir}"
				shift 2
				;;

		-dropbox)	update_dropbox="yes"
				shift 1
				;;

		-hpc)		update_hpc="yes"
				shift 1
				;;

		-*)		echo "
*** invalid option $1 ***"
				my_list_usage
				exit
				;;

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

#-------------------------------------------------------------------------------
# Update DropBox
#-------------------------------------------------------------------------------

if [ "${update_dropbox}" ]; then

	echo "
Using ${local_mirror_data_dir}/downloads/ for package archive files
"

	echo "
Update AFLR package archive files on Dropbox site ..."

	dir_list="AFLR2 AFLR2C AFLR2C_LIB AFLR3 AFLR3_LIB AFLR4 AFLR4_LIB AFLR_LIB BLOOM3 BLOOM3_LIB GRID_TOOLS UG_IO"

	for dir in ${dir_list}; do

		if [ "${dir}" = "UG_IO" ]; then
			dir1=ug_io
		else
			dir1=${dir}
		fi

		rsync -Cauv --delete --exclude=*.php --exclude=special ${local_mirror_data_dir}/downloads/${dir1}/ ${dropbox_data_dir}/AFLR/${dir}/
		echo ""
	done

	echo "
Update MIT package archive files on Dropbox site ..."

	dir_list="UVMAP_SRC"

	for dir in ${dir_list}; do
		rsync -Cauv --delete --exclude=*.php --exclude=special ${local_mirror_data_dir}/downloads/${dir}/ ${dropbox_data_dir}/MIT/${dir}/
		echo ""
	done

	echo "
Update INRIA package archive files on Dropbox site ..."

	dir_list="BLOOM3_SRC WOLF"

	for dir in ${dir_list}; do
		rsync -Cauv --delete --exclude=*.php --exclude=special ${local_mirror_data_dir}/downloads/${dir}/ ${dropbox_data_dir}/INRIA/${dir}/
		echo ""
	done

	exit

#-------------------------------------------------------------------------------
# Update HPC2 web server site
#-------------------------------------------------------------------------------

elif [ "${update_hpc}" ]; then

	echo "
Using ${local_mirror_data_dir}/downloads/ for package archive files
"

	echo "
Updating documentation/ files on HPC2 web server www site..."

	rsync -Cauv --delete --exclude=*.php --exclude=*.bak --exclude=special --exclude=common --exclude=disabled --exclude=REPORTS --exclude=TEMPLATE --exclude=CHEM_GROUP --exclude=LOCI_GROUP --exclude=SM --exclude=uvmap ${local_mirror_data_dir}/documentation/ ${www_site_host}:${www_site_data_dir}/documentation/

	echo "
Updating public/ files on HPC2 web server www site ..."

	rsync -Cauv --delete --exclude=*.php --exclude=*.bak --exclude=special --exclude=common --exclude=disabled --exclude=REPORTS --exclude=TEMPLATE --exclude=CHEM_GROUP --exclude=LOCI_GROUP ${local_mirror_data_dir}/public/ ${www_site_host}:${www_site_data_dir}/public/

	echo "
Updating downloads/ files on HPC2 web server www site ..."

	rsync -Cauv --delete --exclude=*.php --exclude=special --exclude=common --exclude=disabled --exclude=REPORTS --exclude=TEMPLATE --exclude=CHEM_GROUP --exclude=LOCI_GROUP --exclude=AFLR2C --exclude=AFLR2C_LIB --exclude=BLOOM3_SRC --exclude=SM --exclude=UVMAP_SRC --exclude=WOLF --exclude=doc --exclude=ug_io ${local_mirror_data_dir}/downloads/ ${www_site_host}:${www_site_data_dir}/downloads/
	echo ""

	exit
fi

#-------------------------------------------------------------------------------
# Create tar package archive files and update local web mirror
#-------------------------------------------------------------------------------

echo "
Using directory ${sys_dir} for package archive file contents"

package_name_list="\
AFLR2 \
AFLR2C \
AFLR2C_LIB \
AFLR3 \
AFLR3_LIB \
AFLR4 \
AFLR4_LIB \
AFLR_LIB \
BLOOM3 \
BLOOM3_LIB \
BLOOM3_SRC \
GRID_TOOLS \
UVMAP_SRC \
WOLF \
doc \
ug_io \
"

package_list=""

for input_package in ${input_package_list}; do

	if [ "${input_package}" = "ALL" ]; then

		package_list="${package_name_list}"

		break
	else
		found_flag=

		for package in ${package_name_list}; do

			if [ "${input_package}" = "${package}" ]; then

				package_list="${package_list} ${input_package}"
				found_flag="yes"
				break
			fi
		done

		if [ "${found_flag}" = "" ]; then
			echo "
*** no package named ${input_package} found ***"
			my_list_usage
			exit
		fi
	fi
done

input_package_list="${package_list}"

update_package_list=""

for input_package in ${input_package_list}; do

	if [ "${input_package}" = "AFLR3" ]; then

		update_package_list="${update_package_list} AFLR3 AFLR43"

	elif [ "${input_package}" = "GRID_TOOLS" ]; then

		update_package_list="${update_package_list} GRID_TOOLS GRID_TOOLS_SRC"

	elif [ "${input_package}" = "ug_io" ]; then

		update_package_list="${update_package_list} UG_IO UG_IO_SRC"
	else
		update_package_list="${update_package_list} ${input_package}"
	fi
done

for package in ${update_package_list}; do

	if [ "${package}" = "doc" ]; then

		doc_dir_list="aflr2 aflr2c aflr3 aflr4 aflr43 bloom3 grid_tools ug_io uvmap system"

		for dir in ${doc_dir_list}; do
			echo "
Updating documentation/${dir} files on local www site..."

			rsync -Cauv --delete --exclude=*.bak --exclude=special --exclude=*.docx --exclude=docx --exclude=.DS_Store --exclude=Obsolete --exclude=Save --exclude=Testing --exclude=docx ${sys_dir}/doc/${dir}/ ${local_mirror_data_dir}/documentation/${dir}/
		done
	else

		package_dir=${package}
		package_name_format=",*.*.*,*.*"
		sys_file_list="\
README_packages.pdf \
README_install.pdf \
README.pdf"

		if [ "${package}" = "AFLR2" ]; then

			sys_file_list="${sys_file_list} \
License.pdf \
Release_Notes_aflr2.txt \
Release_Notes_aflr2c.txt"

		elif [ "${package}" = "AFLR2C" ]; then

			sys_file_list="${sys_file_list} \
License.pdf \
Release_Notes_aflr2c.txt"

		elif [ "${package}" = "AFLR2C_LIB" ]; then

			package_name_format=",*.*.*,*.*.*.*"
			sys_file_list="${sys_file_list} \
License.pdf \
OpenLicense.pdf \
Release_Notes_aflr2c.txt \
README_developer_install.pdf"

		elif [ "${package}" = "AFLR3" ]; then

			sys_file_list="${sys_file_list} \
License.pdf \
Release_Notes_aflr3.txt"

		elif [ "${package}" = "AFLR3_LIB" ]; then

			package_name_format=",*.*.*,*.*.*.*"
			sys_file_list="${sys_file_list} \
License.pdf \
OpenLicense.pdf \
Release_Notes_aflr3.txt \
README_developer_install.pdf"

		elif [ "${package}" = "AFLR43" ]; then

			package_dir="AFLR3"
			sys_file_list="${sys_file_list} \
License.pdf \
Release_Notes_aflr43.txt"

		elif [ "${package}" = "AFLR4" ]; then

			sys_file_list="${sys_file_list} \
License.pdf \
Release_Notes_aflr4.txt"

		elif [ "${package}" = "AFLR4_LIB" ]; then

			package_name_format=",*.*.*,*.*.*.*"
			sys_file_list="${sys_file_list} \
License.pdf \
OpenLicense.pdf \
Release_Notes_aflr4.txt \
README_developer_install.pdf"

		elif [ "${package}" = "AFLR_LIB" ]; then

			package_name_format=",*.*.*,*.*.*.*"
			sys_file_list="${sys_file_list} \
License.pdf \
OpenLicense.pdf \
Release_Notes_aflr2c.txt \
Release_Notes_aflr3.txt \
Release_Notes_aflr4.txt \
README_developer_install.pdf"

		elif [ "${package}" = "BLOOM3" ]; then

			sys_file_list="${sys_file_list} \
License.pdf \
Release_Notes_bloom3.txt"

		elif [ "${package}" = "BLOOM3_LIB" ]; then

			package_name_format=",*.*.*,*.*.*.*"
			sys_file_list="${sys_file_list} \
License.pdf \
OpenLicense.pdf \
Release_Notes_bloom3.txt \
README_developer_install.pdf"

		elif [ "${package}" = "BLOOM3_SRC" ]; then

			package_name_format=",*.*.*.*"
			sys_file_list="${sys_file_list} \
License.pdf \
OpenLicense.pdf \
Release_Notes_bloom3.txt \
README_developer_install.pdf"

		elif [ "${package}" = "GRID_TOOLS" ]; then

			package_dir="GRID_TOOLS"
			sys_file_list="${sys_file_list} \
OpenLicense.pdf \
README_developer_install.pdf"

		elif [ "${package}" = "GRID_TOOLS_SRC" ]; then

			package_dir="GRID_TOOLS"
			package_name_format=",*.*.*.*"
			sys_file_list="${sys_file_list} \
OpenLicense.pdf \
README_developer_install.pdf"

		elif [ "${package}" = "UG_IO" ]; then

			package_dir=ug_io
			sys_file_list="${sys_file_list} \
OpenLicense.pdf \
Release_Notes_ug_io.txt \
README_developer_install.pdf"

		elif [ "${package}" = "UG_IO_SRC" ]; then

			package_dir=ug_io
			package_name_format=",*.*.*.*"
			sys_file_list="${sys_file_list} \
OpenLicense.pdf \
Release_Notes_ug_io.txt \
README_developer_install.pdf"

		elif [ "${package}" = "UVMAP_SRC" ]; then

			package_name_format=",*.*.*.*"
		fi

		if [ "${package}" != "WOLF" ]; then

			dir="${local_mirror_data_dir}/downloads/${package_dir}"

			chmod u+rwx,g+rwxs,o+rx ${dir} 2> /dev/null

			cd ${dir}

			/bin/rm -f ${package}${package_name_format}

			echo "
Creating new ${package} package archive files in ${dir}..."

			${script_dir}/simsys_archive -quiet -v ${sys_dir} ${package}

			echo "\
Updating ${package} README files in ${dir}..."

			for file in ${sys_file_list}; do

				rsync -Cauv ${sys_dir}/doc/system/${file} ./${file} > /dev/null 2>&1
			done

			chmod ug+rw,o+r * 2> /dev/null

		else

			dir="${local_mirror_data_dir}/downloads/WOLF/"

			echo "
Updating ${package} package archive files in ${dir}..."

			chmod u+rwx,g+rwxs,o+rx ${dir} 2> /dev/null

			/bin/rm -f ${dir}/WOLF_TAR*

			cd ${HOME}/simsys/src/wolf

			/bin/rm -f prog/*.o

			make -f makefile.dlm tar

			file="WOLF_TAR.`date +%Y.%m.%d`.tgz"

			/bin/cp ${file} ${local_mirror_data_dir}/downloads/WOLF/

			chmod ug+rw,o+r ${local_mirror_data_dir}/downloads/WOLF/${file} 2> /dev/null
		fi

	fi
done

echo ""
