# File Name:    out_file
# File Purpose: This file is called during the ARM process. It is a c-shell script
#               which is used to manipulate various parts of the output
#               file dependant on the tool parameters and the machine file being
#               used etc.
# Written by:   Ian Ticehurst (Autom8 Ltd.)
# Version 1A.:  Date: 06.11.00 - Initial design and development.
# Version 1B.:  Date: 09.11.00 - Addition of specific routines to match machine file names.
# Version 1C.:  Date: 30.11.00 - Addition of klingonburg and lenz routines to match machine file names.
# Version 1D.:  Date: 05.12.00 - Removal of "gui" alias and "gui" data and response settings (not used).
# Version 1E.:  Date: 16.01.01 - Add check for NT shell version. Also modify gawk calls dependant on version.
# Version 1E.:  Date: 20.02.01 - Ensure that all "gawk" calls are modified to "awk" and that "awk95" is aliased.
############################# Start of notes ###############################
# This file is located in the hooks/ncr directory and forms part of the Auto Rout
# Manager file set. It is a c-shell script which can be configured to provide user
# and site specific modifications to the rout output files dependant on the different
# parameters allocated to each tool (type, size etc.) and also the machine file being
# used. 
# This script is started with one parameter passed to it. This parameters is a file
# which can be sourced ($1). This source file contains details of the tool table and
# the ARM parameters and this is the information that can be used to define any actions
# required. The file contains the following formatted information:-
#
#set ncREGANGLE    = (0 )
#set ncREGMIRROR   = (0 )
#set ncREGXOFF     = (2 )
#set ncREGYOFF     = (1 )
#set ncREGXORIGIN  = (2 )
#set ncREGYORIGIN  = (1 )
#set ncTRNSXORIGIN = (X2 )
#set ncTRNSYORIGIN = (y1 )
#set ncLAYER       = (rout )
#set ncSET         = (nc1 )
#set ncMACHINE     = (schmoll )
#set ncFORMAT      = (Excellon1 )
#set ncZEROES      = (trailing )
#set ncUNITS       = (mm )
#set ncTOOL_UNITS  = (mm )
#set ncNF          = (2.4 )
#set ncHEADER_PATH  = ('D:/genesis/tmp/genesis18765.263' )
#set ncEOF_PATH     = ('D:/genesis/tmp/genesis18766.263' )
#set ncCCW          = ('0' )
#set ncTYPE         = ('hole'                          'hole'                          'chain'                      )
#set ncSTEP         = ('pcb'                           'pcb'                           'pcb'                        )
#set ncCHAIN        = ('0'                             '0'                             '1'                          )
#set ncCOMP         = ('none'                          'none'                          'right'                      )
#set ncPATH         = ('0'                             '0'                             '13.126'                     )
#set ncCOUNT        = ('12'                            '2'                             '0'                          )
#set ncFLAG         = ('0'                             '0'                             '0'                          )
#set ncCW           = ('0'                             '0'                             '0'                          )
#set ncDUP          = ('0'                             '0'                             '0'                          )
#set ncTOOL_NUM     = ('1'                             '2'                             '3'                          )
#set ncCOMP_NUM     = ('1'                             '1'                             '1'                          )
#set ncCOMP_FACT    = ('0'                             '0'                             '94.488'                     )
#set ncCOMP_FACT2   = ('.000'                          '.000'                          '.094'                       )
#set ncTOOL_SIZE    = ('40'                            '200'                           '94.488'                     )
#set ncTOOL_SIZE2   = ('.040'                          '.200'                          '.094'                       )
#set ncTOOL_SPEED   = ('0'                             '0'                             '0'                          )
#set ncTOOL_FEED    = ('0'                             '0'                             '0'                          )
#set ncTOOL_CHANGE  = ('/genesis/tmp/genesis18767.63' '/genesis/tmp/genesis18768.63' '/genesis/tmp/genesis18769.63' )
#set ncSTART_STEP   = ('/genesis/tmp/genesis1876a.63' '/genesis/tmp/genesis1876b.63' '/genesis/tmp/genesis1876c.63' )
#set ncEND_STEP     = ('/genesis/tmp/genesis1876d.63' '/genesis/tmp/genesis1876e.63' '/genesis/tmp/genesis1876f.63' )
#set ncSR_NAME      = ('pcb'       'pcb'       'pcb'       'pcb'       'pcb'       'pcb'       'pcb'       'panel'  )
#set ncSERIAL       = ('1'         '2'         '3'         '4'         '5'         '6'         '7'         '8'      )
#set ncSR_DX        = ('2.444'     '2.444'     '2.444'     '2.444'     '5.885'     '5.885'     '5.885'     '0'      )
#set ncSR_DY        = ('2.650'     '6.232'     '9.814'     '13.396'    '2.650'     '6.232'     '13.396'    '0'      )
#set ncSR_ANGLE     = ('0'         '0'         '0'         '0'         '0'         '0'         '0'         '0'      )
#set ncSR_MIRROR    = ('0'         '0'         '0'         '0'         '0'         '0'         '0'         '0'      )
#
# This file can control the following parts of the output files:-
# 1. The header.                   --- ncHEADER_PATH 
# 2. The end of file.              --- ncEOF_PATH 
# 3. The tool changes.             --- ncTOOL_CHANGE
# 4. The step and repeat start.    --- ncSTART_STEP
# 5. The step and repeat finish.   --- ncEND_STEP
#
# It should be noted that there is no single output file (as with the set_table hook)
# and the output files are defined by the variables list in notes 1-5 above. These
# variables are only set if the machine file is set to use this out_file program for
# that specific section. Thus, by testing each variable it is possible to know if
# the custom output should be used. It will also be noted that some variables apply
# to the complete output file (ncHEADER_PATH for example) and some apply to each of
# the tool entries being passed.
# If the machine file has any of these sections set to yes, then the program must
# provide that section of the output file. Also, if the machine file has any of these
# sections set to no, the program for that section is ignored.
############################# End of notes ################################ 

