#!/bin/sh
#
#  Quick hack at UCD bug-report script
#

bug_reports_to=ucd-snmp-bugs@ucd-snmp.ucdavis.edu
bug_report_file=/tmp/ucd-snmp-bug-report

# changed automagically
VersionInfo="4.1.2";

#
#   Check how to do echo without a new line
#	-n or \c
#
msg_file=/tmp/ucd-bug.$$
# remove files on signal
trap "rm -f rm ${msg_file} ${msg_file}.2; exit 1" 1 2 15

echo -n "test\c" > $msg_file

if grep c $msg_file>/dev/null
then
    n="-n"
    c=""
else
    n=""
    c="\c"
fi


echo "You are about to compose a bug-report message that is suitable"
echo "for sending to $bug_reports_to." 
echo ""
echo "When the bug-report is completed, you may choose to save to a file"
echo "       and you may choose to send the report in an e-mail."
echo ""
echo "Please write a detailed xdescription of your problem."
echo "At the end you will be given"
echo "the opportunity to include optional text files, such as"
echo "patches, make output, error files, etc..."
echo ""
echo "Note:  If at all possible, this should be run on the machine that"
echo "       that you are having problems with.  If this is not the case,"
echo "       please include the architecture you are having problems on."
echo ""

#
#   Ask for subject and 'From:' field
#
echo $n "Subject [none]: $c"
read answer
subject="$VersionInfo: $answer"

# attempt to figure out domain name.
domain=`grep "^domain" /etc/resolv.conf | awk '{print $2}'`;
if [ "$domain" = "" ]; then
  domain=`grep "^search" /etc/resolv.conf | awk '{print $2}'`;
fi

email=`whoami`@`hostname`.$domain
echo $n "Your email address [$email]: $c"
read answer
if [ "$answer" != "" ]
then
    email=$answer
fi

#
#    Start to build the bug report
#
echo "UCD-SNMP version $VersionInfo bug report" >  $msg_file
echo "----------------------------------------------------------" >> $msg_file
echo                                                              >> $msg_file

cp $msg_file ${msg_file}.2

echo $n "Press return to edit bug report file $c"
read pause
${EDITOR:-vi} $msg_file || exit

cmp -s $msg_file ${msg_file}.2 &&  echo "No description of the bug submitted. Please try again" && exit 1

#
#    Start to create the mail message
#

echo "To: $bug_reports_to"                  > ${msg_file}.2
echo "From: $email"                        >> ${msg_file}.2
echo "Subject: $subject"                   >> ${msg_file}.2
echo "MIME-Version: 1.0"                   >> ${msg_file}.2

#
#  Make this a multipart message
#

#boundary="`hostname`.$$.`date | tr "  " .`"
boundary="temp-boundary.$$"

echo "Content-Type: multipart/mixed;"              >> ${msg_file}.2
echo "        boundary=\"$boundary\""           >> ${msg_file}.2
echo                                            >> ${msg_file}.2
echo "--$boundary"                              >> ${msg_file}.2

#
#  and add the bug report so far
#
echo "Content-Description: Bug Report"          >> ${msg_file}.2
echo                                            >> ${msg_file}.2
cat  ${msg_file}                                >> ${msg_file}.2
echo                                            >> ${msg_file}.2

#
#    Add some potentially useful information
#
echo "--$boundary"                              >> ${msg_file}.2
echo "Content-Description: Version Information" >> ${msg_file}.2
echo                                     	>> ${msg_file}.2
echo "ucd-snmp Version:  $VersionInfo"   	>> ${msg_file}.2
echo $n "System configuration:  $c"            	>> ${msg_file}.2
uname -a  | awk '{ $2=""; print; }'            	>> ${msg_file}.2
if [ -f ./config.guess ]
then
  echo $n "config.guess:  $c"                   >> ${msg_file}.2
  ./config.guess                                >> ${msg_file}.2
fi
if [ -f ./config.status ]
then
  grep running config.status                    >> ${msg_file}.2
fi
echo                                     	>> ${msg_file}.2

#
#    Any other files to add?
#

echo "You may now include optional text files (patches, make -k output, etc)."
echo $n "Enter a file to include [RETURN if none]: $c"
read answer
while [ "$answer" != "" ]
do
    #
    #  Include the specified file
    #   
    if [ -f "$answer" ]
    then
	echo "--$boundary"                              >> ${msg_file}.2
	echo "Content-Description: $answer"             >> ${msg_file}.2
	echo                                            >> ${msg_file}.2
	cat  ${answer}                                  >> ${msg_file}.2
	echo                                            >> ${msg_file}.2
    else
	echo "Cannot open file $answer"
    fi

    echo $n "Enter another file to include [RETURN if no more]: $c"
    read answer
done


#
# Add config.cache file
#
if [ -f config.cache ]
then
  echo "--$boundary"                              >> ${msg_file}.2
  echo "Content-Description: config.cache"        >> ${msg_file}.2
  echo                                            >> ${msg_file}.2
  cat  config.cache                               >> ${msg_file}.2
  echo                                            >> ${msg_file}.2
fi

#
#  Finish off the message, send it and tidy up.
#
#
echo "--$boundary--"                            >> ${msg_file}.2

sendit="y"
echo $n "Do you want to send the bug-report in an e-mail [y] ? $c"
read answer
if [ "$answer" != "y" ]
then
    sendit="n"
fi

if [ "$sendit" = "y" ]
then
if [ -x /usr/lib/sendmail ]
then
    /usr/lib/sendmail $bug_reports_to < ${msg_file}.2
else
    mail $bug_reports_to < ${msg_file}.2
fi
fi

echo $n "Do you want to save the bug-report to a file [y] ? $c"
read answer
if [ "$answer" != "n" ]
then
    echo $n "File to save bug-report [$bug_report_file]: $c"
    read answer
    if [ "$answer" != "" ]
    then
        bug_report_file=$answer
    fi
    rm -f $bug_report_file
    cp ${msg_file}.2 $bug_report_file
fi

rm ${msg_file} ${msg_file}.2
