#!/bin/sh
# e	Edit an ASCII prc. table.  Written by Jerry Rains.  6/15/00
#	For features see help().
#	ASCIIfy() courtesy Bob Stockler added 11/26/00.
#       Changed to call separate ASCIIfy program 1/31/01.
#	Fixed bug that kept syntax check from working in command mode 3/14/01
#       Added trap 2 3 before calling ASCIIfy so it won't barf on site
#       password protected files 3/18/01.
#       Added save of original prc. table 6/25/01. Set SavDir to the directory
#       you want to save prc. tables in.  Default is /tmp.

# 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

# If run from the command line $instdir needs to be set to location of ASCIIfy
[ $instdir ] || {
    type=`type ASCIIfy`
    nf="not found"
    case $type in
      *$nf) {
            $ECHO "\nASCIIfy is not in the PATH. \c"
            read key
            exit
            };;
         *) instdir=`echo "$type" | sed -e "s,\(.* \)\(.*/\)\(.*\),\2,"`;;
    esac
    instdir=`dirname "$instdir"ASCIIfy`
    [ -x $instdir/ASCIIfy ] || {
        $ECHO "\nASCIIfy is not in the PATH! \c"
        read key
        exit
        }
    }

help () {
    clear
    $ECHO
    $ECHO "  Usage - e [filename]   (Do not include '.prc' in the filename)"
    $ECHO 
    $ECHO "  To move to a filePro file    -  Enter the name of the filePro file."
    $ECHO "  To return to the files list  -  Enter .."
    $ECHO "  To edit a process table      -  Enter the name of the table (no prc.)"
    $ECHO "  To change the EDITOR [vi]    -  Enter editor=[name]  (full path not required)"
    $ECHO "                                  'e' only checks that the file is executable"
    $ECHO "                                  and in the PATH.  For example: 'editor=less'"
    $ECHO "  To change PFDATA/PFDIR       -  Enter full pathname of PFDATA/PFDIR"
    $ECHO "  To change PFPROG             -  Enter PFPROG=[pathname of PFPROG]"
    $ECHO "  To exit                      -  Enter an x or Press Delete"
    $ECHO
    $ECHO "  'e' will edit a filePro processing table.  If a processing table is"
    $ECHO "  encoded 'e' will automatically decode it (Courtesy Bob Stockler's ASCIIfy"
    $ECHO "  program, which has been encorporated into 'e'.)  On exit, it will reencode"
    $ECHO "  (if it was originally encoded), then check the syntax of the processing"
    $ECHO "  table being edited."
    $ECHO
    $ECHO "  'e' can be used from the command line or from a menu.  If you include a"
    $ECHO "  'filename' when you launch 'e', it will first look in the current working"
    $ECHO "  directory for that processing table.  (Do not include the prc. in the"
    $ECHO "  filename.)  If the processing table is not found there 'e' will display"
    $ECHO "  all filePro files in the PFDATA/PFDIR/filepro directory."
    $ECHO
    $ECHO -n "  (cont'd) "
    read key
    clear
    $ECHO
    $ECHO "  Select a filePro file and 'e' will open 'filename' if found or create it if"
    $ECHO "  not found.  You can use a '*' in the filename, prc. names can not use '*'."
    $ECHO
    $ECHO "  If too many entries display you can use metacharacters such as '[a-n]'"
    $ECHO "  or the first 1 or 2 letters of the filename to limit the number of entries"
    $ECHO "  displayed.  The last metacharacters entered will be remembered, so once"
    $ECHO "  you limit the number of files or prc'.s displayed the display will stay"
    $ECHO "  the same until you change the metacharacters or press <Enter> to clear them."
    $ECHO
    $ECHO "  If you enter a prcname that is greater than 2 characters and is not found"
    $ECHO "  you will be asked if you want to create it."
    $ECHO
    $ECHO -n "			Press Enter to continue "
    read key
    }

RE_ENCODE () {
  $ECHO
  $ECHO "Reencoding "$PFNAME/prc.$prc.
  sleep 1
  # Construct a command to resave a table:
  CMD="$ECHO \"3$prc\n\033\033NNX\"|$PFPROG/fp/dcabe $PFNAME -y '' >/dev/null"
  ABE="" awk -v CMD="$CMD" 'BEGIN{system(CMD);exit}'
  trap  2
}

