

#!/bin/ksh
######################################################################
# mcplot - plotting from mac's to unix workstation                   #
# written by: Don Stone                                              #
# date      : 11-16-93                                               #
#                                                                    #
#                                                                    #
# Donald C. Stone                                 _/_/_/_/_/ _/_/    #
# Information Systems Analyst                    _/_/       _/_/     #
# California Department of Transportation       _/_/   _/_/_/_/_/_/  #
# District 03 Main Office                      _/_/       _/_/       #
# 703 'B' Street                              _/_/_/_/_/ _/_/  _/    #
# Marysville, California, USA 95901                     _/_/_/_/     #
# Phone: (916)741-4031                                               #
# Email: dstone%trmx2@dot.ca.gov                   Caltrans          #
#                                                                    #
#                                                                    #
# First, create a directory for the script to reside.  Define the    #
# below varible DIR as that directory.  Then, create sub-directories #
# called "./tmp", "./que", and "./log".  Then create a text file     #
# called "control" with the following format:                        #
#                                                                    #
#   yes  000.000.000.000  queue_name  folder_name                    #
#                                                                    #
# The first column should be yes or no indicating whether the script #
# uses the information or not, second column is the IP address of    #
# the Mac, third column is the name the plot queue, and the fourth   #
# column is the full pathname of the folder on the Mac.  Finally,    #
# since this script is currently configured for Intergraph's IPLOT   #
# plotting software, copy a default pentable and colortable by the   #
# using the names "igds.tbl" and "igds.ctb".  Also, define the vari- #
# bles FNT and SCR pointing to the default font library and scratch  #
# directory.                                                         #
#                                                                    #
######################################################################

### Define version.  ###
PRG="Mac Plotting"
VER="1.3"

### Define header and spacing. ###
HDR="%%McPLOT:"
HSP="         "

### Define system directory/file names. ###
DIR="/usr/local/dp"
QUE="$DIR/que"
TMP="$DIR/tmp"
LOG="$DIR/log"
COM="$TMP/comm"
TBL="$DIR/igds.tbl"
CTB="$DIR/igds.ctb"
FNT="/usr/ip32/ip/igds/resrc/fontlib"
SCR="/usr/plotwrk/iplot/scr"

### Remote login for UNIX servers. ###
USER="t3vers"
PASS="plotter"

### Determine terminal for verbose option. ###
if [ "$1" = "-v" -o "$1" = "-V" ]; then
  TERM="$LOG/dplot.log"
  tty | read TERM
else
  TERM="$LOG/dplot.log"
fi

