#!/bin/csh -f
# args : (temp_dir,scripts_dir,config_dir,user,host,display
#
@ i = 1
while ($i <= $#argv)
	set param = (`echo $argv[$i] | cut -d "=" -f1`)
	set value = (`echo $argv[$i] | cut -d "=" -f2-100`)
	set $param = "$value"
	@ i++
end

alias get_param 'set \!:1 = `cat \!:2 | grep \!:1 | cut -d "=" -f2-100`'

if (! $?conf_file) then
	set conf_file = $scripts_dir/communicator.ini
endif

get_param JAVA_BIN $conf_file
get_param CONFIG_DIR $conf_file

if ( $CONFIG_DIR =~ *\$GENESIS_DIR* ) then
	set CONFIG_DIR = ("$GENESIS_DIR/"`echo $CONFIG_DIR | cut -d "/" -f2-100`)
endif

#
# Create Messenger Config Dir (if not exist)
#
set site_conf = $CONFIG_DIR/site
set host_conf = $CONFIG_DIR/hosts/${host}
set user_conf = $CONFIG_DIR/users/${user}

if ( ! -d $CONFIG_DIR ) then
	mkdir $CONFIG_DIR
	mkdir $CONFIG_DIR/hosts
	mkdir $CONFIG_DIR/users
endif

#
# create site configuration file
#
if ( ! -f $site_conf ) then
	touch $site_conf
	if ( -f $site_conf ) then
		echo "#ODB Communicator Site Properties" 	>  $site_conf
		echo "#" `date`					>> $site_conf
		echo "MESSAGE_PORT=4444"			>> $site_conf
		echo "USER_LIST=${user}"			>> $site_conf
		echo "MESSAGE_HOST=${host}"			>> $site_conf
		echo "HOST_LIST=${host}"			>> $site_conf
		echo "LICENSE_HOST=localhost"			>> $site_conf
		echo "LICENSE_PORT=2012"			>> $site_conf
	endif
else
	#
	# Add missing parameters to the site config file
	#
	set res = `cat $site_conf | grep LICENSE_HOST | wc -l`
	if ($res < 1) then
		echo "" 					>> $site_conf
		echo "LICENSE_HOST=${host}" 		>> $site_conf
	endif
	set res = `cat $site_conf | grep LICENSE_PORT | wc -l`
	if ($res < 1) then
		echo ""					>> $site_conf
		echo "LICENSE_PORT=2012" 		>> $site_conf
	endif
endif

#
# Create host configuration file
#
if ( ! -f $host_conf ) then
	touch $host_conf
	if ( -f $host_conf ) then
		echo "#ODB Communicator HostNOTE: Properties for host: ${host}"	>  $host_conf
		echo "#" `date`									>> $host_conf
	endif
endif

#
# Create user configuration file
#
if ( ! -f $user_conf ) then
	touch $user_conf
	if ( -f $user_conf ) then
		echo "#ODB Communicator User Properties for user: ${user}" 	>  $user_conf
		echo "#" `date` 								>> $user_conf
	endif
endif

if ( $action == "auto_login" ) then
	if ( ! -e "${JAVA_BIN}/java${ext}" ) exit 0
	"${JAVA_BIN}/java${ext}" -mx128M -classpath "$class_path" -DTEMP_DIR=$temp_dir -DAUTO_LOGIN=1 -DSCRIPT_DIR=$scripts_dir -DCONFIG_DIR="$CONFIG_DIR" -DIMAGE_DIR=$scripts_dir/images com.frontlinePcb.messenger.NonCommunicatorPictureInterchange  $user $host $display	
	exit 0
endif

if ( $?MESSENGER_EXT_DEBUG ) then
	"${JAVA_BIN}/java${ext}" -mx128M -classpath "$class_path" -DTEMP_DIR=$temp_dir -DSCRIPT_DIR=$scripts_dir -DCONFIG_DIR="$CONFIG_DIR" -DDEBUG=1 -DIMAGE_DIR=$scripts_dir/images  com.frontlinePcb.messenger.NonCommunicatorPictureInterchange  $user $host $display
	echo "${JAVA_BIN}/java${ext}" -mx128M -classpath "$class_path" -DTEMP_DIR=$temp_dir -DSCRIPT_DIR=$scripts_dir -DCONFIG_DIR="$CONFIG_DIR" -DDEBUG=1 -DIMAGE_DIR=$scripts_dir/images  com.frontlinePcb.messenger.NonCommunicatorPictureInterchange  $user $host $display
	echo "Press Enter to close window"
	$<
else
	"${JAVA_BIN}/java${ext}" -mx128M -classpath "$class_path" -DTEMP_DIR=$temp_dir -DSCRIPT_DIR=$scripts_dir -DCONFIG_DIR="$CONFIG_DIR" -DIMAGE_DIR=$scripts_dir/images com.frontlinePcb.messenger.NonCommunicatorPictureInterchange  $user $host $display 
endif
