#!/bin/csh
# File Name:    acquire
# File Purpose: This file is a c-shell script that is designed to acquire data
#               from an archive point and import it into a Genesis database.
# Written by:   Ian Ticehurst (Autom8 Ltd.)
# Version 1A.:  Date: 11.08.00 - Initial design and development.
# Version 1B.:  Date: 27.10.00 - Addition of $TMP definition and useage.
# Version 1C.:  Date: 05.12.00 - Removal of "gui" alias.
############################# Start of notes ###############################
# This file is called from one place within the system. From the engineering
# screen use the function file -> archive -> acquire. When the script is run
# it get two parameters passed to it by the system. The first ($1) is the job
# name that the user requested (in the system pop-up), the second ($2) is the
# name of the database to import to.
# As the hook is a c-shell script, it can perform any action dictated by the
# code below. The default code provided offers three possible actions:-
#  1. No acquire.
#  2. Acquire a previously stored ODB job (see also the secure hook).
#  3. Acquire a job from Star (note that this section is an example only).
#
############################# End of notes ###############################
# Define correct temp. dir. depending on env. vars.
if ($?GENESIS_TMP) then
	set TMP = $GENESIS_TMP
else if ($?GENESIS_DIR) then
	set TMP = $GENESIS_DIR/tmp
else
	set TMP = /genesis/tmp
endif

# Set the INFO alias and the tmp INFO file.
set INFO = $TMP/info
alias DO_INFO 'COM info,out_file=$INFO,write_mode=replace,args=\!:*;source $INFO; rm $INFO'

# Set up gui data and response files
set GUI_DATA = $TMP/gui_data.$$
set GUI_RESP = $TMP/gui_resp.$$

# Set default archive point.
set ARC_POINT = $TMP
# Set up default mount point for Star (if connecting to an existing Star system, this will need changing).
set STAR_POINT = $TMP/star/acquire

# Provide the system parameters with sensible variable names.
set ACQ_JOB = `echo "$1"`
set ACQ_DB = `echo "$2"`

# Put window on screen indicating this is a default hook.
echo "WIN 200 200" >> $GUI_DATA
echo "FONT tbr14" >> $GUI_DATA
echo "BG 222299" >> $GUI_DATA
echo "FG 999999" >> $GUI_DATA
echo "LABEL Frontline Default Hook Pop-up" >> $GUI_DATA
echo "LABEL This is the default hook acquire" >> $GUI_DATA
echo "LABEL It is located in $GENESIS_DIR/sys/hooks" >> $GUI_DATA
echo "LABEL The hook is designed to acquire a previously archived job" >> $GUI_DATA
echo "LABEL Comments and possible applications can be found in the header of this hook" >> $GUI_DATA
echo "LABEL Also see the hooks readme file and on-line manual 0203 (chapter 9) for more details" >> $GUI_DATA
echo "END" >> $GUI_DATA

gui $GUI_DATA
\rm $GUI_DATA

# Put window on screen offering different acquire options.
echo "WIN 200 200" >> $GUI_DATA
echo "FONT tbr14" >> $GUI_DATA
echo "BG 222299" >> $GUI_DATA
echo "FG 999999" >> $GUI_DATA
echo "LABEL Default Acquire Options Pop-up" >> $GUI_DATA
echo "LABEL Acquiring $ACQ_JOB to database $ACQ_DB" >> $GUI_DATA
echo "LABEL Select acquire option required." >> $GUI_DATA
echo "RADIO ACQ_OPT Options: V 1 992222" >> $GUI_DATA
echo "No acquire required" >> $GUI_DATA
echo "Acquire job from $ARC_POINT (only works on .tgz files)" >> $GUI_DATA
echo "Acquire job from Star system" >> $GUI_DATA
echo "END" >> $GUI_DATA

gui $GUI_DATA > $GUI_RESP
source $GUI_RESP
\rm $GUI_DATA $GUI_RESP

if ($ACQ_OPT == 1) then
	# Put window on screen showing no acquire will be done.
	echo "WIN 200 200" >> $GUI_DATA
	echo "FONT tbr14" >> $GUI_DATA
	echo "BG 222299" >> $GUI_DATA
	echo "FG 999999" >> $GUI_DATA
	echo "LABEL No Acquire Pop-up" >> $GUI_DATA
	echo "LABEL No acquire will be done for job $ACQ_JOB" >> $GUI_DATA
	echo "END" >> $GUI_DATA

	gui $GUI_DATA
	\rm $GUI_DATA

else if ($ACQ_OPT == 2) then
	# Check to ensure that a .tgz file exists for the selected job.
	set JOB_TST = `ls $ARC_POINT | grep -c "$ACQ_JOB.tgz"`

	if ($JOB_TST != 0) then
		# Job exists, import into Genesis.
		COM import_job,db=$ACQ_DB,path=$ARC_POINT/$ACQ_JOB.tgz,name=$ACQ_JOB

		# Put window on screen showing acquire is complete.
		echo "WIN 200 200" >> $GUI_DATA
		echo "FONT tbr14" >> $GUI_DATA
		echo "BG 447744" >> $GUI_DATA
		echo "FG 999999" >> $GUI_DATA
		echo "LABEL Acquire Complete Pop-up" >> $GUI_DATA
		echo "LABEL Job $ACQ_JOB has been acquired into the system" >> $GUI_DATA
		echo "END" >> $GUI_DATA

		gui $GUI_DATA
		\rm $GUI_DATA
	else
		# Job does not exist, warn user.
		echo "WIN 200 200" >> $GUI_DATA
		echo "FONT tbr14" >> $GUI_DATA
		echo "BG 992222" >> $GUI_DATA
		echo "FG 999999" >> $GUI_DATA
		echo "LABEL Acquire Error Pop-up" >> $GUI_DATA
		echo "LABEL Job $ACQ_JOB does not exist in $ARC_POINT" >> $GUI_DATA
		echo "END" >> $GUI_DATA

		gui $GUI_DATA
		\rm $GUI_DATA
	endif

