#
# Usage:	actualp
#
# abstract:	This Bourne Shell script returns the actual path for
#		a file in variable Filepath. Only a real existing file
#		returns a non-null value.
#
# note(s):      1. This routine must be called using a . (period)
#		2. The global variable 'File' is the initial filepath.
#
# Copyright (c) 1992-1998 The MathWorks, Inc. All Rights Reserved.
# $Revision: 1.4 $  $Date: 1999/01/11 19:24:38 $
#------------------------------------------------------------------------
#
    Filepath=
#
    lsCmd=`ls -ld $File 2>/dev/null`
    if [ "$lsCmd" ]; then
#
# Check for link portably
#
	if [ `expr "$lsCmd" : '.*->.*'` -eq 0 ]; then
    	    if [ ! -d $File ]; then
	        Filepath=$File
            fi
        else
#
# A directory link?
#
            (cd $File) > /dev/null 2>&1
            if [ $? -eq 0 ]; then
	        :
            else
#
# Now it is either a file, link to a file, or still a bad path.
#
	        cpath=`/bin/pwd`
                localFile=$File
#
# Follow up to 8 links before giving up. Same as BSD 4.3
#
	        n=1
    	        while [ $n -le 8 ]
    	        do
#
# Get directory correctly!
#
   	            newDir=`echo "$localFile" | awk '
                        { tail = $0
                          np = index (tail, "/")
                          while ( np != 0 ) {
                             tail = substr (tail, np + 1, length (tail) - np)
                             if (tail == "" ) break
                             np = index (tail, "/")
                          }
                          head = substr ($0, 1, length ($0) - length (tail))
                          if ( tail == "." || tail == "..")
                             print $0
                          else
                             print head
                        }'`
	            if [ ! "$newDir" ]; then
	                newDir="."
	            fi
	            (cd $newDir) > /dev/null 2>&1
	            if [ $? -ne 0 ]; then
		        break
	            fi
	            cd $newDir
	            newDir=`/bin/pwd`
	            newBase=`expr //$localFile : '.*/\(.*\)' \| $localFile`
                    lsCmd=`ls -l $newBase 2>/dev/null`
	            if [ ! "$lsCmd" ]; then
		        break
	            fi
#
# Check for link portably
#
	            if [ `expr "$lsCmd" : '.*->.*'` -ne 0 ]; then
	                localFile=`echo "$lsCmd" | awk '{ print $NF }'`
	            else
#
# It's a file
#
	                Filepath=$newDir/$newBase
	            fi
		    n=`expr $n + 1`
    	        done
    	        cd $cpath
	    fi
	fi
    fi
