#!/bin/csh
# This is a program to use in conjunction with the example tooling forms.
# This program is designed to check to see if a job exists, create it if it does not, (and open
# it if it does). It will then check for the forms and load copy them in if they do not exist.
# Then it will update the form information if required, and then open the current form.
# As implied by the name, it is designed to be called from the "F4" button (as dictated by the
# example script binding file).
#
# Written by Ian Ticehurst (Autom8 Ltd)
# Version 1A. 11.09.00 - Initial design and development.
# Version 1B. 08.12.00 - Allow for form configuration at different sites.
# Version 1C. 11.12.00 - Modification to gui windows and fonts used.
# Version 1D. 11.12.00 - Add font and colour information to source file.
# Version 1E. 06.01.01 - Add additional information to source file (company, types and layers).
# Version 1F. 05.02.01 - Add job path to forms.
#                      - Allow user to select job from list.
# Version 1G. 09.02.01 - Allow user to select to keep / delete tmp. step.
#                            - Modify the setting and useage of $TMP.
# Version 1H. 14.03.01 - Check how "$GENESIS_EDIR" is set to ensure paths are set correctly.
#                      - Modify check to see if job already exists (to avoid c-shell limitation).
#                      - Set variable to point to "genesislib" location (in case not in standard place).
# Version 1I. 02.04.01 - Fix problem with "JOB1" variable undefined.
# Version IH. 23.07.01 - Code revamp  (Ben)
######################################################################### 

source $_genesis_root/sys/scripts/include_me

set TMP_DAT  = $TMP/f4dat.$$

