#!/bin/ksh
#
# Program:     linuxcfg
# Created by:  Michelle Philpot
# Date:        April 1999
# Purpose:     To collect software and hardware information from
#              a customer's machine.  This korn shell script is
#              incorporated in the CUSTCFG tool owned by IBM's NCSD 
#              Service Delivery Technology in Austin.
#

#set -x

# function to receive confirmation on customer's answers
answers()
{
   if [[ $doneflag = "n" ]]
   then
      echo -e "\nThese are the answers you have supplied:\n"
   fi
   echo -e "\n1. First name:\t\t\t"$fname
   echo -e "2. Last name:\t\t\t"$lname
   echo -e "3. Company name:\t\t"$cname
   echo -e "4. Customer number:\t\t"$custnum
   echo -e "5. PMR number:\t\t\t"$pmrnum
   echo -e "6. Phone number:\t\t"$phonenum
   echo -e "7. Machine make and model:\t"$machinemm
   echo -e "8. E-Mail address:\t\t"$emailaddr
   if [[ $doneflag = "n" ]]
   then
      echo -e "\nIf you need to change any of these answers, type the number"
      echo -e "associated with that item, and hit Enter.  Otherwise, just hit"
      echo -e "the Enter key.\n"
      read response
   fi
}

# function to verify that the customer entered 7 characters
custnumtest()
{
   while [[ ${#custnum} != 7 ]]
   do
      echo -e "\nPlease enter your customer number again.  It must be"
      echo -e "seven digits long.  Thank you.\n"
      read custnum
   done
}

# function to verify that the customer entered correct pmr syntax
pmrnumtest()
{
   pmrnumtest1=$(echo $pmrnum | cut -c 6,10)
   pmrnumtest2=$(echo $pmrnum | cut -c 11-14)
   while [[ $pmrnumtest1 != ",," || ${#pmrnumtest2} != 3 ]]
   do
      echo -e "\nPlease enter your pmr number again using the following"
      echo -e "syntax:  11111,222,333  (where 11111 = PMR number,"
      echo -e "                               222   = Branch Office number,"
      echo -e "                               333   = Country code (USA=000)"
      read pmrnum
      pmrnumtest1=$(echo $pmrnum | cut -c 6,10)
      pmrnumtest2=$(echo $pmrnum | cut -c 11-14)
   done
}

logfile=/var/log/messages
doneflag="n"
distfile=/proc/version
issuefile=/etc/issue

# first find out if the customer has an IBM-supported linux distributor
distributor=""
grep -qi suse $distfile
if [ $? -eq 0 ]
then 
   if [[ -f $issuefile ]]
   then 
      grep -qi suse $issuefile
      if [[ $? -eq 0 ]]
      then distributor=`awk '/SuSE/ {print $3, $4, $5}' $issuefile`
      else distributor="SuSE"
      fi
   else distributor="SuSE"
   fi
fi

grep -qi redhat $distfile
if [ $? -eq 0 ]
then 
   if [[ -f $issuefile ]]
   then
      grep -qi "red hat" $issuefile
      if [[ $? -eq 0 ]]
      then distributor=`awk '/Red Hat/ {print $0}' $issuefile`
      else distributor="Red Hat"
      fi
   else distributor="Red Hat"
   fi
fi

grep -qi caldera $distfile
if [ $? -eq 0 ]
then 
   if [[ -f $issuefile ]]
   then
      grep -qi caldera $issuefile
      if [[ $? -eq 0 ]]
      then 
         distributor1=`awk '/^Caldera/ {print $0}' $issuefile`
         distributor2=`awk '/^Version/ {print $0}' $issuefile`
         distributor=`echo $distributor1 $distributor2`
      else distributor="Caldera OpenLinux"
      fi
   else distributor="Caldera OpenLinux"
   fi
fi

grep -qi turbolinux $distfile
if [ $? -eq 0 ]
then 
   if [[ -f $issuefile ]]
   then
      grep -qi turbolinux $issuefile
      if [[ $? -eq 0 ]]
      then distributor=`awk '/TurboLinux/ {print $0 "-Pacific HiTech"}' $issuefile`
      else distributor="TurboLinux (Pacific HiTech)"
      fi
   else distributor="TurboLinux (Pacific HiTech)"
   fi

fi

if [[ -z "$distributor" ]]
then
  echo -e "\a\nThis is not an IBM-supported Linux distributor.  If you are"
  echo -e "running the Linux system distributed by Red Hat Software, Inc.," 
  echo -e "SuSE Holding AG, Caldera Systems, Inc. or TurboLinux, then"
  echo -e "contact your IBM Service Representative for your next course of"
  echo -e "action.  I will now exit this program.  Thank you.\n\a"
  exit
fi

# ask some preliminary questions 
echo -e "\nI need to ask you some preliminary questions before I gather"
echo -e "your machine's configuration information.  Don't worry if you"
echo -e "make a mistake.  You can change any information after you have"
echo -e "entered your answers."
echo -e "\nWhat is your first name?"
read fname
echo -e "\nWhat is your last name?"
read lname
echo -e "\nWhat is your company name?"
read cname
echo -e "\nWhat is your 7-digit customer number?  (ex. 1234567)"
read custnum
custnumtest
echo -e "\nWhat is your PMR number, branch office and country code?"
echo "(ex. 12345,123,123)  Note:  the country code for USA is 000"
read pmrnum
pmrnumtest
echo -e "\nWhat is your daytime phone number?  (ex. 123-123-1234)"
read phonenum
echo -e "\nWhat is your machine make and model?  (ex. IBM PC 750 6887-86B)"
read machinemm
echo -e "\nWhat is your e-mail address?  (ex. id@domain)"
read emailaddr
answers
while [[ $response = [1-8] ]]
do
   case "$response" in
      "1") echo "What is your first name?"; read fname;;
      "2") echo "What is your last name?"; read lname;;
      "3") echo "What is your company name?"; read cname;;
      "4") echo "What is your customer number?  (ex. 1234567)"; 
           read custnum;
           custnumtest;;
      "5") echo "What is your PMR number?  (ex. 12345,123,123)"; 
           read pmrnum;
           pmrnumtest;;
      "6") echo "What is your phone number?  (ex. 123-123-1234)";read phonenum;;
      "7") echo "What is your machine make and model?  (ex. IBM PC 750 6887-86B)"; read machinemm;;
      "8") echo "What is your e-mail address?"; read emailaddr;;
   esac
   answers
