#!/bin/sh
# $Id: problemreport,v 1.11 1993/08/05 01:00:31 adl Exp $
# Copyright (c) 1986-1993 Frame Technology Corp. All rights reserved.
#
# problemreport
#
# Prompts users for critical information about the bug.
# Formats and sends the information to the database machine
# for automatic insertion into the database.

##### User-editable defaults

# E-Mail address of Frame Technology bug database
bugdb="qadbugload@frame.com"
# bugdb="ames!frame!qadbugload"

# Mail sent to the "qadbugload" address is forwarded directly to 
# Frame's bug database. No human reads it. If you need technical 
# assistance regarding Frame products, please use "comments@frame.com" 
# or "ames!frame!comments" for your email correspondence.

# Defaults file
defaults=".fmbugrc"


##### Initialize variables

PATH="/usr/ucb:/bin:$PATH"; export PATH;
myname=`expr //$0 : '.*/\(.*\)'`
debug=0
tmpmail="/tmp/bugmail$USER$$"
tmpdesc="/tmp/bugdesc$USER$$"
ASK="$FMHOME/bin/scripts/fm_ask"
ARCH="$FMHOME/bin/scripts/fmarch"
prgm=builder
repro=1
sev=4
cont=problemrep
confirm=y
mail=y
keywds=FieldReport
qanote=FieldReport         # for good measure
programlist=
# yep, we're in trouble if there's more than one ^Version line
vers=`sed -n 's/^Version \([^ ][^ ]*\) .*$/\1/p' $FMHOME/.VERSION 2>/dev/null`
fullname= email_addr= phone_num= Xserver= Xserver_ram= Xclient_ram=
Xwmgr= mach= title= compname=

# find mailer
if [ -f /bin/mail ]; then
  mailer=/bin/mail
elif [ -f /usr/ucb/Mail ]; then
  mailer=/usr/ucb/Mail
elif [ -f /usr/bin/mailx ]; then
  mailer=/usr/bin/mailx
else
  echo "$myname: Error: Cannot find appropiate mailer. Exiting."
  exit 1
fi

# find editor
edavail=y
: ${EDITOR:=vi}
if which $EDITOR | grep '^/' >/dev/null 2>&1 ; then
	:
elif which vi | grep '^/' >/dev/null 2>&1 ; then
	EDITOR=vi
else
	edavail=n
fi 

case "`echo -n`" in
-n)	cc="\c"; nn="" ;;
*)	cc=""; nn="-n" ;;
esac


##### Escape sequence

trap "rm -f $tmpmail $tmpdesc; exit 1" 1 2 3 15


##### Catch any command line arguments

case "$1" in
-d)
	debug=1
	echo "
------------ DEBUGGING ON --------------"
	;;
esac


##### Start the inquisition

echo "
This is an automated problem reporting mechanism. You will be prompted
for critical information needed by Frame Technology's Software 
Development and QA departments. Default values are shown in brackets.
Lastly, the new problem data will automatically be distributed to 
developers and QA, and inserted into Frame Technology's bug database
for review.
"
$ASK "y" "n" "Do you wish to continue?" || exit 1

# Read personal defaults file if it exists
/bin/ls $HOME/$defaults 2>/dev/null | grep $defaults >/dev/null 2>&1 &&
	. $HOME/$defaults

# Ask user their name.
echo $nn "
Please enter your full name
[$fullname]: " $cc
read line
[ "$line" = "" ] || fullname=$line
while [ "$fullname" = "" ]; do
    echo $nn "Try again. Full name: " $cc
    read fullname
done

# Ask user for company name.
echo $nn "
Please enter your company name
[$compname]: " $cc
read line
[ "$line" = "" ] || compname=$line
while [ "$compname" = "" ]; do
    echo $nn "Try again. Company name: " $cc
    read compname
done

# Ask user for e-mail address.
echo $nn "
Please enter your e-mail address (i.e. user@company.com)
[$email_addr]: " $cc
read line
[ "$line" = "" ] || email_addr=$line
while [ "$email_addr" = "" ]; do
    echo $nn "Try again. E-mail address: " $cc
    read email_addr
done

# Ask user for phone number.
echo $nn "
Please enter your phone number (i.e. (408) 433-3311)
[$phone_num]: " $cc
read line
[ "$line" = "" ] || phone_num=$line
while [ "$phone_num" = "" ]; do
    echo $nn "Try again. Phone number: " $cc
    read phone_num
