#! /bin/sh
#
#  Name:
#
#     ldd	show shared libraries used by a program.
#     
#  Usage:
#
#     ldd       [-h|-help] | -man | [options] program [ . . . ]
#
#  Description:
#
#     This Bourne shell script calls the appropriate platform
#     specific command to search a program for shared library
#     information. You can also view the manual page for the
#     command used.
#
#  Options:
#
#     -h | -help 	- Help. Print usage.
#
#     -man              - View manual page for command used.
#
#     options		- Platform specific options. See the
#			  the manual page for details.
#
#     program		- Program to search.
#
# Note(s):	1. Commands with options to use:
#
#		   sol2:        /usr/bin/ldd
#		   hp700:	/usr/bin//chatr
#		   alpha:	/usr/bin/odump -Dl
#		   ibm_rs:      /usr/bin/dump -H
#		   sgi: 	/usr/bin/elfdump -Dl	
#		   sgi64: 	/usr/bin/elfdump -Dl	
#		   lnx86:	/usr/bin/ldd
#		   
# Copyright (c) 1984-1998 by The MathWorks, Inc.
# All Rights Reserved.
# $Revision: 1.5 $  $Date: 1998/09/18 18:07:46 $
#-----------------------------------------------------------------------
#23456789012345678901234567890123456789012345678901234567890123456789012
#
    trap "exit 1" 1 2 3 15
#
    stat="OK"
    msg=""
#
# Command and Options
#
#========================= arch.sh (start) ============================
#!/bin/sh
#
# usage:        arch.sh
#
# abstract:     This Bourne Shell script determines the architecture
#		of the the current machine.
#
#		ARCH	  - Machine architecture
#
# note(s):	1. This routine must be called using a . (period)
#
# Copyright (c) 1986-1997 by The MathWorks, Inc.
# $Revision: 1.8 $  $Date: 1997/11/06 23:02:48 $
#----------------------------------------------------------------------------
#
#=======================================================================
# Functions:
#=======================================================================
	matlab5_arch () { # Determine the architecture for MATLAB 5
			  # and outputs it to standard output
                          #
                          # Always returns a 0 status.
                    	  #
                    	  # usage: matlab5_arch
                    	  #
	Arch="unknown"
#
	if [ -f /bin/uname ]; then
   	    case "`/bin/uname`" in
	        SunOS)					# sun4 and sol2
	            if [ -d /dev/pts ]; then
		        Arch="sol2"
	            else
		        Arch="sun4"
	            fi
	            ;;
	        HP-UX)					# hp700, hp800
	            if [ -f /bin/hp9000s700 ]; then
		        (/bin/hp9000s700) > /dev/null 2>&1
		        if [ $? -eq 0 ]; then
	    	            Arch="hp700"
		        fi
	            fi
	            if [ -f /bin/hp9000s800 ]; then
		        (/bin/hp9000s800) > /dev/null 2>&1
		        if [ $? -eq 0 ]; then
	    	            Arch="hp700"
		        fi
	            fi
	            ;;
	        IRIX)					# sgi
    	            if [ -f /bin/4d ]; then
		        (/bin/4d) > /dev/null 2>&1
		        if [ $? -eq 0 ]; then
	    	            Arch="sgi"
		        fi
	            fi
	            ;;
	        IRIX64)		# Want this to mean MIPS 4/64 bit OS sgi
    	            if [ -f /bin/4d ]; then
		        (/bin/4d) > /dev/null 2>&1
		        if [ $? -eq 0 ]; then
	    	            Arch="sgi64"
		        fi
	            fi
#
# Watch out! Some machines are not MIPS 4 (R4400)
#
		    if [ -f /bin/hinv ]; then
			cpu=`/bin/hinv | awk '
#-----------------------------------------------------------------------
	$1 == "CPU:" { print substr($3,1,2) }'`
#-----------------------------------------------------------------------
			if [ "$cpu" = "R4" ]; then
			    Arch=sgi
			fi
		    fi
	            ;;
	        OSF1)					# alpha
	    	    Arch="alpha"
	            ;;
	        AIX*)					# ibm_rs
