#!/bin/sh

#  Copyright (C) 2002 Pawel Kolodziej <pawelk@pld.org.pl>
#
#  Script for easy bug report adding to http://bugs.pld.org.pl
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# $Id: pldbug.sh,v 1.4 2002/09/16 11:32:53 pawelk Exp $

if ! which dml > /dev/null ; then
	echo You must have \"dml\" installed in order to use this script
	exit
fi
if ! which mail > /dev/null ; then
	echo This program sends bg report via mail, but there is no \"mail\" program
	echo in your system.
	exit
fi

if [ -e ~/.pld.bts ] ; then 
	. ~/.pld.bts
fi

if [ "$BTS_EMAIL" = "" -o "$BTS_PASSWORD" = "" ] ; then

	eval `dml << EOF
Welcome in PLD Bug reporting system.<br>
To use this system you should have got account in http://bugs.pld.org.pl<br>
If you don't have account please visit http://bugs.pld.org.pl and create<br>
one. It's realy simply.<br>
If you already have account type email and password here<br>
Email   : <input id=BTS_EMAIL><br>
Password: <input id=BTS_PASSWORD pass=1><br>
<button caption="Login" id="login">
<br><br>
However if you don't have account and for some reason you don't want to<br>
create it, you can send bugreport as \"anonymous\", but we don't<br>
recomend doing that since then we have no way to contact you.<br>
<button caption="Work as anonymous" id="anon">
EOF`

	if [ "$BTS_EMAIL" = "" -o "$BTS_PASSWORD" = "" ] ; then
		if [ $anon = "no" ] ; then
		eval `dml << EOF
		You have NOT typed either email or password.<br>
		<button caption="Exit">
EOF`
exit
fi
fi

## here we have "login=yes" and not emty password and mail or 
## anon=yes

if [ $login = "yes" ] ; then
	eval `dml << EOF
I can save your email and  password in ~/.pld.bts  file, so you will<br>
not have to type it any more.<br>
Should I do that ?<br>
<button caption="Yes, save" id="save"> 
<button caption="No, don't save" id="dontsave">
EOF`

if [ $save = "yes" ] ; then
	echo "BTS_EMAIL=\"${BTS_EMAIL}\"" >> ~/.pld.bts
	echo "BTS_PASSWORD=\"${BTS_PASSWORD}\"" >> ~/.pld.bts
fi
fi
fi		# asking for email/password

if [ "$anon" = "yes" ]; then
	BTS_EMAIL="anon"
fi

eval `dml << EOF
Do you want to report bug in package that you have already installed<br>
<button caption="Yes, I have that package installed" id="inst"> <br>
<button caption="No, this package is not installed in this system now" id="notinst" >
EOF`

if [ "$notinst" = "yes" ]; then
	eval `dml << EOF
	Please give package name and full version (with epoch if exists)<br>
	Package name: <input id=pkg><br>
	Full version: <input id=EVR><br><button>
EOF`
else 		# pakiet jest zainstalowany w systemie
	eval `dml << EOF
Please give package name <input id="pkg"><br><button>
EOF`
	if rpm -q "$pkg" 2>&1 > /dev/null ; then
		echo
	else
		eval `dml "Package \"$pkg\" is not installed in yours system<br><button>"`
		exit
	fi
	EPOCH=`rpm -q --qf '%{EPOCH}' $pkg`
	VER=`rpm -q --qf '%{VERSION}-%{RELEASE}' $pkg`
	if [ "$EPOCH" = "(none)" ] ; then
		EVR=$VER
	else
		EVR="${EPOCH}:${VER}"
	fi
	eval `dml "I have found package \"$pkg\" in version \"$EVR\"<br>in yours system<br><button>"`
fi


eval `dml <<EOF
You are going to send bugreport about package<br>
"$pkg" in version "$EVR". Is that correct ?<br>
<button caption="Yes, it's right" id=ok><br>
<button caption="No, there is mistake !"><br>
EOF`
if [ $ok = "no" ] ; then
	exit
fi

eval `dml <<EOF
Please select yours bug report category:<br>
<menu id=category res=1>
<item id=1>unknown
<item id=2>bug in building stage
<item id=3>bug in rpm's scripts
<item id=4>other bug on spec
<item id=5>doesn't work as it should
<item id=6>improve request
<item id=7>feature request
<item id=8>security problem
</menu>
EOF`

eval `dml <<EOF
Please enter report title. It should be short, but significant<br>
Title: <input id=title><br><button>
EOF`

while [ -l "$title"  -le 3 ] ; do
eval `dml <<EOF
You have entered report title shorter then 3 characters.<br>
Please try to type some bit more descriptive one...<br>
Please enter report title. It should be short, but significant<br>
Title: <input id=title><br><button>
EOF`
done

editor=${EDITOR-vi}
eval `dml <<EOF
Now I will start $editor. Please type your bug report, save file<br>
and exit<br><button>
EOF`

tmpname=/tmp/bugpld.$USER.$$.txt
$editor $tmpname

eval `dml <<EOF
Bug report is now complete. Are you sure taht you want to send it ?<br>
<button caption="Yes, I want to send it and make PLD even beter" id=send><br>
<button caption="No, I was just kiding. There is no bug in PLD">
EOF`

if [ $send != "yes" ] ; then
	eval `dml "Bug report canceled<br><button>"`
	exit
else
	cat > $tmpname.1 << EOF
Email: $BTS_EMAIL
Password: $BTS_PASSWORD
Command: new
Package: $pkg
EVR: $EVR
Category: $category
Title: $title
Text:
EOF

cat $tmpname >> $tmpname.1

mail -s MBTemail pawelk@ep09.kernel.pl < $tmpname.1

eval `dml <<EOF
Your bug report has ben send to PLD via mail. <br>
You should receive email with confirmation soon.<br>
Thanks.<br><button>
EOF`

fi
