#!/bin/csh 
# This script used to activate ODB++ Communicator messenger extention
# script parameters 1-user 2-host 3-display 4-action
#
if ( $#argv < 4 ) then
	PAUSE "Usage <user> <host> <display> <action>"
	exit 0
endif

# ARGS
set user	= $argv[1]
set host	= $argv[2]
set display	= $argv[3]
set action	= $argv[4]

#
# Setting of temp_dir
#
if ($?GENESIS_TMP) then
   set temp_dir = $GENESIS_TMP
else if ($?GENESIS_DIR) then
   set temp_dir = $GENESIS_DIR/tmp
else
   set temp_dir = /genesis/tmp
endif

#
# Setting of genesis edir 
#
if ($_genesis_edir !~ /* && $_genesis_edir !~ ?:*) then
        set _genesis_edir = "$_genesis_root/$_genesis_edir"
endif


# LOCALS
set odb_config_dir 		= \$GENESIS_DIR/share/odbcomm_config
set scripts_dir			= $_genesis_edir/all/odbcomm
set def_conf_file			= $scripts_dir/communicator.ini
set sys_conf_file			= $GENESIS_DIR/sys/communicator.ini
set host_conf_file		= $GENESIS_DIR/hosts/${host}/communicator.ini
set user_conf_file		= $HOME/.genesis/communicator.ini
set OS 				= `uname -s | tr '[:lower:]' '[:upper:]'`
set class_path 			= $scripts_dir/OdbViewer.jar
set port_file 			= ${temp_dir}/port_${user}_${host}_${display}

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

set ext = ""
if ( $OS =~ *WIN* ) set ext = ".exe"

#
# Skip all the messages (perform quiet connection)
#

if (! -d $scripts_dir && $action != "auto_login") then
	PAUSE "ODB++ Comm Error : Directory : $scripts_dir does not exist , Aborting " 
	exit 1
endif

#
# Setting of configurations for the communicator (JAVA_BIN , CONFIG_DIR)
#
if ( -f $user_conf_file) then
	set conf_file = $user_conf_file
else if ( -f $host_conf_file) then
	set conf_file = $host_conf_file
else if ( -f $sys_conf_file ) then
	cp $sys_conf_file $host_conf_file
	set conf_file = $host_conf_file
else if ( -f $def_conf_file ) then
	cp $def_conf_file $host_conf_file
	set conf_file = $host_conf_file
else
	set conf_file = $host_conf_file
	if ($action != "auto_login") then
		touch $conf_file
		if ($status != 0) then
			PAUSE "Couldn\'t write file : $conf_file , Please check permissions"
			exit 0
		endif
	endif
	echo 'CONFIG_DIR=\$GENESIS_DIR/share/odbcomm_conf' > $conf_file
	if ( $OS =~ *WIN* ) then
		echo "JAVA_BIN=C:/Program Files/Frontline-PCB/Communicator/JRE/1.3/bin" >> $conf_file
	else
		echo "JAVA_BIN=/opt/odbcomm/j2re1_3_0/bin" >> $conf_file
	endif
endif

#
# skip to auto login after setting the $conf_file
#
if ( $action == "auto_login" ) goto AUTO_LOGIN

get_param JAVA_BIN $conf_file
if (! -f "$JAVA_BIN/java${ext}") then
	set set_conf_file = "yes"
endif


if ( $?set_conf_file ) then
	set argv = ( $conf_file $_genesis_edir $temp_dir $odb_config_dir $OS)
	source $scripts_dir/set_conf_file 
	if ($?_exit) exit 0
	if ($?orig_conf_file) then
		cp $conf_file $orig_conf_file
		set conf_file = $orig_conf_file
	endif
endif

get_param JAVA_BIN $conf_file

#
# Starting the procedure according to the action
#
if ($action == "show") then
	set load = no
	if ( -f $port_file ) then
		set PORT_ID = `cat $port_file`
		"${JAVA_BIN}/java${ext}" -mx128M -classpath "$class_path" com.frontlinePcb.messenger.MessengerWaker $PORT_ID show ; set stat = $status
		if ( $stat == 255 || $stat == 1 ) then
			set load = "yes"
			rm -f $port_file
		endif
	else
		set load = "yes"
	endif
	if ( $load == "yes" ) then
		set args = ("user=$user" "host=$host" "display=$display" "temp_dir=$temp_dir" "scripts_dir=$scripts_dir" "class_path=$class_path" "genesis_dir=$GENESIS_DIR" "action=$action" "ext=$ext" "conf_file=$conf_file")
		if ( $OS =~ *WIN* ) then
			(start csh -f $scripts_dir/load_messenger $args) & 
		else
			if ( $?MESSENGER_EXT_DEBUG ) then
				nohup xterm -e csh -cf "$scripts_dir/load_messenger $args" & 
			else
				(csh -cf "$scripts_dir/load_messenger $args") &
			endif
		endif
	endif
else if ( $action == "new_message" ) then
	if ( -f $port_file ) then
		set PORT_ID = (`cat $port_file`)
		
		# Check if messenger is alive
		"${JAVA_BIN}/java${ext}" -mx128M -classpath "$class_path" -mx128 com.frontlinePcb.messenger.MessengerWaker $PORT_ID show ; set stat = $status
		if ( $stat == 255 || $stat == 1 ) then
			PAUSE "ODB++ Comm Error : Messenger not loaded, please load messenger first"
			rm -f $port_file
			exit 0
		endif
		set scr_resp = ${temp_dir}/grab.$$		

		echo "set grab_resp = $temp_dir/resp_${user}_${host}_${display}.$$" > $scr_resp
		echo "set jpeg_file = $temp_dir/jpeg_${user}_${host}_${display}.$$" >> $scr_resp
		set argv = "$scr_resp"
		source ${scripts_dir}/grab 
		
		get_param job $grab_resp
		echo "key_word=${job}" >> $grab_resp
		"$JAVA_BIN/java${ext}" -mx128M -classpath "$class_path" -mx128 com.frontlinePcb.messenger.MessengerWaker $PORT_ID $grab_resp  >! /dev/null
	else
		PAUSE "ODB++ Comm Error : Messenger not loaded, please load Messenger first"
	endif
else if ( $action == "auto_login" ) then
AUTO_LOGIN:
	set args = ("user=$user" "host=$host" "display=$display" "temp_dir=$temp_dir" "scripts_dir=$scripts_dir" "class_path=$class_path" "genesis_dir=$GENESIS_DIR" "action=$action" "ext=$ext" "conf_file=$conf_file")	
	if ( $OS =~ *WIN* ) then
		(start csh -f $scripts_dir/load_messenger $args) &
	else
		if ( $?MESSENGER_EXT_DEBUG ) then
			nohup xterm -e csh -cf "$scripts_dir/load_messenger $args" &  
		else
			(csh -cf "$scripts_dir/load_messenger $args") &
		endif
	endif	
endif

exit 0


	