# Section to set up general parameters and variables.
# ---------------------------------------------------
# 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'

# Detect current operating system.
set CURR_OS = `uname -s`

# If current operating system is NT, set alias to call awk95 as "awk".
if ($CURR_OS == "WINDOWS_NT") then
	alias awk '$GENESIS_DIR/e${GENESIS_VER}/nt/bin/awk95'
endif

# Detect current tcsh version (old = 0, new = 1).
if ($CURR_OS == "WINDOWS_NT") then
	set NT_VER = `echo "$version" | awk -F'-' '{if ($7 < 5.0) print 0; else print 1}'`
endif

# Set variable names for parameter file to source and out_file to write to.
set source_script = $1

# Force units to inch.
COM ncrset_units,units=inch

# Source standard parameter file.
source $source_script

# Header definition section.
# --------------------------
if ($ncHEADER_PATH != "") then
	if ($ncMACHINE == "default_excellon") then
		echo "M48" > $ncHEADER_PATH
		if ($ncTOOL_UNITS == "mm") then
			echo "METRIC" >> $ncHEADER_PATH
		else
			echo "INCH" >> $ncHEADER_PATH
		endif

		set i = 1
		foreach TOOL ($ncTOOL_NUM)
			if ($i < 10) then
				echo "T0${i}C${ncTOOL_SIZE2[$i]}" >> $ncHEADER_PATH
			else
				echo "T${i}C${ncTOOL_SIZE2[$i]}" >> $ncHEADER_PATH
			endif
			@ i ++
		end

		set i = 1
		foreach TOOL ($ncTOOL_NUM)
			if ($ncTYPE[$i] == "chain") then
				echo "CP,${ncCOMP_NUM[$i]},${ncCOMP_FACT2[$i]}" >> $ncHEADER_PATH
			endif
			@ i ++
		end

		echo "VER,1" >> $ncHEADER_PATH
		echo "FMAT,2" >> $ncHEADER_PATH
		echo "%" >> $ncHEADER_PATH
		if ($ncUNITS == "inch") then
			echo "M72" >> $ncHEADER_PATH
		else
			echo "M71" >> $ncHEADER_PATH
		endif
		echo "G90" >> $ncHEADER_PATH

	else if ($ncMACHINE == "default_sm3000") then
		# Note that in seibmeyer format the tool information is defined in the EOF section.
		echo "%%3000" >  $ncHEADER_PATH

	else if ($ncMACHINE == "sel_r100") then
		echo "M48" > $ncHEADER_PATH
		if ($ncTOOL_UNITS == "mm") then
			set TL_UNITS = `echo "METRIC"`
		else
			set TL_UNITS = `echo "INCH"`
		endif

		if ($ncZEROES == "trailing") then
			set ZEROES = `echo "LZ"`
		else if ($ncZEROES == "leading") then
			set ZEROES = `echo "TZ"`
		else
			set ZEROES = `echo ""`
		endif

		if ($CURR_OS == "WINDOWS_NT") then
			set MSN = `echo "$ncNF" | awk -F'.' '{print $1}'`
			set LSN = `echo "$ncNF" | awk -F'.' '{print $2}'`
		else
			set MSN = `echo "$ncNF" | awk -F'.' '{print $1}'`
			set LSN = `echo "$ncNF" | awk -F'.' '{print $2}'`
		endif

		set i = 1
		set LZ_CT = ""
		while ($i <= $MSN)
			set LZ_CT = `echo "${LZ_CT}0"`
			@ i ++
		end
		set i = 1
		set TZ_CT = ""
		while ($i <= $LSN)
			set TZ_CT = `echo "${TZ_CT}0"`
			@ i ++
		end

		echo "${TL_UNITS},${ZEROES},${LZ_CT}.${TZ_CT}" >> $ncHEADER_PATH
		echo "FMAT,2" >> $ncHEADER_PATH
		echo "VER,1" >> $ncHEADER_PATH
		echo "CCW,OFF" >> $ncHEADER_PATH

		set i = 1
		foreach TOOL ($ncTOOL_NUM)
			if ($i < 10) then
				echo "T0${i}C${ncTOOL_SIZE2[$i]}" >> $ncHEADER_PATH
			else
				echo "T${i}C${ncTOOL_SIZE2[$i]}" >> $ncHEADER_PATH
			endif
			@ i ++
		end

		set i = 1
		foreach TOOL ($ncTOOL_NUM)
			if ($ncTYPE[$i] == "chain") then
				if ($i < 10) then
					echo "CP,0${i},${ncCOMP_FACT2[$i]}" >> $ncHEADER_PATH
				else
					echo "CP,${i},${ncCOMP_FACT2[$i]}" >> $ncHEADER_PATH
				endif
			endif
			@ i ++
		end

		echo "%" >> $ncHEADER_PATH
		echo "G93XY" >> $ncHEADER_PATH

	else if ($ncMACHINE == "klingonburg_microtronic1" || $ncMACHINE == "klingonburg_microtronic7") then
		# Note that in seibmeyer format the tool information is defined in the EOF section.
		echo "%%3000" >  $ncHEADER_PATH

	else if ($ncMACHINE == "schmoll") then
		# Note that in seibmeyer format the tool information is defined in the EOF section.
		echo "%%3000" >  $ncHEADER_PATH

	else if ($ncMACHINE == "lenz_35") then
		# Note that in seibmeyer format the tool information is defined in the EOF section.
		echo "%%3000" >  $ncHEADER_PATH

	else
		echo "HEADER SECTION IN out_file NOT DEFINED" > $ncHEADER_PATH
		echo "Current machine file does not have matching header details" >> $ncHEADER_PATH
	endif