### Begin program loop. ###
GO="0"
while [ "$GO" = "0" ]; do

  ### Check for loop switch. ###
  if [ "$1" = "1" -o "$2" = "1" -o "$3" = "1" ]; then
    GO="1"
  fi

  echo "\n$HDR $PRG $VER"                                      >> $TERM
  echo "$HSP (Submitting postscript files)"                    >> $TERM
  date +"$HSP %a %h %d,19%y %T"                                >> $TERM
 
  ### Read control to determine which plot queues. ###
  CONTROL="0"
  if [ ! -f $DIR/control ]; then
    touch $DIR/control
    echo "$HDR No control file!"  >> $TERM
  fi
  cat $DIR/control | grep -v "#"     | 
                     grep "Plotters" | 
                     grep "yes "     |
  while read YESNO TCP PLOTTER REMDIR; do

    ### Get plot files from remote server. ###
    echo "$HDR Searching remote server ($TCP) at:"      >> $TERM
    echo "$HSP $REMDIR"                                 >> $TERM
    echo "$HSP Attempting file transfer ...\c"          >> $TERM
    echo "\nuser $USER $PASS"  >  $COM.1
    echo "cd \042$REMDIR\042"  >> $COM.1
    echo "lcd $QUE"            >> $COM.1
    echo "ascii"               >> $COM.1
    echo "mget *"              >> $COM.1
    echo "ls"                  >> $COM.1
    ftp -vni $TCP < $COM.1     >  $TMP/ftp.tmp 2>  $TMP/ftp.tmp
    echo "\n\n"                >> $LOG/ftp.log 2>> $LOG/ftp.log
    cat $TMP/ftp.tmp           >> $LOG/ftp.log 2>> $LOG/ftp.log
    echo " "  >> $TERM

    ### Deleting NULL plot files. ###
    echo "$HSP Deleting null plot files ...\c"  >> $TERM
    find $QUE -type f -size -5 -print |
    while read FILE; do rm -f "$FILE"; done
    echo " "  >> $TERM

    ### Submit remaining files to plotter. ###
    FLAG1="0"
    find $QUE -type f -print |
    while read FILE; do

      ###                                          ###
      ### Determine file type; postscript or igds. ###
      ###                                          ###
      TYPE="invalid"
      IFS=":"
      file "$FILE" | tr -d "\011" | read CHUNK1 CHUNK2
      IFS=" "
      echo "$CHUNK2" | read FLAG2 EXTRA 
      if   [ "$FLAG2" = "text"     ]; then  FLAG2="ascii"; fi
      if   [ "$FLAG2" = "commands" ]; then  FLAG2="ascii"; fi
      if   [ "$FLAG2" = "unstruct" ]; then  FLAG2="ascii"; fi
      if   [ "$FLAG2" = "Postscript" ]; then
        TYPE="script"
      elif [ "$FLAG2" = "data" ]; then
        TYPE="igds"
      elif [ "$FLAG2" = "ascii" ]; then
        head "$FILE" | grep -c "%%Creator" | read CREATOR
        if [ "$CREATOR" = "1" ]; then
          TYPE="script"
        fi
      fi
      if [ "$TYPE" = "invalid" ]; then
        echo "$HSP $FILE is an invalid plotfile!!!"  >> $TERM
        echo "$HSP $FILE is an invalid plotfile!!!"  >  "$FILE"
      fi

      ###                              ###
      ### Submit IGDS file to plotter. ###
      ###                              ###
      if [ "$TYPE" = "igds" ]; then

        ### Check for plot scale. ###
        SCALE="50"
        echo "$FILE" | tr "." "/" | read TEMP
        basename "$TEMP" | tr -d [a-z][A-Z] | 
                           tr -d "._+:/\ #" | read SCALE
        if [ ! "$SCALE" ]; then SCALE="50"; fi
        let a=$SCALE
        if [ $a -lt 1 ]; then SCALE="50"; fi
        SCALE="$SCALE.0:1.0"

        ### Determine IPARM filename. ###
        basename "$FILE" | read TEMP
        echo "$TEMP" | tr "." " " | read IPARM EXTRA
        IPARM="$SCR/$IPARM.i"

        ### Create iparm/meta files and submit it. ###
        echo "$HSP Submitting $FILE ($TYPE/$SCALE) to $PLOTTER ...\c"  >> $TERM
        echo "iplot create "$IPARM" -queue=$PLOTTER \c"         >  $COM.3
        echo "-design=\042$FILE\042 -view=1 -units=inches \c"   >> $COM.3
        echo "-scale=$SCALE -data_rescale=1.0,1.0 \c"           >> $COM.3
        echo "-pen_table=$TBL -color_table=$CTB -fontlib=$FNT"  >> $COM.3
    	echo "iplot generate \042$IPARM\042"                    >> $COM.3
        echo "iplot submit \042$IPARM\042"                      >> $COM.3
        chmod 777 $COM.3
        $COM.3  > $TMP/iplot.tmp 2> $TMP/iplot.tmp

        ### Create message file. ###
        cat $TMP/iplot.tmp  >> $LOG/iplot.log 2>> $LOG/iplot.log
        echo "$PRG $VER"                       >  "$FILE"
        date +"Submitted to $PLOTTER at %D.%T" >> "$FILE"
        cat $TMP/iplot.tmp                     >> "$FILE"
        echo " "  >> $TERM
      fi

      ###                                    ###
      ### Submit postscript file to plotter! ###
      ###                                    ###
      if [ "$TYPE" = "script" ]; then

        ### Check for paper size. ###
        PAPER="E"
        echo "$FILE" | tr "." "/" | read TEMP
        basename "$TEMP" | tr [a-z] [A-Z] | read SIZE
        if [ "$SIZE" = "A" ]; then PAPER="$SIZE"; fi
        if [ "$SIZE" = "B" ]; then PAPER="$SIZE"; fi
        if [ "$SIZE" = "C" ]; then PAPER="$SIZE"; fi
        if [ "$SIZE" = "D" ]; then PAPER="$SIZE"; fi
  
        echo "$HSP Submitting $FILE ($TYPE/$PAPER) to $PLOTTER ...\c"  >> $TERM
        qpr "$FILE" -t $TYPE -q $PLOTTER -o "-a $PAPER"  > $TMP/qpr.tmp 2>