check_func () {
    chgedt=`$ECHO $func | grep editor=`
    [ $chgedt ] && {
        func=$savfunc
        editr=`$ECHO $chgedt | sed -e "s,editor=,,"`
        savdir=`pwd`
        hash -r
        cd /
        is_cmd=`type $editr 2>&1 | sed -e "s,$editr is ,,"`
        tis_cmd=`$ECHO $is_cmd | grep '/'`
        cd $savdir
        if [ -n "$tis_cmd" -o "$editr" = "dclerk" -o "$editr" = "dreport" ]
        then
            $ECHO
            $ECHO "Changing editor to "$editr
            $ECHO
            editrsv=$editor
            editor=$editr
            $ECHO -n "Press Enter to continue. "
            read key
        else
            $ECHO
            $ECHO "$editr not found.  Using 'vi'"
            $ECHO
            $ECHO -n "Press Enter to continue. " 
            read key
            editor=vi
            editrsv=vi
        fi
        }
    chgprog=`$ECHO $func | grep PFPROG=`
    [ $chgprog ] && {
        prog=`$ECHO $func | sed -e "s,PFPROG=,,"`
        if [ -d "$prog/fp" ]
        then
            $ECHO 
            $ECHO "Changing PFPROG to "$prog
            PFPROG=$prog
            export PFPROG
        else
            $ECHO
            $ECHO "$prog does not point to the ./fp directory.  Try again. "
        fi
        $ECHO
        $ECHO -n "Press Enter to continue. "
        read key
        }
    chgqual=`$ECHO $func | grep PFQUAL=`
    [ $chgqual ] && {
        qual=`$ECHO $func | sed -e "s,PFQUAL=,,"`
        if [ $qual ] 
        then
            qual="-m $qual"
        else
            qual=""
        fi
        $ECHO 
        $ECHO "Changing PFQUAL to "$qual
        PFQUAL=$qual
        export PFQUAL
        $ECHO
        $ECHO -n "Press Enter to continue. "
        read key
        }
    dirp=`$ECHO $func | grep \/`
    [ -n "$dirp" ] && {
        func=$savfunc
        [ -d $dirp/filepro ] && {
            PFDATA=""
            PFDIR=$dirp
            $ECHO
            $ECHO "PFDIR changed to $dirp"
            export PFDATA PFDIR
            pf=$PFDIR/filepro
            cd $pf
            if [ -f $dirp/fp/lib/errmsg -a $dirp != $PFPROG ]
            then
                $ECHO
                $ECHO -n "Want to change PFPROG to "$dirp"? [N] "
                read yesno
                [ $yesno ] || yesno="n"
                yesno=`$ECHO $yesno | sed -e "y/YN/yn/"`
                [ "$yesno" = "y" ] && {
                    PFPROG=$dirp
                    export PFPROG
                    }
            else
                $ECHO
                $ECHO -n "Press Enter to continue. "
                read key
            fi
            }
        }
    }

whichfile () {
clear
until [ -d $PFDATA$PFDIR/filepro/$file ]
do
    clear
    cd $PFDATA$PFDIR/filepro
    if [ "$file" = "-" ]
    then
        for j in `$ECHO *`
        do
            [ -s $j/map ] && printf "%-16.16s" $j
        done
        $ECHO
    else
        for j in `$ECHO $file*`
        do
            [ -s $j/map ] && printf "%-16.16s" $j
        done
        $ECHO
    fi
    $ECHO -n "Move to which file? (h=help - x=exit) "
    savfunc=$file
    read file
    file=`$ECHO $file | sed -e "s/ .*//"`
    func=$file
    check_func
    file=$func
    savfile=$savfunc
    [ $file ] || file="-"
    [ "$file" = "x" ] && exit
    [ "$file" = "h" ] && {
        help
        file="-"
        }
done
cd $PFDATA$PFDIR/filepro/$file
# If in filepro file directory and filename doesn't exist create it.
[ -n "$prc" -a -f map -a ! -f "prc.$prc" ] && {
    touch prc.$prc
    chown filepro prc.$prc
    chmod 600 prc.$prc
    }
clear
}

# Start of main process

dir=`pwd`
prc=$1
savfile="-"
# 06/24/01 - Added test for $PFTEMP.
[ -z "$tmpdir" -a -n "$PFTEMP" ] && tmpdir=$PFTEMP
[ -z "$tmpdir" ] && tmpdir=/tmp
export tmpdir
# Set the directory that will hold saved copies of prc. tables.
SavDir=/tmp
file="-"
[ -f $dir/prc.$1 ] && {
    h=`dirname $dir`
    f=`basename $h`
    file=`basename $dir`
    PFDATA=
    PFDIR=`dirname $h`
    export PFDATA PFDIR
    os="unix"
}

