#!/bin/csh
# This is a program for the example Genesis Tooling Forms.
# This file will exist in form 1.
# The purpose of this program is to help the user place the input data in the input directory
# of the job.
# It is called by the "Load Data" button.
# 
# Written by Ian Ticehurst (Autom8 Ltd.)
# Version 1A. Date: 11.09.00 - Initial design and development.
# Version 1B. Date: 11.12.00 - Add source of info. file to support gui creation.
# Version 1C. Date: 12.12.00 - Re-write the gui windows to use the sourced font and colours.
# Version 1D. Date: 12.12.00 - Re-write the gui functions to load files to input directory.
# Version 1E. Date: 05.02.01 - Modify to only check job path if job moved from original location.
#                            - Text change to remove abbreviations.
#                            - Add save time update to forms.
# Version 1F. Date: 09.02.01 - Modify the setting and useage of $TMP.
# Version 1G. Date: 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).
# Version 1H. Date: 03.04.01 - Add current directory to browser, also change priority dir. selection to list.
###########################################################################################

source $_genesis_root/sys/scripts/include_me

set_in_progress genform1 lod

# Close Form
COM close_form,job=$JOB,form=genform1

browse:
# Force directory, file and path variables to be blank.
set DIR_PATH = ""
set DIRS = ""

# Keep running loop until user selects some files.
set FL_CP = 0
while ($FL_CP <= 1)
   # Initial file browser pop-up. Use current dir. for file list, also offer user path text string.
   o_tooling
   o FONT $NORM_FNT
   o BG $NORM_COL
   o BW 0
   o LABEL Directory Browse and File Select.
   o LABEL Enter directory path, browse through directories, or select files
   o TEXT DIR_PATH Go To Directory:
   o DTEXT DIR_PATH $cwd
   o LABEL Directories:
   o BG $LSTBG_COL
   o LIST DIRS 7 S
   ls -aF | grep / | tr -d '\015' >> $GUI_DATA
   o END
   o BG $NORM_COL
   o LABEL Files:
   o BG $LSTBG_COL
   o LIST FILES 9 M
   ls -al | grep ^- | awk -F' ' '{print $9}' >> $GUI_DATA
   o END
   o BG $NORM_COL
   set FILE_CT = `ls -al | grep -c ^-`
   if ($FILE_CT != 0) then
      o LABEL If any files shown are required select options below
      o RADIO FL_CP Copy: V 1 992222
      o None of these files required. Continue browsing.
      o Copy all files shown to $JOB/input
      o Copy selected files to $JOB/input
      o END
      o RADIO MR_FL Files: V 1 992222
      o Copy these and finish browsing
      o Copy these and then browse for more files
      o END
   endif
   o END

   do_gui

   if ($DIRS != "") then
      cd $DIRS
   else if ($DIR_PATH != "") then
      cd $DIR_PATH
   endif

   # Force directory and path variables to be blank ready for next selection.
   set DIR_PATH = ""
   set DIRS = ""
end

if ($FL_CP == 2) then
   # User selected to copy all files into input directory of job.
   cp * $JOB_PATH/input
else if ($FL_CP == 3) then
   # User has selected to copy only selected files into input directory of job.
   cp $FILES $JOB_PATH/input
endif

if ($MR_FL == 2) then
   # User has requested to look for more files. Loop back to browse section.
   unset FILES
   goto browse
endif

# Try to unpack standard compressed (archive) files.
cd $JOB_PATH/input
set FILES = "*"

# Create a tmp. dir. for file uncompression.
mkdir tmp_uncomp_dir.$$

