#!/bin/sh
#
# Copyright (c) 2001 Pawel Golaszewski <blues@pld.org.pl>.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#       This product includes software developed by Pawel Golaszewski.
# 4. Neither the name of the author nor the names of any co-contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY PAWEL GOLASZEWSKI AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# Idea is based on Michal Moskal's shirc
#

default_server=mail.pld.org.pl
default_domain=pld.org.pl
DOMAIN=$default_domain
port=25
BUGADDR=bugs@pld.org.pl
default_user=anonymous@pld.org.pl
OPEN=/tmp/pld-bugreport.open
DATA=/tmp/pld-bugreport.data
END=/tmp/pld-bugreport.end
RELEASE=undetermined
RELSTATUS=unstable
REPORTER_VERSION=0.1


if [ -z "$EDITOR" ]; then
        if [ -x /usr/bin/editor ]; then
                EDITOR=editor
        elif [ -x /usr/bin/vi ]; then
                EDITOR=vi
        else
                echo "$0: No default editor found: attempting to use vi" >&2
                EDITOR=vi
        fi
fi

test "$MAILSERVER" || {
  echo "This is BugReport form for PLD Linux Distribution."
  echo "We are thankfull for every sign that something goes wrong."
  echo "If you know the solution we are thankfull twice :)"
  echo
  echo "If you are in an firewalled network pick your own."
  echo -n "Enter your mail-server [$default_server]: "
  read MAILSERVER
  test "$MAILSERVER" || MAILSERVER=$default_server
}

  echo
  echo "If you wan't to become an answer give us your address"
  echo -n "Enter your mail-server [$default_user]: "
  read USER
  test "$USER" || USER=$default_user

# main part:
cat > $DATA <<EOF
To: ${BUGADDR}
Subject: [50 character or so descriptive subject here (for reference)]

Description:
        [Detailed description of the problem, suggestion, or complaint.]

Repeat-By:
        [Describe the sequence of events that causes the problem
        to occur.]

Fix:
        [Description of how to fix the problem.  If you don't know a
        fix for the problem, don't include this section.]
	
Configuration Information [Automatically generated, do not change]:
Machine: $MACHINE
uname output: $UN
Machine Type: $MACHTYPE

Version: $RELEASE
Release Status: $RELSTATUS

EOF

cp $DATA $DATA.x

until $EDITOR $DATA; do
        echo "$0: editor \`$EDITOR' exited with nonzero status."
        echo "$0: Perhaps it was interrupted."
        echo "$0: Type \`y' to give up, and lose your bug report;"
        echo "$0: type \`n' to re-enter the editor."
        echo $n "$0: Do you want to give up? $c"

        read ans
        case "$ans" in
        	[Yy]*)
			echo "Good Bye!"
			exit 1 
		;;
        esac
done

# Check if something was written:
trap 'rm -f $DATA $DATA.x; exit 1' 2    # restore trap on SIGINT

if cmp -s $DATA $DATA.x
	then
        	echo "File not changed, no bug report submitted."
        	exit
fi
rm $DATA.x

#servers conversation:
cat >$OPEN <<OPENEOF
EHLO $DOMAIN
MAIL FROM:<$USER>
RCPT TO:<$BUGADDR>
DATA
X-BugReport: $REPORTER_VERSION
OPENEOF

cat >$END <<ENDEOF
.
QUIT

ENDEOF

SERVER="nc $MAILSERVER $port"

echo $n "Send bug report? [y/n] $c"
read ans
case "$ans" in
	[Nn]*)
		echo "BugReport was NOT sent"
		exit 0
	;;
	[Yy]*)
		echo "Conversation to the server:"
		cat $OPEN $DATA $END | $SERVER
		rm /tmp/pld-bugreport*
		echo
		echo "Thank you for sending bug-report"
		exit 0
	;;		
esac