endif

# End of file (EOF) definition section.
# -------------------------------------
if ($ncEOF_PATH != "") then
	if ($ncMACHINE == "default_excellon") then
		echo "M30" > $ncEOF_PATH

	else if ($ncMACHINE == "default_sm3000") then
		set CHAR = '$'
		echo "$CHAR" > $ncEOF_PATH

		set i = 1
		while ($i <= $#ncTOOL_NUM)
			if ($CURR_OS == "WINDOWS_NT") then
				if ($NT_VER == 0) then
					set act_toolsize = `echo $ncTOOL_SIZE[$i] | awk '{printf (\"%4.0f\",($1 / 10.0))}'`
				else
					set act_toolsize = `echo $ncTOOL_SIZE[$i] | awk '{printf ("%4.0f",($1 / 10.0))}'`
				endif
			else
				set act_toolsize = `echo $ncTOOL_SIZE[$i] | awk '{printf ("%4.0f",($1 / 10.0))}'`
			endif
			echo "T${i}D${act_toolsize}S${ncTOOL_SPEED[$i]}F${ncTOOL_FEED[$i]}R100" >> $ncEOF_PATH
			@ i ++
		end
		echo "$CHAR" >> $ncEOF_PATH

	else if ($ncMACHINE == "sel_r100") then
		# Note that this machine file does not request special end of file definition.

	else if ($ncMACHINE == "klingonburg_microtronic1" || $ncMACHINE == "klingonburg_microtronic7") then
		set CHAR = '$'
		echo "$CHAR" > $ncEOF_PATH

		set i = 1
		while ($i <= $#ncTOOL_NUM)
			if ($CURR_OS == "WINDOWS_NT") then
				if ($NT_VER == 0) then
					set act_toolsize = `echo $ncTOOL_SIZE[$i] | awk '{printf (\"%4.0f\",($1 / 10.0))}'`
				else
					set act_toolsize = `echo $ncTOOL_SIZE[$i] | awk '{printf ("%4.0f",($1 / 10.0))}'`
				endif
			else
				set act_toolsize = `echo $ncTOOL_SIZE[$i] | awk '{printf ("%4.0f",($1 / 10.0))}'`
			endif
			set SPIN = `echo "$ncTOOL_SPEED[$i] / 100" | bc`
			echo "T${i}D${act_toolsize}S${SPIN}F${ncTOOL_FEED[$i]}R100" >> $ncEOF_PATH
			@ i ++
		end
		echo "$CHAR" >> $ncEOF_PATH

	else if ($ncMACHINE == "schmoll") then
		set CHAR = '$'
		echo "$CHAR" > $ncEOF_PATH

		set i = 1
		while ($i <= $#ncTOOL_NUM)
			if ($CURR_OS == "WINDOWS_NT") then
				if ($NT_VER == 0) then
					set act_toolsize = `echo $ncTOOL_SIZE[$i] | awk '{printf (\"%4.0f\",($1 / 10.0))}'`
				else
					set act_toolsize = `echo $ncTOOL_SIZE[$i] | awk '{printf ("%4.0f",($1 / 10.0))}'`
				endif
			else
				set act_toolsize = `echo $ncTOOL_SIZE[$i] | awk '{printf ("%4.0f",($1 / 10.0))}'`
			endif
			set SPIN = `echo "$ncTOOL_SPEED[$i] / 100" | bc`
			echo "T${i}D${act_toolsize}S${SPIN}F${ncTOOL_FEED[$i]}R100" >> $ncEOF_PATH
			@ i ++
		end
		echo "$CHAR" >> $ncEOF_PATH

	else if ($ncMACHINE == "lenz_35") then
		set CHAR = '$'
		echo "$CHAR" > $ncEOF_PATH

		set i = 1
		while ($i <= $#ncTOOL_NUM)
			if ($CURR_OS == "WINDOWS_NT") then
				if ($NT_VER == 0) then
					set act_toolsize = `echo $ncTOOL_SIZE[$i] | awk '{printf (\"%4.0f\",($1 / 10.0))}'`
				else
					set act_toolsize = `echo $ncTOOL_SIZE[$i] | awk '{printf ("%4.0f",($1 / 10.0))}'`
				endif
			else
				set act_toolsize = `echo $ncTOOL_SIZE[$i] | awk '{printf ("%4.0f",($1 / 10.0))}'`
			endif
			set SPIN = `echo "$ncTOOL_SPEED[$i] / 100" | bc`
			echo "T${i}D${act_toolsize}S${SPIN}F${ncTOOL_FEED[$i]}R100" >> $ncEOF_PATH
			@ i ++
		end
		echo "$CHAR" >> $ncEOF_PATH

	else
		echo "EOF SECTION IN out_file NOT DEFINED" > $ncEOF_PATH
		echo "Current machine file does not have matching end of file details" >> $ncEOF_PATH
	endif
endif

# Tool change definition section.
# -------------------------------
set i = 1
while ($i <= $#ncTOOL_NUM) 
	if ($ncTOOL_CHANGE[$i] != "") then
		if ($ncMACHINE == "default_excellon") then
			if ($i < 10) then
				set TL_STR = `echo "T0"`
			else
				set TL_STR = `echo "T"`
			endif
			if ($ncTYPE[$i] == "chain") then
				if ($ncCOMP_NUM[$i] < 10) then
					echo "${TL_STR}${i}0${ncCOMP_NUM[$i]}" > $ncTOOL_CHANGE[$i]
				else
					echo "${TL_STR}${i}${ncCOMP_NUM[$i]}" > $ncTOOL_CHANGE[$i]
				endif	
			else
				echo "${TL_STR}${i}" > $ncTOOL_CHANGE[$i]
			endif

		else if ($ncMACHINE == "default_sm3000") then
			echo "T${i}" > $ncTOOL_CHANGE[$i]

		else if ($ncMACHINE == "sel_r100") then
			if ($ncTYPE[$i] == "chain") then
				if ($i < 10) then
					echo "T0${i}0${i}" > $ncTOOL_CHANGE[$i]
				else
					echo "T${i}${i}" > $ncTOOL_CHANGE[$i]
				endif
			else
				if ($i < 10) then
					echo "T0${i}" > $ncTOOL_CHANGE[$i]
				else
					echo "T${i}" > $ncTOOL_CHANGE[$i]
				endif
			endif	

		else if ($ncMACHINE == "klingonburg_microtronic1" || $ncMACHINE == "klingonburg_microtronic7") then
			echo "T${i}" > $ncTOOL_CHANGE[$i]

		else if ($ncMACHINE == "schmoll") then
			echo "T${i}" > $ncTOOL_CHANGE[$i]

		else if ($ncMACHINE == "lenz_35") then
			echo "T${i}" > $ncTOOL_CHANGE[$i]

		else
			echo "TOOL CHANGE SECTION IN out_file NOT DEFINED" > $ncTOOL_CHANGE[$i]
			echo "Current machine file does not have matching tool change details" >> $ncTOOL_CHANGE[$i]
		endif
	endif
   	@ i ++
end

# Step and repeat start definition section.
# ----------------------------------------------
set i = 1
while ($i <= $#ncSTART_STEP) 
	if ($ncSTART_STEP[$i] != "") then
		if ($ncMACHINE == "default_excellon") then
			echo "M25" > $ncSTART_STEP[$i]

		else if ($ncMACHINE == "default_sm3000") then
			# Note that this machine file does not request special step and repeat start definition.

		else if ($ncMACHINE == "sel_r100") then
			# Note that this machine file does not request special step and repeat start definition.

		else if ($ncMACHINE == "klingonburg_microtronic1" || $ncMACHINE == "klingonburg_microtronic7") then
			# Note that this machine file does not request special step and repeat start definition.

		else if ($ncMACHINE == "schmoll") then
			# Note that this machine file does not request special step and repeat start definition.

		else if ($ncMACHINE == "lenz_35") then
			# Note that this machine file does not request special step and repeat start definition.

		else
			echo "S+R START SECTION IN out_file NOT DEFINED" > $ncSTART_STEP[$i]
			echo "Current machine file does not have matching s+r start details" >> $ncSTART_STEP[$i]
		endif
	endif
   	@ i ++
end

# Step and repeat end definition section.
# --------------------------------------------
set i = 1
while ($i <= $#ncEND_STEP) 
	if ($ncEND_STEP[$i] != "") then
		if ($ncMACHINE == "default_excellon") then
			echo "M02" > $ncEND_STEP[$i]
			echo "M08" >> $ncEND_STEP[$i]

		else if ($ncMACHINE == "default_sm3000") then
			# Note that this machine file does not request special step and repeat end definition.

		else if ($ncMACHINE == "sel_r100") then
			# Note that this machine file does not request special step and repeat end definition.

		else if ($ncMACHINE == "klingonburg_microtronic1" || $ncMACHINE == "klingonburg_microtronic7") then
			# Note that this machine file does not request special step and repeat end definition.

		else if ($ncMACHINE == "schmoll") then
			# Note that this machine file does not request special step and repeat end definition.

		else if ($ncMACHINE == "lenz_35") then
			# Note that this machine file does not request special step and repeat end definition.

		else
			echo "S+R END SECTION IN out_file NOT DEFINED" > $ncEND_STEP[$i]
			echo "Current machine file does not have matching s+r end details" >> $ncEND_STEP[$i]
		endif
	endif
   	@ i ++
end

exit
