showmap() {
clear
awk  -F: ' 
hc == 0 	{	
		lable="----------Fields for "file"----------"
		printf "%s%40.40s%s", "Number     ", lable, "     Len   --Type--"
		print ""
		hc=hc+1
	}
$1 == "Alien"	{
        getline
        printf "%s%-40.40s", "Alien File - Data File Name: ", $1
        print ""
        hc=hc+1
        continue
        }
NR > 1	{
	ct=ct+1
	printf "%4.4s%s%-40.40s%8.8s%s%-7.7s", ct, "   -   ",  $1, $2, "   ", $3
	print  ""
	hc=hc+1
		}
hc == 24 { hc=0 }
' file=$file $PFDATA$PFDIR/filepro/$file/map | $pager 
}

help() {
    clear
    $ECHO
    $ECHO "  Usage: cmap [-h | filename]"
    $ECHO
    $ECHO "  cmap displays filePro maps using the same format as used by ddefine."
    $ECHO "  However, it uses a pager so you can scroll and search."
    $ECHO
    $ECHO "  cmap can also be used from the command line."
    $ECHO "  You must either set PFDATA and PFDIR in the environment - or -"
    $ECHO "  run 'cmap' while sitting in a ./filepro directory."
    $ECHO "  Sitting in a ./filepro directory takes precidence over PFDATA/PFDIR"
    $ECHO
    $ECHO "  Pressing <Enter> by itself will produce a listing of all filePro files."
    $ECHO "  You may use wildcards (for example [a-n]) to limit the number of files listed."
    $ECHO
    $ECHO "  Enter 'q' to leave the map."
    $ECHO "  Enter 'x' or press 'break' from the 'Enter filename:' prompt to exit cmap."
    $ECHO
    }

mainlp() {
	while [ "$file" != "x" ] 
	do
	    until [ $file ] 
	    do
	        $ECHO "Enter filename: \c"
	        read file
	        [ "$file" = "x" ] && exit
	        clear
	        [ $file ] || {
                    for j in `echo $file*`
                    do
                        [ -s $j/map ] && printf "%-16.16s" $j
                    done
                    $ECHO
                    }
	    done
	    if [ -d $pf/$file -a $file ]
	    then
	        showmap
		file=""
	    else 
                for j in `echo $file*`
                do
                    [ -s $j/map ] && printf "%-16.16s" $j
                done
                $ECHO
		file=""
	    fi
	done
}

# ------ Main script ------

[ -z "$1" -o "$1" = "-h" ] && help

dir=`pwd`
[ `basename $dir` = "filepro" ] && {
    PFDIR=
    pf=
    }
[ "$PFDIR" = "" ] && {
    PFDATA=
    PFDIR=`dirname $dir`
    }

[ -d $PFDATA$PFDIR/filepro ] || help

# [ "$pf" = "" ] && pf=$PFDATA$PFDIR/filepro
pf=$PFDATA$PFDIR/filepro
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
$ECHO
$ECHO "cmap is running in "$PFDATA$PFDIR
$ECHO

hash -r
cd /
is_less=`type less 2>/dev/null | sed -e "s,less is ,,"`
tis_less=`echo $is_less | grep '/'`
if [ -n "$tis_less" ]
then 
    pager=$is_less" -S "
elif [ $PAGER ]
then
    pager=$PAGER
else 
    pager=more
fi
cd $pf

if [ $1 ] 
then
	file=$1
	    if [ -d $pf/$file ]
	    then
	        showmap
		file=""
		mainlp
	    else 
		$ECHO "$file is bad filename. \c"
		read key
		file=""
	    fi
else	{
	file=""
	mainlp
	}
fi

