#!/bin/sh
#
#       @(#)showrev.sh	1.2 12/5/90 SMI
#
# showrev - tells things about this system for user reporting a bug
#
# use absolute paths - the system/PATH may be hosed!
#

check_dir_to_dir_symlink()
{
thispath=$real_path
current_dir=`/usr/bin/dirname $real_path`
perm=`/usr/bin/ls -ld $real_path|(read perm nlink owner sz dm dd\
      dt dir pt link;/usr/bin/echo $perm)`
link=`/usr/bin/ls -ld $real_path|(read perm nlink owner sz dm dd\
      dt dir pt link;/usr/bin/echo $link)`
case $perm in
  l*) symlink=yes
      dir_to_dir_link=yes
      case $link in
        /*) real_path=$link;; 
         *) real_path=$current_dir/${link} # relative to current_dir
            case $real_path in
              //*) real_path=`/usr/bin/echo $real_path|/usr/bin/tr -s '/' '/'`;;
            esac;;
      esac 
      /usr/bin/echo "     \"$thispath\" Symbolic link to $real_path"
      keep_tracing_the_path=yes
      for p in $all_paths
      do
	if [ $real_path = "$p" ]; then
		keep_tracing_the_path=no
	fi
      done
      if [ $keep_tracing_the_path = "yes" ]; then
		check_dir_to_dir_symlink
      fi;;
  *) dir_to_dir_link=no
     check_file_to_file_symlink
esac
}

check_file_to_file_symlink()
{
thispath=$real_path
current_dir=$real_path
perm=`/usr/bin/ls -ld $real_path/$file|(read perm nlink owner sz dm dd\
      dt dir pt link;/usr/bin/echo $perm)`
link=`/usr/bin/ls -ld $real_path/$file|(read perm nlink owner sz dm dd\
      dt dir pt link;/usr/bin/echo $link)`
case $perm in
  l*) symlink=yes
      case $link in
        /*) real_path=`/usr/bin/dirname $link`
	    file=`/usr/bin/basename $link`
            check_dir_to_dir_symlink;;
         *) real_path=`/usr/bin/dirname $current_dir/$link` # relative to current_\dir
	    file=`/usr/bin/basename $link`
            case $real_path in
              //*) real_path=`/usr/bin/echo $real_path|/usr/bin/tr -s '/' '/'`;;
            esac;;
      esac
      /usr/bin/echo "     \"$thispath/$file\" Symbolic link to $link"
      check_dir_to_dir_symlink;;
   *) all_real_pathnames="${all_real_pathnames} ${real_path}/${file}";;
esac
}

findfile()
{
if [ -f $thispath/$file ]; then
	case $symlink in
		yes) ;;  # doesn't count found yet, need to follow links
		no) /usr/bin/echo
 		    /usr/bin/echo "* -- \"$file\" found in \"$thispath\" --";;
	esac
	real_path=$thispath
	check_dir_to_dir_symlink
	if [ -f $real_path/$file ]; then
		foundinthepath="yes"
	fi
fi
}

general_sys_rev_info()
{
hostname=`/sbin/hostname`
/usr/bin/echo "* Hostname: \"${hostname}\""
# check to see if set, check against /etc/hosts
# note: [ 	] contains a space and a tab
egrep "([ 	]${hostname}[ 	])|([ 	]${hostname}\$)" /etc/hosts > /dev/null
if [ $? -ne 0 ]; then
	/usr/bin/echo  "ERROR: the hostname doesn't match with /etc/hosts"
fi
/usr/bin/echo "* Hostid: `hostid`"
/usr/bin/echo "* Kernel Arch: \"`/usr/bin/arch -k`\""
/usr/bin/echo "* Application Arch: \"`/usr/bin/arch`\""
kline=`/usr/ucb/strings /vmunix | /usr/bin/grep OS/ | (read junk1 junk2 the_rest; /usr/bin/echo $the_rest)`
/usr/bin/echo "* Kernel Revision:"
/usr/bin/echo "  $kline"
if [ -f /usr/sys/conf.common/RELEASE ]; then
	/usr/bin/echo "* Release: `/usr/bin/cat /usr/sys/conf.common/RELEASE`"
elif [ -f /etc/install/release ]; then
	/usr/bin/echo "* Release: `/usr/bin/cat /etc/install/release`"
fi
}

print_info()
{
/usr/bin/echo "     a) Library information:"
/usr/bin/echo "`/usr/bin/ldd ${real_path_dir}/${real_path_file}`"
/usr/bin/echo "     b) Sccs Id:   `/usr/ucb/what ${real_path_dir}/${real_path_file}`"
/usr/bin/echo -n "     c) Permission: "
w1=`/usr/bin/ls -ldg ${real_path_dir}/${real_path_file} | (read w1 w2 w3 w4 junk; /usr/bin/echo $w1)`
w2=`/usr/bin/ls -ldg ${real_path_dir}/${real_path_file} | (read w1 w2 w3 w4 junk; /usr/bin/echo $w2)`
w3=`/usr/bin/ls -ldg ${real_path_dir}/${real_path_file} | (read w1 w2 w3 w4 junk; /usr/bin/echo $w3)`
w4=`/usr/bin/ls -ldg ${real_path_dir}/${real_path_file} | (read w1 w2 w3 w4 junk; /usr/bin/echo $w4)`
/usr/bin/echo " $w1 $w2 $w3 $w4"
/usr/bin/echo "     d) Sum: `/usr/bin/sum ${real_path_dir}/${real_path_file}`"
/usr/bin/echo
}

command_rev_info()
{
all_paths=`/usr/bin/echo $PATH | /usr/bin/tr ':' ' '`
found=no
found_no=0
file=$1
for thispath in $all_paths
do 
      foundinthepath=no
      symlink=no
      file=$1
      findfile 
      case $foundinthepath in
        yes) found=yes;;
         no) if test $symlink = yes
               then /usr/bin/echo "$file doesn't exist in point to path"
             fi;;
      esac
done
case $found in
	no) /usr/bin/echo " \"$file\" is not found";;
	yes) path_printed=""
	     for path in $all_real_pathnames
	     do
	     if [ "$path" != "$path_printed" ]; then
		/usr/bin/echo
		/usr/bin/echo "     $path"
		real_path_dir=`/usr/bin/dirname $path`
		real_path_file=`/usr/bin/basename $path`
		print_info
		path_printed=$path
	     fi
	     done;;
esac
/usr/bin/echo
if [ "`/usr/bin/echo $LD_LIBRARY_PATH`" = "" ]; then
	/usr/bin/echo "* LD_LIBRARY_PATH not set"
else	/usr/bin/echo "* LD_LIBRARY_PATH is: "
	/usr/bin/echo $LD_LIBRARY_PATH
fi
/usr/bin/echo
/usr/bin/echo "* Path is: "
/usr/bin/echo $PATH
}

patches_info()
{
/usr/bin/echo
/usr/bin/echo "* showrev of patches not implemented yet"
}

#===================== MAIN ======================
/usr/bin/echo
/usr/bin/echo "***************  showrev version 1.2  *****************"  
#
flag_set=0
cflag=0
pflag=0
#
# parse options
#while getopts pc: c
while getopts c: c
do
	case $c in
#	  p) pflag=1
#	     flag_set=1;;
	  c) cflag=1
	     flag_set=1
	     c_arg=$OPTARG;;
	 \?) # XXX may need to add more options
#	     /usr/bin/echo "usage: showrev [ -p ] [ [-c] command ]"
	     /usr/bin/echo "usage: showrev [ [-c] command ]"
	     exit 1
	esac
done
shift `expr $OPTIND - 1`
arg=$1
#
# no flags are set
if [ $flag_set -eq 0 ]; then
	if [ "$arg" = "" ]; then
		general_sys_rev_info
	else	command_rev_info $arg
	fi
fi
#
#
if [ $pflag -eq 1 ]; then
	patches_info
fi
if [ $cflag -eq 1 ]; then
	command_rev_info $c_arg
fi
/usr/bin/echo
/usr/bin/echo "*******************************************************"