[ "$os" = "" ] && {
    [ -f $dir/$1.prc ] && {
        h=`dirname $dir`
        f=`basename $h`
        file=`basename $dir`
        PFDATA=
        PFDIR=`dirname $h`
        export PFDATA PFDIR
        os="dos"
        }
    }

progpf=`sed -n "1 p" /etc/default/fppath`    # Use fppath instead.
datapf=`sed -n "2 p" /etc/default/fppath`    # Use fppath instead.
dirpf=`sed -n "3 p" /etc/default/fppath`    # Use fppath instead.

[ $PFDIR ] || {
    wd=`pwd`
    [ -f map ] && {
        PFDIR=`dirname $wd`
        PFDIR=`dirname $PFDIR`
        PFDATA=""
        export PFDATA PFDIR
        }
    [ `basename $wd` = filepro ] && {
        PFDIR=`dirname $wd`
        PFDATA=""
        export PFDATA PFDIR
        }
    }

until [ -d $PFPROG/fp -a -n "$PFPROG" ]
do
    $ECHO
    $ECHO "			PFPROG must be set."
    $ECHO -n "	Enter the location of the ./fp directory. [$progpf] "
    read PFPROG
    [ $PFPROG ] || PFPROG=$progpf
done
export PFPROG

until [ -d $PFDATA$PFDIR/filepro -a -n "$PFDIR" ]
do
    $ECHO
    $ECHO "			PFDIR must be set. "
    $ECHO -n "	Enter the location of the ./filepro directory. [$dirpf] "
    read PFDIR
    PFDATA=""
    [ $PFDIR ] || {
        PFDATA=$datapf
        PFDIR=$dirpf
        export PFDATA PFDIR
        }
done

[ "$os" = "" ] && {
    find $PFDATA$PFDIR/filepro/*/prc.* 2>/dev/null > $tmpdir/tst
    [ -n "$tmpdir/$tst" ] && os="unix"
    find $PFDATA$PFDIR/filepro/*/*.prc 2>/dev/null > $tmpdir/tst
    if [ -s "$tmpdir/$tst" -a "$os" = "" ]
    then
        os="dos"
    elif [ -n "$tst" -a "$os" != "" ] 
    then
        until [ $os ] 
        do
            $ECHO 
            $ECHO "There are both unix and dos type process tables."
            $ECHO -n "Which mode should be used? (unix/dos) "
            read os
            [ "$os" != "unix" -a "$os" != "dos" ] && {
                $ECHO
                $ECHO "Please enter 'unix' or 'dos' "
                os=""
                }
        done
    fi
    }

# If in filepro file directory and filename doesn't exist create it.
[ -n "$prc" -a -f map ] && {
    [ "$os" = "unix" -a ! -f "prc.$prc" ] && {
        touch prc.$prc
        chown filepro prc.$prc
        chmod 600 prc.$prc
        }
    [ "$os" = "dos" -a ! -f "$prc.prc" ] && {
        touch $prc.prc
        chown filepro $prc.prc
        chmod 600 $prc.prc
        }
    }

[ "$os" = "unix" ] && ls prc.* 2>/dev/null > $tmpdir/list
[ "$os" = "dos" ] && ls *.prc 2>/dev/null > $tmpdir/list
[ -s "$tmpdir/$list" ] || whichfile

until [ "$file" = "x" ]
do
[ "$os" = "unix" ] && {
    type="prc\."
    typefil="prc.$prc"
    }
[ "$os" = "dos" ] && {
    type="\.prc"
    typefil="$prc.prc"
    }
[ -n "$list" ] || whichfile
until [ -f "$typefil" ]
do
    [ "$file" = "-" ] && {
        file=$savfile
        whichfile
        }
    clear
    ls  prc.* > $tmpdir/prc.tst 2>/dev/null
    ls  *.prc > $tmpdir/tst.prc 2>/dev/null
    if [ -f "$typefil" ]
    then
        continue
    elif [ -s "$tmpdir/prc.tst" -o -s "$tmpdir/tst.prc" ]
    then
        [ "$os" = "unix" ] && {
            for j in `$ECHO prc.$prc*`
            do
                j1=`$ECHO $j | sed -e "s/prc\.//"`
                printf "%-16.16s" $j1
            done
            }
        [ "$os" = "dos" ] && {
            for j in `$ECHO $prc*.prc`
            do
                j1=`$ECHO $j | sed -e "s/\.prc//"`
                printf "%-16.16s" $j1
            done
            }
        $ECHO
