#!/bin/sh
BOURNE=/bin/sh
XTERMPATH=/usr/bin/X11
XTERMBIN=xterm
XTERMOPTS="-T 'SoftLoad 6.0 Console' -fn 9x15  -sl 200 -g 80x40+0+0 -bg \#e0e0e0 -fg \#800000 -sb -e ${BOURNE} softload_GUI"
sysPATH=/bin:/usr/bin:/usr/ucb:$XTERMPATH

jsr=''

FlInfo() {
    /bin/ls -ldL $*  |
	sed -e 's@^\(..........\)\([^ 	]\)@\1 \2@'	\
	    -e 's@^L@l@' -e 's@^D@d@' -e 's@^F@-@'
}
	
FlOwner() {
    $jsr FlInfo $* | awk '{print $3}'
}

FlGroup() {
    $jsr FlInfo $* | awk '{print $4}'
}

FlSize() {
    $jsr FlInfo $* | awk '{print $5}'
}

LinkName() {
    $jsr FlInfo $* | awk '{print $11}'
}

IsSymLink() {
    $jsr FlInfo $1  2>/dev/null | grep '^l' 1>/dev/null
    return $?
}


DirentExists() {		#non-referencing symLinks included
    test -d $1 -o -f $1 || $jsr IsSymLink $1
    return $?
}
	
FlsNewer() {
    than=`echo $1 | sed 's@[/.]@\\\\&@g'`	#stick in backslashes to escape
    {
	echo ""					#in case none newer
	/bin/ls -Ldtr $* 
    } | sed -e '1,/^'$than'$/d' -e '/^'$than'$/d'
}

FlsOlder() {
    than=`echo $1 | sed 's@[/.]@\\\\&@g'`	#stick in backslashes to escape
    {
	echo ""					#in case none older
	/bin/ls -Ldt $* 
    } | sed -e '1,/^'$than'$/d' -e '/^'$than'$/d'
}



FsFragSize() {
    echo 4096
}

TreeSizeK() {
    du -sk $1 | awk '{print $1}'
}


FlSizeK() {
    $jsr FlSize $1 | awk '{print int($1 / 1024 + 0.99) }'
}


Df() {					#reports info in Kbytes, hierarchically
    echo "\
Filesystem                          avail Kb  Mounted on"
    df="df -I"
    $df $* | sed -e 's@//@/@g' -e 's@\([^ 	]\)/$@\1@' | awk '\
	NR == 1  { next }
	NF == 1  { filesys = $1; next }		#broken into two lines
	{   if ( $0 ~ /^[^ 	].*/ ) filesys = $1
	    mntPt = $(NF)
	    avail = $(NF-2)
	    if ( avail < 0 ) avail = 0
	    printf( "%-35s %8d  %s\n", filesys, avail, mntPt )
	}' | sort -b +2
}


DfTree() {
    (
	if [ $# -eq 1 ]; then
	    cd $1 || return 1
	fi

        if [ `expr "$1" : "/afs*"` -gt 0 ]; then
                echo "\
Filesystem                          avail Kb  Mounted on"
           /usr/afsws/bin/fs lq . > /tmp/fslq$$
           /usr/afsws/bin/fs df . > /tmp/fsdf$$
           spaceOnPartion=`awk 'NR == 2,NR == 2 { print $4 }' /tmp/fsdf$$`
           quotaOnVolume=`awk 'NR == 2,NR == 2 { print $2 }' /tmp/fslq$$`
           usedOnVolume=`awk 'NR == 2,NR == 2 { print $3 }' /tmp/fslq$$`
           if [ $quotaOnVolume = "no" ]; then
              avail=$spaceOnPartion
           else
              availQuota=`expr $quotaOnVolume - $usedOnVolume`
              if [ $spaceOnPartion -gt $availQuota ]; then
                 avail=$availQuota
              else
                 avail=$spaceOnPartion
              fi
           fi
           rm /tmp/fslq$$ /tmp/fsdf$$
           echo "AFS       $avail     /afs"
        else
           $jsr Df | awk '\
           BEGIN   { here = "'`/bin/pwd`'" }       #requires real absolute path
           NR == 1 { print; next }
           {
             if ( here == "/" ) {
               	print                           #filesys is descendant of $here
             } else if ( $3 == "/" || \
                $3 "/" == substr( here , 1, length($3)+1 ) ) {
               	pending = $0                    #filesys is ancestor of $here
             } else if ( $3 == here ) {
               	pending = ""
               	print                           #filesys is $here
             } else if ( substr( $3, 1, length(here)+1 ) == here "/" ) {
              	if ( pending != "" ) print pending
               	pending = ""
               	print                           #filesys is descendant of $here
             }
           }
       	   END {
               if ( pending != "" ) print pending
           }'
         fi

    )
}


DkAvail() {
    ( cd $1 && $jsr Df . ) | awk 'NR == 2 {print $2}'
}


Dirname() {					#print argument minus leaf, or .
    case $1 in
	*/* )	echo $1 | sed 's@/[^/]*$@/@' ;;
	*)	echo "." ;;
    esac
}

Leafname() {					#print leaf
    case $1 in
	*/* )	echo $1 | sed 's@.*/@@' ;;
	*)	echo $1 ;;
    esac
}

Fullpath() {					#print rooted path to arg dir
    ( cd $1 && /bin/pwd |
	sed 's@^/tmp_mnt/@/@' )			#print automount access dir
}

Dirpath() {					#print rooted path to arg - leaf
    $jsr Fullpath `$jsr Dirname $1`
}

Suffix() {
    echo $1 | sed -n 's@.*\.@@p'
}

Prefix() {
    echo $1 | sed 's@\.[^.]*$@@'
}

OSvendor=ibmrs
OSarchs=$OSvendor
Proot=`$jsr Fullpath ../..` ; export Proot

echo `$jsr DfTree $Proot | sed '1d' | awk ' {print $2} '` > $1 