foreach FILE ($FILES)
   # Set post-fix varibles to null values.
   set LCASE_FILE = `echo $FILE | tr 'A-Z' 'a-z'`

   if ($LCASE_FILE:e == "zip") then
      # ".zip" file found, unpack using "unzip -j".
      mv $FILE tmp_uncomp_dir.$$
      cd tmp_uncomp_dir.$$
      unzip -j $FILE
      set ZIP_STATUS = $status

      o_tooling
      o FONT $NORM_FNT
      o BG $NORM_COL
      o BW 0
      o LABEL File Extraction Pop-up
      if ($ZIP_STATUS != 0) then
         o LABEL File $FILE uncompressed using unzip -j
      else
         o BG $WARN_COL
         o LABEL unzip -j $FILE was unsuccessful
         o LABEL See console for failure information
      endif
      o BG $NORM_COL
      o END

      do_gui

      # Move all files back to input dir. Change dir. to input dir.
      mv * ..
      cd ..
   endif

   if ($LCASE_FILE:e == "gz") then
      # ".gz" file found, unpack using "gzip -d".
      mv $FILE tmp_uncomp_dir.$$
      cd tmp_uncomp_dir.$$
      gzip -d $FILE
      set GZIP_STATUS = $status

      o_tooling
      o FONT $NORM_FNT
      o BG $NORM_COL
      o BW 0
      o LABEL File Extraction Pop-up
      if ($GZIP_STATUS == 0) then
         o LABEL $FILE uncompressed using gzip -d
      else
         o BG $WARN_COL
         o LABEL gzip -d $FILE failed
         o LABEL See console for failure information
      endif
      o BG $NORM_COL
      o END

      do_gui

      # Now test to see if this process has extracted a ".tar" file.
      set T_FILE = "*"
      if ("$T_FILE:e" == "tar") then
      	 # ".tar" file found, unpack using "tar xvf".
      	 tar xvf $T_FILE
         set TAR_STATUS = $status

      	 o_tooling
         o FONT $NORM_FNT
         o BG $NORM_COL
         o BW 0
         o LABEL File Extraction Pop-up
         if ($TAR_STATUS != 1) then
            o LABEL tar extraction for $T_FILE successful
         else
            o BG $WARN_COL
            o LABEL tar extraction failed for $T_FILE found in $FILE
            o LABEL See console for failure information
         endif
         o BG $NORM_COL
         o END

      	 do_gui
      endif

      # Move all files back to input dir. Change dir. to input dir.
      mv * ..
      cd ..
   endif

   if ($LCASE_FILE:e == "tgz") then
      # ".tgz" file found, unpack tar/gzip files using genesis import (assume that all .tgz files are ODB).
      set DBS = (`$EDIR_PATH/misc/dbutil list dbs | awk -F' ' '{print $1}'`)
      COM import_job,db=$DBS[1],path=$JOB_PATH/input/$FILE,name=$JOB.odb.$$

      # Check to see if job exists and import is OK.
      DO_INFO -t job -e $JOB -d exists
      if ($gEXISTS == "yes") then
         set IMP_OK = 1
      else
         set IMP_OK = 0
      endif

      if ($IMP_OK) then
         o_tooling
         o FONT $NORM_FNT
         o BG $NORM_COL
         o BW 0
         o LABEL File Extraction Pop-up
         o LABEL File $FILE has been imported as ODB
         o LABEL Job exists in Genesis. Import assumed OK
         o END

         do_gui

         # Find full path of tmp. job.
         set T_JOB_PATH = `$EDIR_PATH/misc/dbutil path jobs $JOB.odb.$$`

         # Import is OK. Copy all of imported job into this job. Then delete imported job.
         $EDIR_PATH/misc/glock -o lock -t job  -e $JOB
         if ($status == 0) then
            cp -r $T_JOB_PATH/* $JOB_PATH
            $EDIR_PATH/misc/glock -o unlock -t job  -e $JOB
            COM delete_entity,job=,type=job,name=$JOB.odb.$$
         else
            COM delete_entity,job=,type=job,name=$JOB.odb.$$
            o_tooling
            o FONT $NORM_FNT
            o BG $NORM_COL
            o BW 0
            o LABEL File Extraction Pop-up
            o LABEL Failed locking $JOB for copying
            o LABEL See console for failure message
            o END

            do_gui
         endif   
      else
         o_tooling
         o FONT $NORM_FNT
         o BG $WARN_COL
         o BW 0
         o LABEL ODB Import Error Pop-up
         o LABEL File $FILE failed the ODB import
         o BG $NORM_COL
         o END

         do_gui
      endif
   endif

   if ($LCASE_FILE:e == "arc" || $LCASE_FILE:e == "exe" || \
       $LCASE_FILE:e == "arj" || $LCASE_FILE:e == "lzh" || $FILE:e == "Z") then
      # Unsupported compressed file found. Warn user.
      o_tooling
      o FONT $NORM_FNT
      o BG $WARN_COL
      o BW 0
      o LABEL Compressed File Detection Pop-up
      o LABEL WARNING - $FILE may be a compressed file.
      o LABEL This file type is not supported by this program
      o LABEL and may require manual uncompression
      o BG $NORM_COL
      o END

      do_gui
   endif
end
# Remove temp. input dir.
\rm -r $JOB_PATH/input/tmp_uncomp_dir.$$

# Open Form
COM show_form,job=$JOB,form=genform1

set_complete genform1 lod

save_job

exit 0