# Inhibit <Enter> for edit last file again.  It created a problem when I wanted
# <Enter> to redisplay all prc.'s 
prc_last=""
        if [ $prc_last ] 
        then
            $ECHO -n " - $file - Edit which table? (h=help - <..>=Back - <Enter>='$prc_last'): "
        else
            $ECHO -n " - $file - Edit which table? (h=help - x=exit - <..>=Back): "
        fi
        savfunc=$prc
        read prc
        func=$prc
        check_func
        prc=$func
        savprc=$savfunc
#        chgedt=`$ECHO $prc | grep editor=`
#        [ $chgedt ] && {
#            prc=$savprc
#            editr=`$ECHO $chgedt | sed -e "s,editor=,,"`
#            prog=`$ECHO $chgedt | sed -e "s,PFPROG=,,"`
#            [ -d "$prog" ] && {
#                $ECHO 
#                $ECHO "Changing PFPROG to "$prog
#                PFPROG=$prog
#                export PFPROG
#                $ECHO
#                $ECHO -n "Press Enter to continue. "
#                read key
#                }
#            savdir=`pwd`
#            hash -r
#            cd /
#            is_cmd=`type $editr 2>&1 | sed -e "s,$editr is ,,"`
#            tis_cmd=`$ECHO $is_cmd | grep '/'`
#            cd $savdir
#            if [ -n "$tis_cmd" -o "$editr" = "dclerk" -o "$editr" = "dreport" ]
#            then
#                $ECHO
#                $ECHO "Changing editor to "$editr
#                $ECHO
#                editrsv=$editor
#                editor=$editr
#                $ECHO -n "Press Enter to continue. "
#                read key
#            else
#                $ECHO
#                $ECHO "$editr not found.  Using 'vi'"
#                $ECHO
#                $ECHO -n "Press Enter to continue. " 
#                read key
#                editrsv=vi
#                editor=vi
#            fi
#            }
#        chgprog=`$ECHO $prc | grep PFPROG=`
#        [ $chgprog ] && {
#            prog=`$ECHO $prc | sed -e "s,PFPROG=,,"`
#            [ -d "$prog" ] && {
#                $ECHO 
#                $ECHO "Changing PFPROG to "$prog
#                PFPROG=$prog
#                export PFPROG
#                $ECHO
#                $ECHO -n "Press Enter to continue. "
#                read key
#                }
#            }
#        chgqual=`$ECHO $file | grep PFQUAL=`
#        [ $chgqual ] && {
#            qual=`$ECHO $file | sed -e "s,PFQUAL=,,"`
#            if [ $qual ] 
#            then
#                qual="-m $qual"
#            else
#                qual=""
#            fi
#            $ECHO 
#            $ECHO "Changing PFQUAL to "$qual
#            PFQUAL=$qual
#            export PFQUAL
#            $ECHO
#            $ECHO -n "Press Enter to continue. "
#            read key
#            }
#        dirp=`$ECHO $prc | grep \/`
#        [ -n "$dirp" ] && {
#            prc=$savprc
#            [ -d $dirp/filepro ] && {
#                PFDATA=""
#                PFDIR=$dirp
#                export PFDATA PFDIR
#                pf=$PFDIR/filepro
#                cd $pf
#                [ -f $dirp/fp/lib/errmsg ] && {
#                    $ECHO
#                    $ECHO -n "Want to change PFPROG to "$dirp"? [N] "
#                    read yesno
                     [ $yesno ] || yesno="n"
#                    yesno=`$ECHO $yesno | sed -e "y/YN/yn/"`
#                    [ "$yesno" = "y" ] && {
#                        PFPROG=$dirp
#                        export PFPROG
#                        }
#                    }
#                }
#            }
##        prc=`$ECHO prc.$prc | sed -e "s/ .*//"`
        [ -z "$prc" -a -n "$prc_last" ] && prc=$prc_last 
        [ "$os" = "unix" ] && typefil="prc.$prc"
        [ "$os" = "dos" ] && typefil="$prc.prc"
        [ "$prc" = "x" ] && exit
        [ "$prc" = '..' ] && {   # -o -z "$prc" 
    	    file="-"
    	    prc=""
            prc_last=""
    	    }
        [ "$prc" = "h" ] && {
    	    help
    	    prc=$savprc
    	    }
        meta=`$ECHO $prc | sed -n "/\[.*/p"`
        meta1=`$ECHO $prc | sed -n "/.../p"`
        if [ ! -f "prc.$prc" -a -n "$prc" -a -z "$meta" -a -n "$meta1" ]
        then
            $ECHO
            $ECHO -n "Want to create $prc? [N] "
            read yesno
            [ $yesno ] || yesno="n"
            yesno=`$ECHO $yesno | sed -e "y/YN/yn/"`
            if [ "$yesno" = "y" ]
            then
                > prc.$prc
                chown filepro prc.$prc
            else
                prc=$savprc
            fi
        fi
    else
        $ECHO
        $ECHO "There are no prc tables in $file"
        $ECHO
        $ECHO -n "Press Enter to continue: "
        read key
        file=$savfile    # "-"
    fi