else if ($ACQ_OPT == 3) then
	# Warn user that if Star is not set up correctly this option will loop forever.
	echo "WIN 200 200" >> $GUI_DATA
	echo "FONT tbr14" >> $GUI_DATA
	echo "BG 992222" >> $GUI_DATA
	echo "FG 999999" >> $GUI_DATA
	echo "LABEL Star Configuration Warning Pop-up" >> $GUI_DATA
	echo "LABEL Warning - This section is an example only of a connection to Star" >> $GUI_DATA
	echo "LABEL If no response from Star is detected this script will loop forever" >> $GUI_DATA
	echo "LABEL Do you want to continue?" >> $GUI_DATA
	echo "RADIO STAR_SEL Select: V 1 222299" >> $GUI_DATA
	echo "No, abort this acquire" >> $GUI_DATA
	echo "Yes, continue with Star acquire" >> $GUI_DATA
	echo "END" >> $GUI_DATA

	gui $GUI_DATA > $GUI_RESP
	source $GUI_RESP
	\rm $GUI_DATA $GUI_RESP

	if ($STAR_SEL != 2) then
		goto abort
	endif

	# Check to see if job already exists on the Genesis system.
	DO_INFO -t root
	foreach GEN_JOB ($gJOBS_LIST)
		if ($GEN_JOB == $ACQ_JOB) then
			set ERR_STR = `echo "Job $ACQ_JOB already exists in the Genesis database"`
			set ERR_COL = `echo "992222"`
			goto gui_out
		endif
	end

	# Set pointers to instruction file and trigger file (Star will detect these files and act on them).
	set INS_FILE = $STAR_POINT/$ACQ_JOB.INS
	set TRG_FILE = $STAR_POINT/$ACQ_JOB.TRG

	# Set pointer to response file (Star will write this file and the one line content will indicate the acquire status from Star).
	set RESP_FILE = $STAR_POINT/$ACQ_JOB.RSP

	# Create the instruction file (Star uses this file to get job (draw) name and data type (folder) - Note this may be changed for different Star installations).
	echo "drw=$ACQ_JOB" > $INS_FILE
	echo "fld=genesis" >> $INS_FILE

	# Create the trigger file (Star will look for this file, and start the acquire process when it finds it).
	touch $TRG_FILE

	# Wait for Star to finish acquire process (keep looking for response file - if not found continue loop).
	while (! -e $RESP_FILE)
		echo "Waiting for Star to finish acquire for job $ACQ_JOB"
		sleep 5
	end

	# Response file found, read contents (in this example the one line will be "GOOD", "BAD", or "NOT_AVAIL").
	set STAR_RESP = `head -1 $RESP_FILE`
	if ($STAR_RESP == "BAD") then
		# Star failed to restore the data. Set error line and skip to end of program.
		set ERR_STR = `echo "The Aqcuire for job $ACQ_JOB failed"`
		set ERR_COL = `echo "992222"`
		goto gui_out	
	else if ($STAR_RESP == "NOT_AVAIL") then
		# Star does not contain this data. Set error line and skip to end of program.
		set ERR_STR = `echo "There is no genesis data in Star for job $ACQ_JOB"`
		set ERR_COL = `echo "992222"`
		goto gui_out	
	endif
	
	# Make a temp. job directory (Star will have put the job.tar file in the $STAR_POINT/acquire dir.).
	mkdir $STAR_POINT/$ACQ_JOB

	# Move the job.tar file to the temp. dir and extract data.
	cd $STAR_POINT/$ACQ_JOB
	mv $STAR_POINT/$ACQ_JOB.tar .
	tar xf $ACQ_JOB.tar
	rm $ACQ_JOB.tar
	if (-e data.tar) then
		rm data.tar
	endif

	# Import the job into the Genesis db
	COM import_job,db=$ACQ_DB,path=$STAR_POINT/$ACQ_JOB,name=$ACQ_JOB
	set ERR_STR = `echo "Acquire for job $ACQ_JOB ended succesfully"`
	set ERR_COL = `echo "447744"`

	gui_out:
	# Put final window on screen with results of Star acquire. 
	echo "WIN 200 200" >> $GUI_DATA
	echo "FONT tbr14" >> $GUI_DATA
	echo "BG $ERR_COL" >> $GUI_DATA
	echo "FG 999999" >> $GUI_DATA
	echo "LABEL Star Acquire Results Pop-up" >> $GUI_DATA
	echo "LABEL Acquire finished with the following message" >> $GUI_DATA
	echo "LABEL $ERR_STR" >> $GUI_DATA
	echo "END" >> $GUI_DATA

	gui $GUI_DATA
	\rm $GUI_DATA

	# Clean up file system.
	if (-e $RESP_FILE) then
		\rm $RESP_FILE
	endif
	if (-e $STAR_POINT/$ACQ_JOB) then
		\rm -r $STAR_POINT/$ACQ_JOB
	endif

endif

abort:
	
exit
