#!/sbin/ksh

#******************************************************************************
# Flame Flint & Inferno debug information script.
#
# Last Modification Date: 1996/06/28
#
# Author: Milton Hubsher
#
# Copyright (c) 1994 Discreet Logic, Inc.
#******************************************************************************


CONFIG_FILE=''

function sect_header {
   print >>  debug.log
   print '=========' >> debug.log
   print $1 >> debug.log
   print '^^^^^^^^^' >> debug.log
   print >> debug.log
}


function mail_the_stuff {
   print
   print 'Do you want to send this to Discreet Logic Technical Support?'
   PS3="send to Technical Support: "
   select yn in \
   'yes' \
   'no'
   do
      case $REPLY in
         1) Mail -s "$prod_ver" support@discreet.com < debug.log ;;
         2) read person?"Who do you want to send this to? "
            Mail -s "$prod_ver" $person@discreet.com < debug.log ;;
      esac
      break
   done ;
}

function crap_out {
   print
   print '*****************************************************************************'
   print '*  For this script to run correctly you must be logged in as the DL product *'
   print '*  user and in the DL product home directory.                               *'
   print '*****************************************************************************'
   print

   exit
}

function verify_login {
   if [ -a *_VERSION ]
   then
      prod=$(print *_VERSION)
      prod=${prod%%_*}
      if [ -n $(eval print $"${prod}_HOME") ]
      then
         prod_ver=$(cat *_VERSION)
         prod_ver="${prod} ${prod_ver#*\"}"
         prod_ver=${prod_ver%*\"}
      else
         crap_out
      fi
   else
      crap_out
   fi
}


function do_prelim {
   rel_num=$(uname -r)

   if [ -a debug.log ]
   then
      mv debug.log debug.log.old
   fi

   clear
   print 'This script assembles the following information in a file called debug.log.'
   print
   print 'your company name                         product name and version'
   print 'user and group ids                        other user information from finger'
   print 'configuration file                        hardware inventory'
   print 'free local disk space                     graphics information'
   print 'daemons and subsystems used               logical volume configuration'
   print 'Stone + Wire configuration                software inventory'
   print 'IRIX version                              contents of tablet configuration file'
   print 'system id                                 dynamic kernel modules information'
   print 'contents of the .cshrc file               environment information'
   print 'last 200 lines of .log file               last 200 lines of SYSLOG file'
   print 'contents of non-volatile ram              IP addresses and routing table'
   print 'contents of fstab file                    contents of exports file'
   print
   print '*******************************************************************************'
   print '* WARNING: This file will contain complete information about the              *'
   print '*          configuration of the system.  It is advisable that you check with  *'
   print '*          your Systems Administrator before you send this file over a public *'
   print '*          network by E-mail!                                                 *'
   print '*******************************************************************************'
   print
   print 'Please wait while information is collected ...'
   print
   print '*******************************************************************************'
   print '* When prompted for a selection from a list enter the number of the selection.*'
   print '*******************************************************************************'
   print
   sleep 5
}


function get_company {
   # company name
   # ------------
   sect_header 'company name'

   if [[ -a company_name ]]
   then
      cat company_name >> debug.log
   else
      print -n 'Please enter your company name: '
      read comp_name
      print ${comp_name} >> company_name
      cat company_name >> debug.log
   fi
   print >>  debug.log
   print >>  debug.log
   print >>  debug.log
}

function get_name_and_version {
   # product name and version
   # ------------------------
   sect_header 'product name and version'

   prod=$(print *_VERSION)
   prod=${prod%%_*}

   prod_ver=$(cat *_VERSION)
   prod_ver="${prod} ${prod_ver#*\"}"
   prod_ver=${prod_ver%*\"}
   print $prod_ver  >> debug.log
}


function get_ids {
   # user/group id
   # -------------
   sect_header 'user/group id'

   id >> debug.log
}

function get_user_info {
   # finger
   # ------
   sect_header 'finger'

   finger -l ${prod} >> debug.log
}

function get_cfg {
   # configuration file
   # ------------------
   sect_header 'configuration file'

   if [[ -n $(eval print $"${prod}_CONFIG") ]]
   then
      set -A cfgs $(eval ls -1 $"${prod}_CONFIG")
      cfg_path=$(eval print $"${prod}_CONFIG")
   else
      set -A cfgs $(ls -1 ~/cfg)
      cfg_path=~/cfg
   fi

   print 'Select the configuration file you used: '
   PS3='configuration file: '
   select cfg in ${cfgs[*]}
   do
      CONFIG_FILE=$cfg_path/${cfgs[REPLY-1]}
      break
   done

   cat ${CONFIG_FILE} >> debug.log
}

function get_hardware {
   # hinv info
   # ---------
   sect_header 'hinv'

   hinv >> debug.log
}

function get_freedisk {
   # free disk info
   # --------------
   sect_header 'free disk'

   df -kl >> debug.log
}

function get_graphics_info {
   # graphics info
   # -------------
   sect_header 'gfxinfo'

   /usr/gfx/gfxinfo -v >> debug.log
}

function get_chkconfig_info {
   # chkconfig info
   # --------------
   sect_header 'chkconfig'

   chkconfig >> debug.log
}

function get_disk_array_info {
   # lvinfo
   # ------
   sect_header 'lvinfo'

   lvinfo >> debug.log

   # Stone + Wire info
   # -----------------
   sect_header 'Stone + Wire'

   if [[ -a $(eval print $"${prod}_HOME")/Stone_+_Wire/VERS/${rel_num}/STONE/DISKTOOLS/print_disk_config ]]
   then
      $(eval print $"${prod}_HOME")/Stone_+_Wire/VERS/${rel_num}/STONE/DISKTOOLS/print_disk_config 2>> debug.log
   fi

   #if [ -a visinfo ]]
   #then
   #   visinfo >> debug.log
   #fi

}

function get_sw_info {
   # versions info
   # -------------
   sect_header 'versions'

   versions -b >> debug.log
}

function get_system_info {
   # system info
   # -----------
   sect_header 'uname'

   uname -a >> debug.log
}

function get_tablet {
   # dlwacom info
   # ------------
   sect_header 'dlwacom'

   cat /usr/lib/X11/input/config/dlwacom >> debug.log
}

function get_system_id {
   #systen id
   # --------
   sect_header 'sysinfo'

   sysinfo >> debug.log
   print >> debug.log
   sysinfo -s >> debug.log
}

function get_reg_drives {
   # ml info
   # -------
   sect_header 'ml'

   /sbin/ml >> debug.log
}

function get_cshrc {
   # .cshrc
   # ------
   sect_header '.cshrc'

   cat $(eval print $"${prod}_HOME")/.cshrc >> debug.log
}

function get_environment {
   # printenv
   # --------
   sect_header 'printenv'

   printenv >> debug.log
}

function get_product_log {
   # .log
   # ----
   sect_header '.log'

   tail -200 .log >> debug.log
}

function get_system_log {
   # SYSLOG
   # ------
   sect_header '/usr/adm/SYSLOG'

   tail -200 /usr/adm/SYSLOG >> debug.log
}

function get_non_vol_ram {
   # nvram
   # -----
   sect_header 'nvram'

   nvram >> debug.log
}

function get_network_info {
   # As networking related information is sensitive ask if it's ok to include it.
   # ------
   print "\nHas your Systems Administrator OKed including the networking information\n"
   PS3="include networking information: "
   select yn in \
   'yes' \
   'no'
   do
      case $REPLY in
         1) # IP addresses and routing table
            # ------------------------------
            sect_header 'IP address and routing table'

            netstat -rn >> debug.log

            # fstab
            # -----
            sect_header '/etc/fstab'

            cat /etc/fstab >> debug.log

            # exports
            # -------
            sect_header '/etc/exports'

            cat /etc/exports >> debug.log ;;
         2) ;;
      esac
      break
   done ;
}

function send_it {
   # mail the lot?
   # -------------
   print "\nWhat do you what to do with the information collected?\n"
   PS3="select an action: "
   select action in \
      'E-mail to Discreet Logic' \
      'Save in file debug.log' \
      'Send to printer (You must have a default printer set.)'
   do
      case $REPLY in
         1 ) mail_the_stuff ;;
         2 )  ;;
         3 ) lp debug.log ;;
         * ) print 'invalid.' ;;
      esac
      break
   done
}

verify_login
do_prelim
get_company
get_name_and_version
get_ids
get_user_info
get_cfg
get_hardware
get_freedisk
get_graphics_info
get_chkconfig_info
get_disk_array_info
get_sw_info
get_system_info
get_tablet
get_system_id
get_reg_drives
get_cshrc
get_environment
get_product_log
get_system_log
get_non_vol_ram
get_network_info
send_it
