#!/bin/csh

#

# Written by Bjarne Nielsen

# Revideret til Priform5 med support for XAL af Pawel Kramarz

#########################################################################


set PKXALPATH = "c:/Users/pkr.PRIDANA/.genesis/scripts/pkXAL3.pl"


# Find out the user name.

COM get_user_name

set USER = `echo "$COMANS"`


# Test to see if "$JOB" is defined.

if ($?JOB) then

   set JOBNAME = $JOB

   set silent = 0

else

   #call the GUI for varenummer

   echo "WIN 200 200" >> $GUI_DATA

   echo "FONT hbr18" >> $GUI_DATA

   echo "BG 222299" >> $GUI_DATA

   echo "FG 999999" >> $GUI_DATA

   echo "BW 0" >> $GUI_DATA

   echo "LABEL Insert Intern Nr." >> $GUI_DATA

   echo "TEXT JOBNAME 14 Intern Nr.   :" >> $GUI_DATA

   echo "END" >> $GUI_DATA

   

   gui $GUI_DATA > $GUI_RESP

   source $GUI_RESP; \rm $GUI_RESP; \rm $GUI_DATA


   if ($JOBNAME == "") then        # varenummer not inserted

       PAUSE "The varenummer cannot be empty. Script exits."

       exit

       

   else      # varenumer inserted, does the job exist in the file structure?

       set JOBNAME = `echo "$JOBNAME" | awk '{print tolower($1)}'`

       set jobstat = `echo $JOBNAME Match | gawk '{ if ( $1 ~/........\...../ ){print $2}}'`

       if ($jobstat != "Match") then

           PAUSE "Jobname has to be standard e.g. 123x4567.0844. Program will abort"

           exit

       endif

       

       set JOB_PATH = `$GENESIS_EDIR/misc/dbutil path jobs $JOBNAME`

       if ($JOB_PATH != "") then

           DO_INFO -t job -e $JOBNAME -d exists    # Test to see if job already exists.

           if ($gEXISTS == "yes") then

               PAUSE "$JOBNAME already exists in file structure. Program exits."

               exit

           endif    

       endif    


       echo my_pwd: `pwd`

       # check if the job exists in the XALDB

       set RESULT = `perl.exe $PKXALPATH -checkVareNummer $JOBNAME`

       echo RESULT: $RESULT


       # the job doesn't exist in the XALDB nor in the file structure

       if ($RESULT == 0) then

           set silent = 0

           PAUSE "$JOBNAME does not exist in XALDB and will be created with empty values."

       else

           # the job exists in the XALDB, the data will be silent read from the DB

           set XAL_RESULT = `perl.exe $PKXALPATH -prepare_GUIResp $JOBNAME`

           set silent = 1

           # read the XAL_RESULT to myXALArray[]

           set myXALArray

           set myLine

           foreach myLine ($XAL_RESULT)

               #echo $myLine

               #set myFeature = ($myLine:x)                # split on single word

               set myFeature = ($myLine:as/ / /)            # split on space

               set j = 1

               while ($j <= $#myFeature)

                   set myXALArray = ( $myXALArray $myFeature[$j] )

                   @ j++

               end

           end        # end of foreach

   

           # assign some variables according to gui_resp

           set ordrenummer = $myXALArray[4]

           set varenummer = $myXALArray[8]

           set REVISION = $myXALArray[12]

           set KUNDE = $myXALArray[16]

           if ($KUNDE == "IO_error") then

               PAUSE "kundelist.txt mangler i v:\\sys\\dokumenter"

           else if ($KUNDE == "not_found") then

               PAUSE "Kundenavn mangler i v:\\sys\\dokumenter\\kundelist.txt"

           endif

           set Tegn_Nr = $myXALArray[20]

           set tykkelse = $myXALArray[24]

           set materiale = $myXALArray[28]

           set overflade = $myXALArray[32]

           set scoring = $myXALArray[36]

           set nitte_fix = $myXALArray[40]

           set afrivelig = $myXALArray[44]

           set plugged = $myXALArray[48]

           set galguld = $myXALArray[52]

           set kul = $myXALArray[56]

           set keramisk = $myXALArray[60]

           set klasse = $myXALArray[64]

       endif    # end of checkVareNummer

   endif    #end of $JOBNAME

endif    # end of $JOB



# Test to see if job already exists.

DO_INFO -t job -e $JOBNAME -d exists

if ($gEXISTS == "yes") then

   set JOB_EXIST = 1

else

   set JOB_EXIST = 0

endif



# If job does not exist, create it.

if ($JOB_EXIST == 0) then

   # Create job and step.

   COM create_entity,job=,is_fw=no,type=job,name=$JOBNAME,db=genesis,fw_type=form

   COM create_entity,job=$JOBNAME,is_fw=no,type=step,name=pcb,db=genesis,fw_type=form

endif


#Formselect

DO_INFO -t job -e $JOBNAME -d FORMS_LIST

if ($gFORMS_LIST != "") then

   set formname = $gFORMS_LIST[1]

else

   set formname = priform5

endif


# Get full path of job.

set JOB_PATH = `$GENESIS_EDIR/misc/dbutil path jobs $JOBNAME`

set DUE_D = `date`


# Job and form exist

if (! -e $JOB_PATH/forms/$formname && $JOB_EXIST == 1) then

   echo "WIN 200 200" >> $GUI_DATA

   echo "FONT hbr18" >> $GUI_DATA

   echo "BG 222299" >> $GUI_DATA

   echo "FG 999999" >> $GUI_DATA

   echo "BW 1" >> $GUI_DATA

   echo "LABEL Job existerer allerede uden PRIFORM." >> $GUI_DATA

   echo "LABEL Vil du oprette en form til det." >> $GUI_DATA

   echo "FONT hbr14" >> $GUI_DATA

   echo "BG 222299" >> $GUI_DATA

   echo "BW 0" >> $GUI_DATA

   echo "RADIO CONT Options: V 1 992222" >> $GUI_DATA

   echo "Yes, create form" >> $GUI_DATA

   echo "No, abort program" >> $GUI_DATA

   echo "END" >> $GUI_DATA


   gui $GUI_DATA > $GUI_RESP

   source $GUI_RESP; \rm $GUI_RESP; \rm $GUI_DATA


   if ($CONT == 2) then

       goto abort

   endif

endif    



if ((! -e $JOB_PATH/forms/$formname) && ($silent == 0)) then

   switch ($materiale)

       case "1":

           set materiale = Not_Set

           breaksw

       case "2":

           set materiale = FR4

           breaksw

       case "3":

           set materiale = HighTG

           breaksw

       case "4":

           set materiale = Rogers

           breaksw

       case "5":

           set materiale = 104i

           breaksw

       case "6":

           set materiale = 85n

           breaksw

       case "7":

           set materiale = 85nt

           breaksw

       case "8":

           set materiale = IS400

           breaksw

       case "9":

           set materiale = Teflon

           breaksw

   endsw


   switch ($overflade)

       case "1":

           set overflade = Not_Set

           breaksw

       case "2":

           set overflade = HAL

           breaksw

       case "3":

           set overflade = Kem.Guld

           breaksw

       case "4":

           set overflade = Guld.total

           breaksw

       case "5":

           set overflade = Kem.Tin

           breaksw

       case "6":

           set overflade = Blyfri_HAL

           breaksw

       case "7":

           set overflade = Kem.Silver

           breaksw

       case "8":

           set overflade = Hot_oil

           breaksw

   endsw


   switch ($scoring)

       case "1":

           set scoring = No

           breaksw

       case "2":

           set scoring = Yes

           breaksw

   endsw


   switch ($nitte_fix)

       case "1":

           set nitte_fix = No

           breaksw

       case "2":

           set nitte_fix = Yes

           breaksw

   endsw


   switch ($afrivelig)

       case "1":

           set afrivelig = No

           breaksw

       case "2":

           set afrivelig = Top

           breaksw

       case "3":

           set afrivelig = Bottom

           breaksw

       case "4":

           set afrivelig = Both

           breaksw

   endsw


   switch ($plugged)

       case "1":

           set plugged = No

           breaksw

       case "2":

           set plugged = Top

           breaksw

       case "3":

           set plugged = Bottom

           breaksw

       case "4":

           set plugged = Both

           breaksw

   endsw


   switch ($galguld)

       case "1":

           set galguld = No

           breaksw

       case "2":

           set galguld = Top

           breaksw

       case "3":

           set galguld = Bottom

           breaksw

       case "4":

           set galguld = Both

           breaksw

   endsw


   switch ($kul)

       case "1":

           set kul = No

           breaksw

       case "2":

           set kul = Top

           breaksw

       case "3":

           set kul = Bottom

           breaksw

       case "4":

           set kul = Both

           breaksw

   endsw

   

   switch ($keramisk)

       case "1":

           set keramisk = No

           breaksw

       case "2":

           set keramisk = Keramisk

           breaksw

       case "3":

           set keramisk = Viafill

           breaksw

       case "4":

           set keramisk = Both

           breaksw

   endsw


   switch ($klasse)

       case "1":

           set klasse = Standard

           breaksw

       case "2":

           set klasse = IPC_kl.3

           breaksw

       case "3":

           set klasse = IPC6012DS

           breaksw

       case "4":

           set klasse = ECSS

           breaksw

   endsw


   

   # Create notater.txt

   if !(-e $JOB_PATH/user/notater.txt) then

       echo "$JOBNAME" > $JOB_PATH/user/notater.txt

   endif

endif


# Open job.

COM check_inout,mode=out,type=job,job=$JOBNAME

COM open_job,job=$JOBNAME


set DT1 = `echo "$DUE_D" | cut -c 8-10`

set DT2 = `echo "$DUE_D" | cut -c 5-7`

set DT3 = `echo "$DUE_D" | cut -c 20-24`

set DUE_T = `echo "$DUE_D" | cut -c 11-19`

set DUE_D = `echo "$DT1 $DT2 $DT3"`


# If job has just been created, initialise the info.source file.

if (! -e $JOB_PATH/user/info.source) then

   if (! -e $JOB_PATH/user) then

       mkdir $JOB_PATH/user

   endif

   # Initialise info.source file with default values.

   echo "set PAN = 0" >> $JOB_PATH/user/info.source

   echo "set netcmp = 0" >> $JOB_PATH/user/info.source

   echo "set drc_pg_inner = 0" >> $JOB_PATH/user/info.source

   echo "set drc_sig_inner = 0" >> $JOB_PATH/user/info.source

   echo "set drc_pg_yder = 0" >> $JOB_PATH/user/info.source

   echo "set drc_sig_yder = 0" >> $JOB_PATH/user/info.source

   echo "set drc_mask = 0" >> $JOB_PATH/user/info.source

   echo "set drc_silk = 0" >> $JOB_PATH/user/info.source

   echo "set drc_drl = 0" >> $JOB_PATH/user/info.source

   echo "set drc_profile = 0" >> $JOB_PATH/user/info.source

   echo "set inner_15 = 0" >> $JOB_PATH/user/info.source

   echo "set yder_15 = 0" >> $JOB_PATH/user/info.source

   echo "set borekontrol = 0" >> $JOB_PATH/user/info.source

#    echo "" >> $JOB_PATH/user/info.plot

#    echo "" >> $JOB_PATH/user/scale_info

endif


# Check job to see if forms exist. If they do not copy them to job.

if (! -e $JOB_PATH/forms/$formname) then

   COM copy_form,src_job=genesislib,src_form=$formname,dst_job=$JOBNAME,dst_form=$formname

   # Initialise the form.

   COM edit_form,job=$JOBNAME,form=$formname,elem=job_path,value=$JOB_PATH

   COM edit_form,job=$JOBNAME,form=$formname,elem=int_nr,value=$JOBNAME

   COM edit_form,job=$JOBNAME,form=$formname,elem=big_nr,value=$JOBNAME

   COM edit_form,job=$JOBNAME,form=$formname,elem=ap,value=none

   COM edit_form,job=$JOBNAME,form=$formname,elem=tegn_nr,value=$Tegn_Nr

   COM edit_form,job=$JOBNAME,form=$formname,elem=tykkelse,value=$tykkelse

   COM edit_form,job=$JOBNAME,form=$formname,elem=rev,value=$REVISION

   COM edit_form,job=$JOBNAME,form=$formname,elem=kunde,value=$KUNDE

   COM edit_form,job=$JOBNAME,form=$formname,elem=due_date,value=$DUE_D

   COM edit_form,job=$JOBNAME,form=$formname,elem=tdue_date,value=$DUE_T

   COM edit_form,job=$JOBNAME,form=$formname,elem=overflade,value=$overflade,opt_name=yes,callback=no

   COM edit_form,job=$JOBNAME,form=$formname,elem=materiale,value=$materiale,opt_name=yes,callback=no

   COM edit_form,job=$JOBNAME,form=$formname,elem=scoring,value=$scoring,opt_name=yes,callback=no

   COM edit_form,job=$JOBNAME,form=$formname,elem=keramisk,value=$keramisk,opt_name=yes,callback=no

   #COM edit_form,job=$JOBNAME,form=$formname,elem=klasse,value=$klasse,opt_name=yes,callback=no

   #COM edit_form,job=$JOBNAME,form=$formname,elem=nitter,value=$nitte_fix,opt_name=yes,callback=no

   COM edit_form,job=$JOBNAME,form=$formname,elem=afrivelig,value=$afrivelig,opt_name=yes,callback=no

   COM edit_form,job=$JOBNAME,form=$formname,elem=plugged,value=$plugged,opt_name=yes,callback=no

   COM edit_form,job=$JOBNAME,form=$formname,elem=galguld,value=$galguld,opt_name=yes,callback=no

   COM edit_form,job=$JOBNAME,form=$formname,elem=kul,value=$kul,opt_name=yes,callback=no

   if ($tykkelse != "") then

       set tykkelse_mm = `echo $tykkelse| gawk -F' ' '{print ($1/1000)}'`

       COM set_attribute,type=job,job=$JOBNAME,name1=,name2=,name3=,attribute=.board_thickness,value=$tykkelse_mm,units=mm

   endif

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

else

   COM read_form,job=$JOBNAME,form=$formname,elem=int_nr

   set int_nr = `echo "$COMANS"`

   if ($JOBNAME != $int_nr) then

       COM edit_form,job=$JOBNAME,form=$formname,elem=int_nr,value=$JOBNAME    

       COM edit_form,job=$JOBNAME,form=$formname,elem=big_nr,value=$JOBNAME

       COM edit_form,job=$JOBNAME,form=$formname,elem=job_path,value=$JOB_PATH

   endif

endif

COM show_form,job=$JOBNAME,form=$formname,updonly=No,updelem=


abort:


exit