done

# Ask user for their X server type.
echo $nn "
Please enter the source of your X server (MIT, Sun, HP, DEC, IBM, etc.)
[$Xserver]: " $cc
read line
[ "$line" = "" ] || Xserver=$line
while [ "$Xserver" = "" ]; do
    echo $nn "Try again. X server: " $cc
    read Xserver
done

# Ask user for their X server RAM.
echo $nn "
Please enter the amount of RAM on the X server machine
[$Xserver_ram]: " $cc
read line
[ "$line" = "" ] || Xserver_ram=$line
while [ "$Xserver_ram" = "" ]; do
    echo $nn "Try again. X server RAM: " $cc
    read Xserver_ram
done

# Ask user for their X client RAM.
echo $nn "
Please enter the amount of RAM on the X client machine
[$Xclient_ram]: " $cc
read line
[ "$line" = "" ] || Xclient_ram=$line
while [ "$Xclient_ram" = "" ]; do
    echo $nn "Try again. X client RAM: " $cc
    read Xclient_ram
done

# Ask user for their Window Manager.
echo $nn "
Please enter the name of your X window manager
[$Xwmgr]: " $cc
read line
[ "$line" = "" ] || Xwmgr=$line
while [ "$Xwmgr" = "" ]; do
    echo $nn "Try again. X window manager: " $cc
    read Xwmgr
done


##### Start repeating loop

while : ; do

# Get the bug title
	echo $nn "
Enter a 1 line descriptive title for the bug (70 char max)
[$title]: " $cc
	read line
	[ "$line" = "" ] || title=$line
	while [ "$title" = "" ]; do
		echo $nn "Try again. Bug title: " $cc
		read title
	done

# Get program name.
	echo "
