#!/bin/sh
# viewprt - Written by Jerry M. Rains 06/25/00
#           Enhanced 11/04/00 for Installation Utilities Menu.
#           02/28/01 - Cleaned up operation.
#
# 'viewprt' was written to be used with the filePro PFPOSTPRINT environment
# variable.  It displays a file using a pager.  It will use 'less -S' if 
# possible.  'less -S' has horizontal scrolling capability so your wide 
# reports won't wrap.  If less is not found and PAGER is defined it will use 
# $PAGER.  Otherwise 'more' will be used.  It can be used from a menu or the
# command line.
# 
# For the Installation Utilities Menu I enhanced 'viewprt' so that if a 
# filename is not supplied it will display the '/' directory using 'ls -xdF'
# which will mark directories with a trailing '/', executables with a
# trailing '*', symbolic links with a trailing '@' and pipes with a trailing '|'
# If a directory name is supplied it will display that directory as above.
# 
# Entering the name of a directory will move to that directory.  Entering 
# <..> will move up one directory.  Entering <x> or <q> will exit the pager and
# you will be asked if you want to print the file.  Enter <y> or <Y> to print,
# <n> or <N> or <Enter> to exit.
#
# 04/12/01 - Added test for LPDEST being set and allow lp only if it is.

# 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

# Which directory is this?
this_dir=`pwd`

viewpt() {
    clear
    # Determine which PAGER to use.
    cd /
    hash -r
    is_less=`type less 2>&1 | sed -e "s,less is ,,"`
    tis_less=`echo $is_less | grep '/'`
    if [ -n "$tis_less" ]
    then {
        lesS=$is_less" -S "$dir/$file
        $lesS 
        }
    elif [ $PAGER ]
    then {
        $PAGER $dir/$file
        }
    else {
        more $dir/$file
        }
    fi
    err=$?
    if [ $err = 0 ]
    then
      until [ "$print" = "y" -o "$print" = "n" ]
      do
        $ECHO "\nDo you want to print $1? [y/N] \c"
        read print
        $ECHO 
        [ $print ] || print="n"
        print=`echo $print | sed -e "y/YN/yn/"`
        [ "$print" = "y" ] && {
            if [ -z "$LPDEST" ]
            then
                echo "LPDEST is not set.  It must be set to the default printer. "
                echo "Press Enter to continue. \c"
                read key
            else
                lp $dir/$file
            fi
            # If error Beep then stop in case called from a menu.
            [ $? = 0 ] || {
                $ECHO \\007
                $ECHO "Press Enter to continue. \c"
                read key
                }
            }
      done
    fi
    file=""
    print=""
    }
# End of viewpt()

file=$1
# If called with filename just view/print.
if [ -f "$file" ]
then
    # If a relative address this will make it absolute.
    dir=`dirname $file | sed -e "s,^\.,$this_dir,"`
    # If ./ was not placed on front of address this will make it absolute.
    [ `echo $dir | sed -e "s,\(.\)\(.*\),\1,"` = "/" ] || dir=$this_dir/$dir
    file=`basename $file`
    viewpt $file
    exit
# Else display directory.  / is default.
else
    # See if argument is a directory.  If so put $file in $dir.
    [ -d "$file" ] && {
        dir=$file
        file=""
        }
    until [ "$file" = "x" ]
    do
        if [ $file ]
        then
        # If $file isn't empty then split it.
            dir=`dirname $file`
            file=`basename $file`
        else
        # If $file is empty check for $dir being empty and set to / if so.
            [ $dir ] || dir=/
        fi
        # Until a good filename is entered browse the directories.
        until [ -f "$dir/$file" ]
        do
            $ECHO "directory = $dir\n"
            cd $dir
            # Display the directory using following suffixes:
            # / for directories.
            # * for executable files.
            # @ for symbolic links.
            # | for named pipes and FIFO's.
            clear
            ls -dxF $file*
            $ECHO "\ndirectory = $dir - View which file? (x to exit) \c"
            read file
            [ "$file" = "x" -o "$file" = "q" ] && exit
            # If $file is not "/" then strip the /
            [ "$file" != "/" ] && file=`echo $file | sed -e "s,^/,,"`
            # If $file is ".." then backup one directory.
            [ "$file" = '..' ] && {
                file=""
                dir=`dirname $dir`
                continue
                }
            # If $file is "/" then dir has to be null.
            [ "$file" = "/" ] && dir=""
            # If $file is a directory then move to that directory.
            [ -d $dir/$file -a ! -f $dir/$file ] && {
                if [ "$file" = "" ]
                then
                    : nothing
                elif [ "$dir" = "/" ]
                then
                    dir=/$file
                    file=""
                else
                    dir=$dir/$file
                    file=""
                fi
                [ "$dir" = "//" ] && dir=/
                continue
                }
            # In case $file is still a directory here force it into $dir.
            [ -d $file -a ! -f $file ] && {
                dir=$file
                file=""
                continue
                }
            # This keeps from having // when $file includes the path.
            [ ! -d "$file" -a -f "$file" -a "$dir" = "/" ] && {
                dir=`dirname $file`
                file=`basename $file`
                continue
                } 
            $ECHO
        done
        viewpt
        $ECHO
    done
fi
