#!/bin/ksh
# @(#) ASCIIfy function - Bob Stockler - Sun Nov 19 19:19:19 EST 2000
# @(#) Test a process table for being saved in ASCII format
# @(#) and resave it in ASCII format on the fly if it is not.
# @(#) Modified to be called from a script by Jerry Rains 12/7/00
# @(#) Changed Unencoded to Decoded and added 'trap 3' so that script 
# @(#) will not say decoded when it doesn't 12/17/00
# @(#) The environment must be set before calling.
# @(#) 01/19/01 - Modified to work with Linux - Jerry Rains.  This consisted of:
# @(#) Adding -d "\000" to tr command.

# Determine form of echo to use.
if [ `echo "x\c"` = "x" ]
then
    # Then the standard command will work just fine.
    ECHO="echo"
else
    # Otherwise we need to enable the 'backslash' processing.
    ECHO="echo -e"
fi
export ECHO

# 06/24/01 - Set tmpdir if not set and check for PFTEMP being set too.
[ -z "$tmpdir" -a -n "$PFTEMP" ] && tmpdir=$PFTEMP
[ -z "$tmpdir" ] && tmpdir=/tmp

# Get filename of prc. table.
PFNAME=$1
prc=$2
> $tmpdir/ENCODED
trap '
  $ECHO "3" > $tmpdir/ENCODED
  kill -1 $BG_PID 2>/dev/null
  $ECHO 
  $ECHO Could not Decode $PFNAME/prc.$prc.
  sleep 1
  exit
' 2 3
# 06/24/01 - Changed to use awk ' below that also works on ATT Unix.
# Entertain . . .
# $ECHO "\nWorking \c"
# (i=1;until [ $i = 1111111111111111 ];do $ECHO ". \c";i=1$i;done;$ECHO "To Break Out enter Ctrl-\\ \c" ) &
$ECHO
BG_PID=$!
  # Assure ABE=ASCII is set in the environment for this function:
  # Construct a command to resave a table in ASCII format:
  CMD="echo \"3$prc\n\033\033NNX\"|$PFPROG/fp/dcabe $PFNAME -y '' >/dev/null"
  # Regular expression to match tables saved in ASCII:
  # 06/24/01 - Changed line below and incorporated into standard awk ' statement
  # RE="^[ -9;-~]*:[ -9;-~]*:[ -9;-~]*:[ -~]*$"
  # Use 'tr' to remove possible NUL characters in encoded
  # tables because all AWKs (except 'gawk') choke on them:
  ABE=ascii export ABE
  tr -d "\000" < $PFDATA$PFDIR/filepro/$PFNAME/prc.$prc |
  # 06/24/01 - Changed awk statement below because ATT Unix wouldn't recognize
  #            -v.
  # awk -v CMD="$CMD" -v RE="$RE" '{if($0!~RE){system(CMD);exit 99};exit}'
  awk '
    $0 != "" {
      if ($0 !~ /^[ -9;-~]*:[ -9;-~]*:[ -9;-~]*:[ -~]*$/)
      {
        printf "To Break Out enter Ctrl-\\ "
        system(CMD)
      }
      else exit
      exit
    }
    ' CMD="$CMD" RE="$RE" PFPROG="$PFPROG" PFNAME="$PFNAME"
  STATUS=$?
  kill -1 $BG_PID 2>/dev/null
  [ $STATUS -eq 99 ] && {
    ENCODED=`cat $tmpdir/ENCODED`
    [ "$ENCODED" != 3 ] && {
      $ECHO 1 > $tmpdir/ENCODED
      $ECHO
      $ECHO Decoded $PFNAME/prc.$prc.
      }
    } || ENCODED=""