done

[ $editor ] || editor=vi; editrsv=vi
err="1"
until [ $err = 0 ]
do
  [ "$os" = "unix" ] && {
    PFNAME=$file
    trap 'trap 2 3; break ' 2 3
    $instdir/ASCIIfy $file $prc
    trap 2 3
    ENCODED=`cat $tmpdir/ENCODED`
    if [ "$editor" = "dclerk" -o "$editor" = "dreport" ] 
    then
      trap 2
      $PFPROG/fp/$editor $file -z $prc -db
      $ECHO
      $ECHO "Switching back to $editrsv."
      read key
      editor=$editrsv
    else
      [ "$ENCODED" != 3 ] && {
        trap "" 2
        [ "$editor" = "vi" ] && {
          savdt=`date +%y%m%d%H%M`
          SavBackup=""
          [ -f prc.$prc ] && {
            # 06/25/01 - See if previous backup is different.
            tst=`diff prc.$prc $SavDir"/"$file"."$prc`
            [ -n "$tst" ] && {
              # If so save it.
              SavBackup="\nPrevious backup saved in "$SavDir"/"$file"."$prc"_"$savdt
              mv $SavDir"/"$file"."$prc $SavDir"/"$file"."$prc"_"$savdt
              }
            # and make new backup.
            cp -p prc.$prc $SavDir"/"$file"."$prc
          }
        $editor prc.$prc
        # Check to see if a change was made.
        [ -n "$SavBackup" ] && $ECHO $SavBackup
        tst=`diff prc.$prc $SavDir"/"$file"."$prc`
        [ -n "$tst" ] && {
          $ECHO "\n   Original backed up in "$SavDir"/"$file"."$prc
          }
        }
      }
    fi
    # Call Bob Stockler's ck.syn routine to check for common syntax
    # errors caused by using 'vi'.
    ck.syn $prc
    err=$?
    if [ $err -gt 0 ]
    then
      # If prc was encoded but a 'vi' error is put into it it may lose
      # lines if reencoded, so set ENCODED=4 in that case.
      [ "$ENCODED" = 1 ] && ENCODED=4
      $ECHO -n "Want to reedit "$prc"? [Y] "
      read yesno
      [ $yesno ] || yesno="y"
      yesno=`echo $yesno | sed -e "y/YN/yn/"`
      [ "$yesno" = "n" ] && err=0
      [ "$yesno" = "y" ] && ENCODED=5
    else
      # Now check to see if encoded prc had 'vi' error fixed.
      [ "$ENCODED" = 4 ] && ENCODED=0
      $ECHO 
      $ECHO -n "	Press Enter to continue "
      read key
    fi
    file=$savfile
    [ "$ENCODED" = 1 ] && RE_ENCODE
    # Give user one last chance to reedit so prc. can be re-encoded.
    [ "$ENCODED" = 4 -a "yesno" != "y" ] && {
      $ECHO "\nprc.$prc was encoded but now has a 'vi' syntax error that"
      $ECHO "can't be re-encoded.\n\nAre you sure you want to do this? [N] \c"
      read reenc
      [ $reenc ] || reenc="n"
      reenc=`echo $reenc | sed -e "y/YN/yn/"`
      [ "$reenc" != "y" ] && err=1
      }
    }
  [ "$os" = "dos" ] && {
    [ "$ENCODED" != 3 ] && {
      trap "" 2
      [ "$editor" = "vi" ] && {
        savdt=`date +%y%m%d%H%M`
        [ -f $prc.prc ] && cp -p $prc.prc $SavDir"/sav."$prc"_"$savdt
        }
      $editor $prc.prc
      [ -f $SavDir"/sav."$prc"_"$savdt ] && {
        $ECHO "\nOriginal saved in "$SavDir"/sav."$prc"_"$savdt
        }
      }
    file=$savfile
    }
  if [ -s prc.$prc ]
  then
    dir=`pwd`
    file=`basename $dir`
    if [ "$ENCODED" != 3 ]
    then
      :
    else
      err=0
    fi
    prc_last=$prc
  else
    dir=`pwd`
    file=`basename $dir`
    rm -f prc.$prc
    prc=$savprc
    prc_last=""
    err=0
  fi
done
prc=$savprc
done
