#!/bin/sh
# $Id: classpath,v 1.1 1998/09/21 17:46:32 tom Exp $
#
# Title:	classpath
# Author:	T.E.Dickey
# Created:	06 Nov 1997
#
# Function:  Displays information about the user's CLASSPATH environment
#		variable (which controls linking and loading of Java classes.
#
#		If no arguments given, this script displays the names of the
#		directories in the class-path.  If argument is given, it is assumed to
#		be either a class name (e.g., "String"), or a jar-file.
#
#		All matching names are displayed; the first is the one that will be
#		loaded.
#
#		Options (arguments beginning with "-") are passed to 'ls'.
#		

IFS="${IFS}:"
opts=""
name=""
temp=/tmp/class$$
TEST=/tmp/CLASS$$

for N in $*
do
	case $N in
	-*)
		opts="$opts $N"
		;;
	*)
		name=$N
		J=`basename $N .jar`
		C=`basename $N .class`
		for P in $CLASSPATH
		do
			if [ -d $P ]
			then
				case $name in #(vi
				*.jar|*.class) #(vi
					test -f $P/$name && ls $opts $P/$name
					;;
				*)
					test -f $P/$name.class && ls $opts $P/$name.class
					;;
				esac
			elif [ -f $P ]
			then
				case $P in #(vi
				*.jar) #(vi
					case $name in #(vi
					*.jar) #(vi
						;;
					*)
						if ( unzip -l $P 2>/dev/null |sed -e 's/^.* //' >$temp )
						then
							case $name in #(vi
							*.class) #(vi
								egrep "^$name$" $temp >$TEST
								egrep "/$name$" $temp >>$TEST
								;;
							*)
								egrep "^$name.class$" $temp >$TEST
								egrep "/$name.class$" $temp >>$TEST
								;;
							esac
							test -s $TEST && echo $P'('`cat $TEST`')'
						fi
						rm -f $temp $TEST
						;;
					esac
					;;
				*)
					if [ `basename $P` = $name ]
					then
						ls $opts $P
					fi
					;;
				esac
			fi
		done
	esac
done
#
if [ -z "$name" ]
then
	for P in $CLASSPATH
	do
		ls -d$opts $P
	done
fi
