#!/bin/ksh
#
# Component: viewer
#
# Description:
#  Control script of component viewer
#

#--- INIT ------------------------------
#needs $HOME

. $HOME/etc/ctrl.main.shi

[ -f $HOME/etc/setenv.sh ] && . $HOME/etc/setenv.sh


#--- FUNC ------------------------------

do_shell_()
{
	cat <<-EndOfInlineData
		VIEWER_HOME=$HOME/component/viewer
	EndOfInlineData
	
}

do_start_()
{
	if [ ! -d "${PLUGINS_DATA}" ] ; then
		# download and plugins would also create if not yet exists
		mkdir "${PLUGINS_DATA}" 2>/dev/null
	fi

	if [ ! -d "${PLUGINS_DATA}/x64" ] ; then
		# plugins would also create if not yet exists
		mkdir "${PLUGINS_DATA}/x64" 2>/dev/null
	fi

	if [ ! -d "${PLUGINS_DATA}/v8" ] ; then
		# plugins would also create if not yet exists
		mkdir "${PLUGINS_DATA}/v8" 2>/dev/null
	fi

	for f in "${VIEWER_HOME}/plugins.std"/* ; do
		# we don't want to link the x64 and v8 directories
		[[ "$f" == */plugins\.std/x64 ]] && continue;
		[[ "$f" == */plugins\.std/v8 ]] && continue;
		ln -sf $f "${PLUGINS_DATA}/"
	done
	
	for f in "${VIEWER_HOME}/plugins.std/x64"/* ; do 
		ln -sf $f "${PLUGINS_DATA}/x64/"
	done
	
	for f in "${VIEWER_HOME}/plugins.std/v8"/* ; do 
		ln -sf $f "${PLUGINS_DATA}/v8/"
	done

        # For backward compatibility
	ln -sf "${VIEWER_HOME}/dragon_commands.xml" "${DOWNLOAD_HOME}/htdocs/dragon_commands.xml"
	ln -sf "${VIEWER_HOME}/FluencyDirect-RadiologyRecognizer.msi" "${DOWNLOAD_HOME}/htdocs/FluencyDirect-RadiologyRecognizer.msi"
	ln -sf "${VIEWER_HOME}/repcomp.html" "${DOWNLOAD_HOME}/htdocs/repcomp.html"
	ln -sf "${VIEWER_HOME}/reptemp.html" "${DOWNLOAD_HOME}/htdocs/reptemp.html"
	ln -sf "${VIEWER_HOME}/spmiketypes.conf" "${DOWNLOAD_HOME}/htdocs/spmiketypes.conf"
	ln -sf "${VIEWER_HOME}/SpMikeCtrl.msi" "${DOWNLOAD_HOME}/htdocs/SpMikeCtrl.msi"
	ln -sf "${VIEWER_HOME}/SpMikeCtrl64.msi" "${DOWNLOAD_HOME}/htdocs/SpMikeCtrl64.msi"
	
} #do_start_()

#
# List files/tables to be backed up grouped by categories
#
# output lines should be in '<category> <type> <names>' format
#
#do_listbackup_()
#{
#	cat <<-EOFLISTBACKUP
#		data.plugins FILE ${DATA#$HOME/}/plugins
#		data.plugins.x64 FILE ${DATA#$HOME/}/plugins/x64
#		data.plugins.v8 FILE ${DATA#$HOME/}/plugins/v8
#	EOFLISTBACKUP
#}

#--- MAIN --------------------------------

check_action_ $1
if [ 0 = $? ]; then
	do_$1_

else
	echo "--- Component tools [ctrl $$]: Bad command: $1" >&2
	exit 1

fi