Indicate which program (executable) in the product was being used. 
Here is a list of names:
"
	ncol=4
	[ "$programlist" ] || {
		programlist="\
			`cd $FMHOME/bin;
			for i in * ; do
				[ -d \"$i\" ] || echo \" $i\"
			done` \
			`cd $FMHOME; ls *read* 2>/dev/null;` \
			fdk fmusersetup"
		lines=`echo $programlist | wc -w`
		lines=`expr $lines / $ncol + 1`
		programlist=`echo $programlist | tr ' ' '\012' | sort | \
			pr -$ncol -t -l$lines`
	}
	echo $nn "$programlist

Program Name [$prgm]: " $cc
	read line
	[ "$line" = "" ] || prgm=$line
	while [ "$prgm" = "" ]; do
		echo $nn "Try again. Program name: " $cc
		read prgm
	done

# Get program version number
	echo $nn "
Enter the version number of the program you are using
(12 char max) [$vers]: " $cc
	read line
	[ "$line" = "" ] || vers=$line
	while [ "$vers" = "" ]; do
		echo $nn "Try again. Version: " $cc
		read vers
	done

# Get machine / operating system
	echo $nn "
Enter the machine and OS version you are using
(30 char max) [$mach]: " $cc
	read line
	[ "$line" = "" ] || mach=$line
	while [ "$mach" = "" ]; do
		echo $nn "Try again. Machine and OS: " $cc
		read mach
	done

# Get reproducibility code.
	while : ; do
		echo $nn "
Enter reproducibility index as follows:
   1 = Didn't try to reproduce
   2 = Tried but couldn't reproduce
   3 = Reproducible
[$repro]: " $cc
		read line
		[ "$line" != "" ] && repro=$line
		case "$repro" in
		[1-3])	break ;;
		esac
		echo "Try again."
		repro=
	done

# Get severity code.
	while : ; do
		echo $nn "
Enter severity index as follows: 
   1 = Causes crash
   2 = No work around
   3 = Has work around
   4 = Inconvenience
   5 = Enhancement request
   6 = Documentation error
[$sev]: " $cc
		read line
		[ "$line" != "" ] && sev=$line
		case "$sev" in
		[1-6])	break ;;
		esac
		echo $nn "Try again." $cc
		sev=
	done

# See if user wants to use editor.
	echo "
Enter a description of the problem. If you have any related documents, 
core files, etc., please contact Frame technical support."
	eduse=n
	case $edavail in
	y)
		$ASK n y "
Do you want to use an editor ($EDITOR) to enter the 
description?" ||
		{
			eduse=y
			touch $tmpdesc
			$EDITOR $tmpdesc
		}
		;;
	esac
	case $eduse in
	n)
		echo "
WARNING:   You must press return after each line you type.
           This script cannot handle extremely long lines.

When you are finished entering a description, type <Control>-D
(press the 'Control' key and 'D' simultaneously).
"
		cat > $tmpdesc
		;;
	esac

# Set product abbreviation based on program.
	pabbrev=MkrCoreX
	case "$prgm" in
	*[Ff]ont*)
		pabbrev=FiltersUnx ;;
	*to*)
		pabbrev=FiltersUnx ;;
	add_extension|filterbatch|fminvert|wpconfig)
		pabbrev=FiltersUnx ;;
	esac

# Now echo everything back to user and see if it's OK
	echo "

This bug will be logged in against: $pabbrev
The information you have entered is as follows:
 Title: 	$title
 Program Name:	$prgm
 Version #: 	$vers
 Machine/OS: 	$mach
 Reproducible:	$repro
 Severity: 	$sev
 Description:
" > $tmpmail
	cat $tmpdesc >> $tmpmail
	more $tmpmail

	case $debug in
	0)	where="Frame Technology" ;;
	*)	where="$USER" ;;
	esac
	send=n
	$ASK y n "
Send to $where?" && send=y

# Ask user if they would like to save settings
	$ASK y n "Would you like to save the information you've entered
in defaults file $HOME/$defaults?" && 
	{
		mv $HOME/$defaults $HOME/$defaults.old 2>/dev/null
		cat >$HOME/$defaults <<defEOF
# Frame Technology 'problemreport' defaults file
fullname='$fullname'
compname='$compname'
email_addr='$email_addr'
phone_num='$phone_num'
Xserver='$Xserver'
Xserver_ram='$Xserver_ram'
Xclient_ram='$Xclient_ram'
Xwmgr='$Xwmgr'
prgm='$prgm'
vers='$vers'
mach='$mach'
defEOF
	}
	case "$send" in
	y)
# Turn repro code that the user entered (1,2,3) into a percent that
# the database wants (0, 50, 100).
		tmprepro=`expr $repro - 1`
		dbrepro=`expr $tmprepro \* 50`
# Put the fields that auto-bug load program wants into the bug mail file.
		echo "**********  Bug Mailed `date` by $USER **********" > $tmpmail
		echo "
Product:	$pabbrev
Title: 		$title
Program Name:	$prgm
Version #: 	$vers
Machine/OS: 	$mach
Reproducible:	$dbrepro
Severity: 	$sev
Contact: 	$cont
Keywords:	$keywds
QANote:		$qanote
Confirmation:	$confirm
Send Mail: 	$mail
Description:
" >> $tmpmail

# Add an ID line & motd line to the beginning of the description.
# Copy the description into the bug mail file. Run thru sed so ~
# at beginning of line doesn't get processed by mail.
  		echo "Reported by $fullname ($USER), `date`, from `hostname`
fmarch returns  : `$ARCH`
E-mail address  : $email_addr
Company         : $compname
Phone number    : $phone_num
X server source : $Xserver
X server RAM    : $Xserver_ram
X client RAM    : $Xclient_ram
X window manager: $Xwmgr" >> $tmpmail
		[ -f /etc/motd ] && head -1 /etc/motd >> $tmpmail
		echo "
Title: $title
Version: $vers
" >> $tmpmail
		cat $tmpdesc | fold | sed 's@^~@>~@' >> $tmpmail
		echo "
Version Info:" >> $tmpmail
		cat $FMHOME/.VERSION >> $tmpmail 2>&1
		echo " " >> $tmpmail

# Mail the bug file to the auto bug program
		case $debug in
		0)
			$mailer $bugdb < $tmpmail
			$mailer $USER < $tmpmail
			echo "
    Problem report mailed to $USER and Frame Technology for entry 
    into database.
"
			;;
		*)
			$mailer $USER < $tmpmail
			echo "
    Problem report mailed to $USER for scrutiny.  No report was sent
    to Frame Technology.
"
			;;
		esac
		> $tmpdesc
		;;
	esac		# send=y
	$ASK "y" "n" "Report another problem?" || break
done

rm -f $tmpmail $tmpdesc
exit 0