done

# now write these answers to the output file (outfile)
outfile=/tmp/`echo $pmrnum | cut -c 1-5,7-9`.lnx #use 8.3 filename format
#outfile=/tmp/`echo $pmrnum | cut -c 1-5,7-9,11-13`.`hostname | cut -f1 -d.`
echo -e "\nLinux getcfg output produced on $(date).\n" > $outfile
doneflag=y
answers >> $outfile

# write the linux distributor to the outfile
echo -e "Now I am going to collect some hardware and software information.\n"
echo -e "\nThe Linux distributor is:  "`echo $distributor`".\n" >> $outfile

# machine hardware type
echo -e "The machine processor type is:  " `uname -m`"\n" >> $outfile

# machine nodename
echo -e "The machine nodename is:  " `uname -n`"\n" >> $outfile

# machine operating system and release
echo -e "The operating system and release are:  " `uname -s` `uname -r`"\n" \
  >> $outfile

# machine operation system version
echo "The version is" `uname -v | cut -f1 -d' '` "and it was made available on"\
  >> $outfile
echo -e `uname -v | cut -f3-7 -d' '`".\n" >> $outfile

# memory available
echo -e "\nThe memory installed on this machine is:\n" >> $outfile
free >> $outfile

# hardware information
echo -e "\nThe hardware information is as follows:  \n" >> $outfile
# the following command doesn't work on TurboLinux because it states that
# permission is denied when attempting to write to /dev/null when NOT on
# root id
#lsdev > /dev/null 2>&1  #redirect error info to "bit bucket" so that it doesn't
                        #write to standard out if this executable is not found
