#! /usr/bin/ksh

# Original Author: Tim Mooney <mooney@golem.phys.ndsu.NoDak.edu>
# $Id: irix6.req,v 1.7 2001/09/15 13:49:11 jbj Exp $
#
# This file is distributed under the terms of the GNU Public License
#
# find-requires is part of RPM, the Red Hat Package Manager.  find-requires
# reads a list of full pathnames (in a package) on stdin, and outputs all
# shared libraries the package requires to execute.
#
# NOTE: IRIX libraries (even system libraries) have "version information"
# in both the soname and the internal version field, so it's important to
# be able to separate the soname and internal version fields.  As has
# become the case on other platforms, the soname/iversion delimiters have
# become the `(' and `)' characters.
#
# On IRIX, use `elfdump -Dl' to find what libraries are required by
# an executable.  `elfdump -L' does what we need too, but it gives us more
# than we really need.
#
# Example `elfdump -Dl' output:
#
#$elfdump -Dl /usr/bin/X11/xterm
#
#
#
#/usr/bin/X11/xterm:
#
#		   **** MIPS LIBLIST INFORMATION ****
#.liblist :
#[INDEX]	Timestamp	        Checksum        Flags   Name            Version
#[1]	Nov 23 15:39:02 1997	0x4da65893	-----	libXaw.so.2	sgi2.0
#[2]	Nov 23 15:39:02 1997	0x414eece6	-----	libXmu.so	sgi1.0
#[3]	Nov 23 15:39:02 1997	0x6f314e69	-----	libXt.so	sgi1.0
#[4]	Nov 23 15:39:02 1997	0xcbe81fff	-----	libXext.so	sgi1.0
#[5]	Nov 23 15:39:02 1997	0x89ae8e98	-----	libX11.so.1	sgi1.0
#[6]	Oct 27 01:00:29 1997	0x99b27890	-----	libcurses.so	sgi1.0
#[7]	Jun 16 18:23:15 1997	0x92321a0c	-----	libc.so.1	sgi1.0
#
 
#
# TVM: it might be better to re-write this so that `file' isn't used, since
# it can all be done with `elfdump',  but this works.
#

PATH=/usr/bin:/usr/sbin
export PATH

#
# TVM: Marc Stephenson (marc@austin.ibm.com) points out we run things
# like `file', et. al. and expect the output to be what we see in the
# C/POSIX locale.  Make sure it is so.
#
LANG=C
export LANG

#
# TVM: switch to using `while read ...' instead of `for f in ...', because
# packages with a large number of files could be too big for one shell
# variable to hold.
#
IFS=""
while read f
do

	#
	# Uncomment the next line for additional debugging:
	#echo "read ->$f<-"
	
	#
	# Only run file once per file:
	#
	file_output=`file $f`

	#
	# Handle scripts first
	#
	is_shell_script=`echo "$file_output" | grep 'script text' | \
		cut -d: -f 2 | awk '{ print $1 }'`
	
	#
	# If it's a script...
	#
	if test X"$is_shell_script" != X ; then
		echo "$is_shell_script"
		#
		# use `continue' to skip back up to the top of the loop.  We've
		# already done as much as we need to, and this saves me from having
		# to have an else, and another indent level... ;-)
		#
		continue
	fi

	#
	# the `else' is implied here, since we used `continue' in the test above
	#

	#
	# It might be a shared library.
	#
	maybe_shared_lib=`echo "$file_output" | grep -E 'executable|lib'`
	if test X"$maybe_shared_lib" != X ; then

		elfdump -Dl $f 2>/dev/null \
		| awk '

		#
		# Since this entire awk script is enclosed in single quotes,
		# you need to be careful to not use single quotes, even in awk
		# comments, if you modify this script.
		#

		BEGIN { 
			found_column_headers = 0;
			FS = " ";
			RS = "\n";
			OFS="";
		}

		# uncomment the next line for debugging information
		#{ print "Saw input:", $0 }

		found_column_headers == 1 && $0 !~ /^$/ {

			# get the library name (field 15) and the library version (field 16)
			# if present.
			numfields = split($0,fields)
			if (numfields == 8) {
				print fields[8]
			} else if (numfields == 9) {
				#
				print fields[8], "(", fields[9], ")"
			} else if (numfields > 9) {
				#
				# SGI has this annoying habit of putting comments, complete
				# with whitespace, in their library IVERSION field.  Yuck.
				#
				# Handle libraries like this gracefully.
				#
				verfields = split(fields[NF], junk, "#")
				if (verfields == 2) {
					print fields[8], "(", junk[2], ")"
				} else if (verfields > 2) {
					print fields[8], "(", junk[verfields], ")"
				} else {
					print "Cannot find version:", fields[numfields] | "cat 2>&1"
				}
			}
		}

		/^\[INDEX\].Timestamp.*Checksum.*Flags.*Name.*Version$/ {
			# we better start paying attention now.
			found_column_headers = 1
			#
			# uncomment the next line for debugging information
			#print "found the column headers: ", $0
		}

	' # end of awk
	fi
done | sort -u
# comment out the previous line and uncomment the next when debugging
#done