# Check to see if $GENESIS_EDIR is local path or absolute & set "EDIR_PATH" accordingly.
if ($GENESIS_EDIR =~ /* || $GENESIS_EDIR =~ ?:*) then
   set EDIR_PATH = ($GENESIS_EDIR)
else
   set EDIR_PATH = ($GENESIS_DIR/$GENESIS_EDIR)
endif 

# Set up path for "genesislib" job (form library).
set LIB_PATH = `$EDIR_PATH/misc/dbutil path jobs genesislib`

# Set up font sizes and colours to use in gui's.

set TITLE_COL  = "772288"
set NORM_COL   = "222299"
set LSTBG_COL  = "222260"
set WARN_COL   = "992222"
set HINT_COL   = "886666"

# 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 "$EDIR_PATH/nt/bin/awk95"
endif

# Test to see if "$JOB" is defined.
if (! $?JOB) then
   # Initial pop-up window. Ask user to enter job name, customer name, and other general details.
   o_tooling
   o FONT $NORM_FONT
   o BG $NORM_COL
   o BW 0
   o LABEL Job Selection Pop-up
   o LABEL Select desired job
   o LIST JOB1 15 S
   $EDIR_PATH/misc/dbutil list jobs | awk -F' ' '{print $1}' >> $GUI_DATA
   o END
   o TEXT JOB2 Job:
   o END

   do_gui

   if ($JOB2 == "") then
      set JOBNAME = "$JOB1"
   else
      set JOBNAME = "$JOB2"
   endif
else
   set JOBNAME = "$JOB"
endif

# Test to see if job already exists.
DO_INFO -t job -e $JOBNAME -d exists
if ($gEXISTS == "yes") then
   set JOB_EXISTS = 1
else
   set JOB_EXISTS = 0
endif

# If job does not exist, create it.
if (! $JOB_EXISTS) then
   # Check for available databases, ask user to confirm job creation.
   set DBS_LIST = `$EDIR_PATH/misc/dbutil list dbs * | awk -F' ' '{print $1}'`

   o_tooling
   o FONT $NORM_FONT
   o BG $NORM_COL
   o BW 0
   o LABEL Job Creation Pop-up
   if ($#DBS_LIST == 1) then
      o LABEL Create $JOBNAME in database $DBS_LIST
      set DBS = "$DBS_LIST"
   else
      o LABEL $JOBNAME will be created
      o LABEL Select database
      o BG $LSTBG_COL
      o LIST DBS 3 S
      foreach DBS ($DBS_LIST)
         o $DBS
      end
      o END
      o BG $NORM_COL
   endif
   o RADIO CONT Options: V 1 992222
   o Create job
   o Abort program
   o END

   do_gui

   if ($CONT == 2) then
      goto abort
   endif

   # Create job and step.
   COM create_entity,job=,is_fw=no,type=job,name=$JOBNAME,db=$DBS,fw_type=form
   COM create_entity,job=$JOBNAME,is_fw=no,type=step,name=pcb,db=$DBS,fw_type=form
endif

# Open job.
COM open_job,job=$JOBNAME

# Get full path of job.
set JOB_PATH = `$EDIR_PATH/misc/dbutil path jobs $JOBNAME`

# If job has just been created, initialise the info.source file.
if (! $JOB_EXISTS) then
   # Initialise info.source file with default values (panelise=no, and netlist=fail).
   echo "set PAN = 0" >> $JOB_PATH/user/info.source
   echo "set NET = 0" >> $JOB_PATH/user/info.source
endif

# Check to see if library forms have been initialised.
if (! -e $LIB_PATH/forms/genform1/def/form_info.source) then
   # Inform user that this is the first time these forms have been run 
   # and configuration information is required.
   o_tooling
   o FONT $NORM_FONT
   o BG $NORM_COL
   o BW 0
   o LABEL Form Information Pop-up.
   o LABEL The system has detected that these forms have not yet been used.
   o LABEL In order to customise these forms some input is required.
   o LABEL The following pop-ups will request information on:
   o LABEL Company name
   o LABEL Board type terminology
   o LABEL Board finishes used
   o LABEL Copper weights used
   o FONT $HINT_FONT
   o BG $HINT_COL
   o LABEL Hint - To re-run this configuration remove the file
   o LABEL form_info.source from $LIB_PATH/forms/genform1/def
   o LABEL Form reconfiguration is also available from buttons on the forms
   o BG $NORM_COL
   o END

   do_gui

   # Form is not initialized. Offer user configuration options.
   o_tooling
   o FONT $NORM_FONT
   o BG $NORM_COL
   o BW 0
   o LABEL Form Initialisation Pop-up (1 of 3).
   o LABEL Enter details for forms:
   o TEXT COMP 10 Company Name (for form label):
   o DTEXT COMP Autom8 Ltd.
   o TEXT KEEP_ORG 4 Keep Pre-Engineering Data:
   o DTEXT KEEP_ORG yes
   o TEXT MAX_LYR 3 Maximum Layer Count:
   o DTEXT MAX_LYR 20
   o LABEL Form Labels For Different Board Types:
   o TEXT SS_BD 20 Single Sided Board:
   o TEXT DS_BD 20 Double Sided Board:
   o TEXT ML_BD 30 Multilayer Board:
   o DTEXT SS_BD Single Sided
   o DTEXT DS_BD Double Sided
   o DTEXT ML_BD Multi-Layer
   o END

   do_gui

   # Board finish parameters.
   o_tooling
   o FONT $NORM_FONT
   o BG $NORM_COL
   o BW 0
   o LABEL Form Initialisation Pop-up (2 of 3).
   o LABEL Enter all possible finish types:-
   o TEXT FIN1 15 Finish Type:
   o TEXT FIN2 15 Finish Type:
   o TEXT FIN3 15 Finish Type:
   o TEXT FIN4 15 Finish Type:
   o TEXT FIN5 15 Finish Type:
   o TEXT FIN6 15 Finish Type:
   o TEXT FIN7 15 Finish Type:
   o TEXT FIN8 15 Finish Type:
   o TEXT FIN9 15 Finish Type:
   o TEXT FIN10 15 Finish Type:
   o DTEXT FIN1 H.A.S.L.
   o DTEXT FIN2 Immersion Gold
   o END

   do_gui

   # Copper weight parameters.
   o_tooling
   o FONT $NORM_FONT
   o BG $NORM_COL
   o BW 0
   o LABEL Form Initialization Pop-up (3 of 3).
   o LABEL Enter all possible copper weights:
   o TEXT CU1 15 Copper Weight:
   o TEXT CU2 15 Copper Weight:
   o TEXT CU3 15 Copper Weight:
   o TEXT CU4 15 Copper Weight:
   o TEXT CU5 15 Copper Weight:
   o TEXT CU6 15 Copper Weight:
   o TEXT CU7 15 Copper Weight:
   o TEXT CU8 15 Copper Weight:
   o FONT $HINT_FONT
   o BG $HINT_COL
   o LABEL Hint - Ensure that copper weights are entered in ascending order
   o LABEL with the lightest copper weight at the top.
   o BG $NORM_COL
   o END

   do_gui

   # Create information source file.
   cat << END > $LIB_PATH/forms/genform1/def/form_info.source 
set TITLE_FONT  = ('$TITLE_FONT')
set NORM_FONT   = ('$NORM_FONT')
set HINT_FONT   = ('$HINT_FONT')
set TITLE_COL   = ('$TITLE_COL')
set NORM_COL    = ('$NORM_COL')
set LSTBG_COL   = ('$LSTBG_COL')
set WARN_COL    = ('$WARN_COL')
set HINT_COL    = ('$HINT_COL')
set COMP        = ('$COMP')
set KEEP_ORG    = ('$KEEP_ORG')
set MAX_LYR     = ('$MAX_LYR')
set BD_TYPE     = ('$SS_BD' '$DS_BD' '$ML_BD')
set FINISH_TYPE = ('$FIN1' '$FIN2' '$FIN3' '$FIN4' '$FIN5' '$FIN6' '$FIN7' '$FIN8' '$FIN9' '$FIN10')
set CU_WGHTS    = ('$CU1' '$CU2' '$CU3' '$CU4' '$CU5' '$CU6' '$CU7' '$CU8')
END

   # Modify library forms header files.
   foreach dir ($LIB_PATH/forms/genform1/def $LIB_PATH/forms/genform2/def)
      sed -e "s/NOT INITIALISED/$COMP/"       \
          -e "s/TBC-SS/$SS_BD/"               \
          -e "s/TBC-DS/$DS_BD/"               \
          -e "s/TBC-ML/$ML_BD/"               \
          -e "s/MAX=20/MAX=$MAX_LYR/"         \  
          -e "s/HASL Finish/$FIN1/"           \
          -e "s/Immersion Gold Finish/$FIN2/" \
          -e "s/#ADDITIONAL FINISH/$FIN3|$FIN4|$FIN5|$FIN6|$FIN7|$FIN8|$FIN9|$FIN10/" \
          -e "s/0.5oz/$CU1|$CU2|$CU3|$CU4|$CU5|$CU6|$CU7|$CU8/" < $dir/hdr.orig \
      | tr '|' '\012' > $dir/hdr
   end
endif

# Check job to see if forms exist. If they do not copy them to job.
if (! -e $JOB_PATH/forms/genform1) then
   o_tooling
   o FONT $NORM_FONT
   o BG $NORM_COL
   o BW 0
   o LABEL Job Details Pop-up
   o LABEL Forms for $JOBNAME do not exist and will be created
   o LABEL Enter job details:
   o TEXT MANUF 10 Manufacturing Number:
   o DTEXT MANUF $JOBNAME
   o TEXT DUE_D 10 Dispatch Date:
   o TEXT TDUE_D 10 Tooling Avail. Date:
   o TEXT CUST 10 Customer Name:
   o TEXT BDR 10 Customer Ref. Number:
   o END

   do_gui

   COM set_attribute,type=job,job=$JOBNAME,name1=,name2=,name3=,attribute=.customer,value=$CUST

   COM copy_form,src_job=genesislib,src_form=genform1,dst_job=$JOBNAME,dst_form=genform1
   if (! -e $JOB_PATH/forms/genform2) then
      COM copy_form,src_job=genesislib,src_form=genform2,dst_job=$JOBNAME,dst_form=genform2
   endif

   # Initialize forms
   echo "elem=job_no,value=$JOBNAME"    >  $TMP_DAT
   echo "elem=manu_no,value=$MANUF"     >> $TMP_DAT
   echo "elem=due_date,value=$DUE_D"    >> $TMP_DAT
   echo "elem=tdue_date,value=$TDUE_D"  >> $TMP_DAT
   echo "elem=bdr,value=$BDR"           >> $TMP_DAT
   echo "elem=cust,value=$CUST"         >> $TMP_DAT

   COM edit_form_list,job=$JOBNAME,form=genform2,in_file=$TMP_DAT

   echo "elem=job_path,value=$JOB_PATH" >> $TMP_DAT

   COM edit_form_list,job=$JOBNAME,form=genform1,in_file=$TMP_DAT
   \rm -f $TMP_DAT
endif

# Check log file to see which form is in progress.
tail -1 $JOB_PATH/user/control_log | grep -q genform1
if ($status == 0) then
   set CURR_FORM = genform1
else
   set CURR_FORM = genform2
endif

COM show_form,job=$JOBNAME,form=$CURR_FORM

abort:

exit 0