tmpfile=./lsdev.tmp
lsdev > $tmpfile 2>&1
if [[ $? -eq 0 ]]
then
   echo -e "...lsdev output...\n" >> $outfile
   lsdev >> $outfile
else
   procinfo > $tmpfile 2>&1
   if [[ $? -eq 0 ]]
   then
      echo -e "...procinfo output...\n" >> $outfile
      procinfo | grep -i irq >> $outfile
   else echo -e "...lsdev AND procinfo not available...\n" >> $outfile
   fi
fi
rm -f $tmpfile

# network info
echo -e "\n\nThe network interfaces are:\n"  >> $outfile
netstat -i >> $outfile
echo -e "\nThe routing information is:\n" >> $outfile
netstat -rn >> $outfile

# user resource limits
echo -e "\nThe current user resource limits are:\n" >> $outfile
ulimit -a >> $outfile

# filesystems
echo -e "\nThe defined filesystems are:\n"  >> $outfile
df >> $outfile

# mount information
echo -e "\nThe mount information is:\n"  >> $outfile
mount >> $outfile

# machine's up time
echo -e "\nThe up time and load average for this machine are:  \n" >> $outfile
uptime >> $outfile

# gather all software packages installed via rpm (Red Hat Package Manager)
echo -e "\nIt may take about a minute to collect the installed software"
echo -e "information.  We appreciate your patience.\n"
echo -e "\nAll of the software packages installed on this machine \
via RPM (Red Hat Package Manager) are the following:\n" >> $outfile

tmpfile=./product.tmp
tmpfile2=./tmpfile
errout=./error.out

for product in $(rpm -qa) #set variable as a result of command substitution
do
   rpm -qi $product > $tmpfile 2> $errout
   if [[ -s $errout ]]
   then cat $errout >> $outfile
   else
      name=$(awk '/^Name/ {print $3}' $tmpfile)
      version=$(awk '/^Version/ {print $1,$3}' $tmpfile)
      release=$(awk '/^Release/ {print $1,$3}' $tmpfile)
      grep "Install date:" $tmpfile > $tmpfile2
      installdate=$(awk '{print "Installed on "$4,$5,$6,$7}' $tmpfile2)
      echo "$name => $version, $release, $installdate" >> $outfile
   fi
done

rm -f $tmpfile
rm -f $tmpfile2
rm -f $errout

# gather the /var/log/messages file; must be root to read it
echo -e "\n\nThe following is the /var/log/messages file:\n" >> $outfile
myid=`whoami`
if [[ $myid != "root" ]]
then
   echo -e "\aI must become root to read the /var/log/messages file."
   echo -e "\aPlease enter your root password when prompted.  I will NOT be"
   echo -e "\areading or saving root's password.  Thank you.\n"
   sleep 2
   su root "-c cat $logfile >> $outfile"
   rc=$?
   attempts=0
   while [[ $rc = 1 && $attempts -lt 2 ]]
   do
      echo -e "\nSince you entered an incorrect password, would you like to"
      echo -e "try again?  (y|n)"
      read reply
      if [[ $reply = "y" ]]
      then
         su root "-c cat $logfile >> $outfile"
         rc=$?
         attempts=$(expr $attempts + 1)
      else 
         echo -e "Can't collect /var/log/messages because customer" >> $outfile
         echo -e "chose not to switch to the root id." >> $outfile
         break
      fi
   done
   if [[ $attempts -eq 2 ]]
   then
      echo -e "Sorry, you entered an incorrect root password 3 times."
      echo -e "Therefore, I can't collect the /var/log/messages file."
      echo -e "Can't collect /var/log/messages because an incorrect" >> $outfile
      echo -e "root password was entered 3 times." >> $outfile
   fi
   if [[ $rc = 0 ]]
   then echo -e "\a\nI have exited from root's id now.\n\a"
   fi
else
   cat $logfile >> $outfile
fi

# script completed
echo -e "\nThis program is now finished.  Thanks for your participation.\n"
