#!/bin/csh
# File Name:    secure
# File Purpose: This file is a c-shell script that is designed to secure a
#               Genesis job to an archive point. This archive point can be
#               any archive directory structure (such as a raid disk), or
#               the Star system.
# Written by:   Ian Ticehurst (Autom8 Ltd.)
# Version 1A.:  Date: 14.08.00 - Initial design and development.
# Version 1B.:  Date: 26.10.00 - Addition of $TMP defintion 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 -> secure. 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
# path of the job.
# 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 secure.
#  2. Secure a Genesis job to an archive directory (see also the acquire hook).
#  3. Secure a Genesis job to 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/secure

# Provide the system parameters with sensible variable names.
set SEC_JOB = `echo "$1"`
set SEC_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 secure" >> $GUI_DATA
echo "LABEL It is located in $GENESIS_DIR/sys/hooks" >> $GUI_DATA
echo "LABEL The hook is designed to secure data to an archive point" >> $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 secure 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 Secure Options Pop-up" >> $GUI_DATA
echo "LABEL Securing $SEC_JOB from path $SEC_DB" >> $GUI_DATA
echo "LABEL Select secure option required." >> $GUI_DATA
echo "RADIO SEC_OPT Options: V 1 992222" >> $GUI_DATA
echo "No secure required" >> $GUI_DATA
echo "Secure job to $ARC_POINT (creates a .tgz)" >> $GUI_DATA
echo "Secure job to Star system" >> $GUI_DATA
echo "END" >> $GUI_DATA

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

if ($SEC_OPT == 1) then
	# Put window on screen showing no secure 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 Secure Pop-up" >> $GUI_DATA
	echo "LABEL No secure will be done for job $SEC_JOB" >> $GUI_DATA
	echo "END" >> $GUI_DATA

	gui $GUI_DATA
	\rm $GUI_DATA

else if ($SEC_OPT == 2) then
	# Check to ensure that a .tgz file does not already exist for the selected job.
	set JOB_TST = `ls $ARC_POINT | grep -c "$SEC_JOB.tgz"`

	if ($JOB_TST == 0) then
		# Export from Genesis.
		COM export_job,job=$SEC_JOB,path=$ARC_POINT,mode=tar_gzip

		# Put window on screen showing secure 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 Secure Complete Pop-up" >> $GUI_DATA
		echo "LABEL Job $SEC_JOB has been secured to archive point" >> $GUI_DATA
		echo "END" >> $GUI_DATA

		gui $GUI_DATA
		\rm $GUI_DATA

		# Offer user chance to delete the Genesis job.
		echo "WIN 200 200" >> $GUI_DATA
		echo "FONT tbr14" >> $GUI_DATA
		echo "BG 222299" >> $GUI_DATA
		echo "FG 999999" >> $GUI_DATA
		echo "LABEL Genesis Job Deletion Pop-up" >> $GUI_DATA
		echo "LABEL Job $SEC_JOB has been secured to $ARC_POINT" >> $GUI_DATA
		echo "LABEL Do you want to delete the Genesis job?" >> $GUI_DATA
		echo "RADIO DEL_JOB Select: V 1 992222" >> $GUI_DATA
		echo "No, do not delete job" >> $GUI_DATA
		echo "Yes, delete $SEC_JOB from Genesis system" >> $GUI_DATA
		echo "END" >> $GUI_DATA

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

		if ($DEL_JOB == 2) then
			# Offer user final warning before job delete.
			echo "WIN 200 200" >> $GUI_DATA
			echo "FONT tbr14" >> $GUI_DATA
			echo "BG 992222" >> $GUI_DATA
			echo "FG 999999" >> $GUI_DATA
			echo "LABEL Delete Job Confirmation Pop-up" >> $GUI_DATA
			echo "LABEL WARNING - You have selected to delete $SEC_JOB from Genesis" >> $GUI_DATA
			echo "LABEL Are you sure?" >> $GUI_DATA
			echo "RADIO DEL_JOB2 Select: V 1 222299" >> $GUI_DATA
			echo "No, do not delete job" >> $GUI_DATA
			echo "Yes, delete $SEC_JOB from Genesis system" >> $GUI_DATA
			echo "END" >> $GUI_DATA

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

			if ($DEL_JOB2 == 2) then
				COM delete_entity,job=,type=job,name=$SEC_JOB
			endif
		endif
	else
		# Job file aleady exists, 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 Secure Error Pop-up" >> $GUI_DATA
		echo "LABEL Job $SEC_JOB already exists in $ARC_POINT" >> $GUI_DATA
		echo "END" >> $GUI_DATA

		gui $GUI_DATA
		\rm $GUI_DATA
	endif