$TMP/qpr.tmp

        ### Create message file. ###
        cat $TMP/qpr.tmp  >> $LOG/qpr.log 2>> $LOG/qpr.log
        echo "$PRG $VER"                       >  "$FILE"
        date +"Submitted to $PLOTTER at %D.%T" >> "$FILE"
        cat $TMP/qpr.tmp                       >> "$FILE"
        echo " "  >> $TERM
      fi

      ### Copy message file to remote server. ###
      echo "$HSP Sending message file to remote server ...\c"  >> $TERM
      basename "$FILE" | read BNAME
      cat $TMP/ftp.tmp | grep -v  "local:" |
                         grep -v "remote:" |
                         grep     "$BNAME" | read DFILE
      echo "$DFILE" | read DFILE
      echo "\nuser $USER $PASS"                       >  $COM.2
      echo "cd \042$REMDIR\042"                       >> $COM.2
      echo "lcd $QUE"                                 >> $COM.2
      echo "ascii"                                    >> $COM.2
      echo "send \042$FILE\042 \042$DFILE\042"        >> $COM.2
      ftp -vni $TCP < $COM.2   >> $LOG/ftp.log 2>> $LOG/ftp.log
      echo " "  >> $TERM
      rm -f "$FILE"

      FLAG1="1"
    done
    if [ "$FLAG1" = "0" ]; then
      echo "$HSP No files copied or submitted!"  >> $TERM
    fi

    ### Send plot status to remote server. ###
    echo "$HSP Sending queue status to remote server ...\c"  >> $TERM
    echo "$PRG $VER" >  $TMP/qstat.tmp
    date             >> $TMP/qstat.tmp
    uname -a         >> $TMP/qstat.tmp
    echo "\n"        >> $TMP/qstat.tmp
    qstat $PLOTTER   >> $TMP/qstat.tmp
    echo "$PLOTTER Status" | tr " " "_" | read STATUS
    echo "\nuser $USER $PASS"                   >  $COM.5
    echo "cd \042$REMDIR\042"                   >> $COM.5
    echo "ascii"                                >> $COM.5
    echo "send $TMP/qstat.tmp \042$STATUS\042"  >> $COM.5
    ftp -vni $TCP < $COM.5   >> $LOG/ftp.log 2>> $LOG/ftp.log
    echo " "  >> $TERM

    sleep 5
    CONTROL="1"
  done
  if [ "$CONTROL" = "0" ]; then
    echo "$HSP No control data!"  >> $TERM
  fi

  ###                                           ###
  ### Purging log files and deleting tmp files. ###
  ###                                           ###
  echo "$HDR Purging log files ...\c"  >> $TERM
  find $LOG -type f -print |
  while read FILE; do
    tail -1000 $FILE > $TMP/logfile
    cp $TMP/logfile $FILE
  done
  echo "\n$HSP Deleting tmp files ...\c"  >> $TERM
  rm -f $TMP/*
  echo " "  >> $TERM


  ### The end of the loop! ###
  echo "$HDR [!]\n"  >> $TERM
  if [ "$GO" = "0" ]; then
    sleep 240
  fi
done
### The end! ###