#
# With AIX 4.1, uname can return more than just 'AIX'
#
	            Arch="ibm_rs"
	            ;;
	        Linux)
		    Arch="lnx86"
		    ;;
	        *)
		    :
	            ;;
	    esac
	elif [ -f /bin/arch ]; then				# early sun4
	    Arch="`/bin/arch`"
	fi
#
        echo $Arch
#
	return 0
    }
#
    if [ "$ARCH_CHECK" = "" -a "$ARCH_LIST" != "" ]; then
#
# Check input arguments $1, ..., $$# for -ARCH
#
	i=1
	while [ $i -le $# ]
	do
	    value=`eval echo '$'"$i"`
	    ARCH=`echo "$ARCH_LIST EOF $value" | awk '
#-----------------------------------------------------------------------
	{ for (i = 1; i <= NF; i = i + 1)
	      if ($i == "EOF") {
		  narch = i - 1
		  if (NF != i + 1) exit
	          break
	      }
	  for (i = 1; i <= narch; i = i + 1)
		if ("-" $i == $NF) {
		    print $i
		    exit
		}
	}'`
#-----------------------------------------------------------------------
	    if [ "$ARCH" != "" ]; then
		break
	    fi
	    i=`expr $i + 1`
	done
    fi
#
    if [ "$ARCH" = "" ]; then
	ARCH=`matlab5_arch`
    fi
    Arch=$ARCH
#========================= arch.sh (end) ==============================
    case "$ARCH" in
	hp700)
	    cmd=/usr/bin/chatr
	    mancmd=chatr
	    options=
	    ;;
	sgi|sgi64)
	    cmd=/usr/bin/elfdump
	    mancmd=elfdump
	    options=-Dl	
	    ;;
	ibm_rs)
	    cmd=/usr/bin/dump
	    mancmd=dump
	    options=-H
	    ;;
	sol2)
	    cmd=/usr/bin/ldd
	    mancmd=ldd
	    options=
	    ;;
	alpha)
	    cmd=/usr/bin/odump
	    mancmd=odump
	    options=-Dl
	    ;;
	*)
	    if [ "`uname`" = "Linux" ]; then
		cmd=/usr/bin/ldd
		mancmd=ldd
		options=
	    else
		msg='fix this script for ARCH ($ARCH)'
		stat=""
	    fi
    esac
#
# Verify input
#
    help=0
    man=0
    if [ $# -eq 0 ]; then
	stat=""
    fi
    while [ "$stat" = "OK" -a  $# -gt 0 ]; do
	case "$1" in
	    -help)		# -help: Help option.
		stat=""
		help=1
		;;
	    -man)		# -man: Man pages.
		man=1
		stat=""
		;;
	    *)
		break
		;;
	esac
	shift
    done
#
    if [ "$stat" = "" ]; then
	if [ "$man" != "0" ]; then 
	   man $mancmd
	   exit 1
	fi
        if [ "$msg" != "" ]; then
#-----------------------------------------------------------------------
    echo " "
    echo "    Error: $msg"
    echo " "
#-----------------------------------------------------------------------
        fi
#------------------------------------------------------------------------------
    echo "-----------------------------------------------------------------"
    echo " "
    echo "    usage: ldd [-h|-help] | -man | [options] program [ . . . ]"
    echo " "
    echo "    -h|-help        - Print Usage."	
    echo "    -man            - View manual page for command used."
    echo "    options         - Platform specific options. See the"
    echo "                      manual page for details."
    echo "    program         - Program to search."
    echo " "
    echo "    DEFAULT: executes '$cmd $options [options] program [ . . . ]'"
    echo " "
    echo "    Show shared libraries used by a program. You can also"
    echo "    view the manual page for the command used."
    echo " "
    echo "-----------------------------------------------------------------"
#------------------------------------------------------------------------------
	exit 1
    fi
#
    $cmd $options $*
    exit $?