else if ($SEC_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 secure" >> $GUI_DATA
	echo "Yes, continue with Star secure" >> $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

	# Create the tar file of the job, and move to the Star (secure) mount point.
	cd $SEC_DB
	tar cvf $STAR_POINT/$SEC_JOB.tar *

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

	# Set pointer to response file (Star will write this file and the one line content will indicate the secure status from Star).
	set RESP_FILE = $STAR_POINT/$SEC_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=$SEC_JOB" > $INS_FILE
	echo "fld=genesis" >> $INS_FILE
	echo "uat_group=genesis" >> $INS_FILE
	echo "gnstar:\.-\* $STAR_POINT/$SEC_JOB.tar" >> $INS_FILE

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

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

	# Response file found, read contents (in this example the one line will be "GOOD", "BAD", or "EXISTS").
	set STAR_RESP = `head -1 $RESP_FILE`
	if ($STAR_RESP == "GOOD") then
		# Star has secured the data without any problems. Set error line.
		set ERR_STR = `echo "Secure of job $SEC_JOB ended OK"`
		set ERR_COL = `echo "447744"`

		# Offer user chance to delete the Genesis job.
		echo "WIN 200 200" >> $GUI_DATA
		echo "FONT tbr14" >> $GUI_DATA
		echo "BG 222299" >> $GUI_DATA
		echo "FG 999999" >> $GUI_DATA
		echo "LABEL Genesis Job Deletion Pop-up" >> $GUI_DATA
		echo "LABEL Star reports that $SEC_JOB is secured OK" >> $GUI_DATA
		echo "LABEL Do you want to delete the Genesis job?" >> $GUI_DATA
		echo "RADIO DEL_JOB Select: V 1 992222" >> $GUI_DATA
		echo "No, do not delete job" >> $GUI_DATA
		echo "Yes, delete $SEC_JOB from Genesis system" >> $GUI_DATA
		echo "END" >> $GUI_DATA

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

		if ($DEL_JOB == 2) then
			# Offer user final warning before job delete.
			echo "WIN 200 200" >> $GUI_DATA
			echo "FONT tbr14" >> $GUI_DATA
			echo "BG 992222" >> $GUI_DATA
			echo "FG 999999" >> $GUI_DATA
			echo "LABEL Delete Job Confirmation Pop-up" >> $GUI_DATA
			echo "LABEL WARNING - You have selected to delete $SEC_JOB from Genesis" >> $GUI_DATA
			echo "LABEL Are you sure?" >> $GUI_DATA
			echo "RADIO DEL_JOB2 Select: V 1 222299" >> $GUI_DATA
			echo "No, do not delete job" >> $GUI_DATA
			echo "Yes, delete $SEC_JOB from Genesis system" >> $GUI_DATA
			echo "END" >> $GUI_DATA

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

			if ($DEL_JOB2 == 2) then
				COM delete_entity,job=,type=job,name=$SEC_JOB
			endif
		endif
	else if ($STAR_RESP == "EXISTS") then
		# Star already contains this data. Set error line.
		set ERR_STR = `echo "A revision of $SEC_JOB already exists. Secure aborted."`
		set ERR_COL = `echo "992222"`
	else if ($STAR_RESP == "BAD") then
		# Star secure ended with an error. Set error line.
		set ERR_STR = `echo "Secure of job $SEC_JOB ended with error on Star system. Secure aborted."`
		set ERR_COL = `echo "992222"`
	endif
	
	# Put final window on screen with results of Star secure. 
	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 Secure Results Pop-up" >> $GUI_DATA
	echo "LABEL Secure 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/$SEC_JOB.tar) then
		\rm -r $STAR_POINT/$SEC_JOB.tar
	endif
endif

abort:
	
exit


