#!/bin/sh

#  Copyright (C) 2002 Pawel Kolodziej <pawelk@pld-linux.org>
#
#  Script for easy bug report adding to http://bugs.pld-linux.org
#
#  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; 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.6 2002/09/25 12:07:08 pawelk Exp $


#BTSEMAIL=pawel
BTSEMAIL=pawelk@ep09.kernel.pl

VERSION=1.4

usage()
{
	echo "pldbug v.${VERSION} (c) by Pawel Kolodziej 2002 <pawelk@pld-linux.org>"
	echo "Commands:"
	echo "--help			help"
	echo "--spool			send all reports from spool"
	echo "--send <filename>	send report previously created by plgbug and saved in given file"
}

spool()
{
	for i in ~/.pldbug/spool/* ; do
		if [ -e "$i" ] ; then  
		if send $i ; then
			mv $i ~/.pldbug/sent/
			echo file "$i moved to ~/.pldbug/sent folder"
		fi
		fi
	done
}

send()
{
	mail -s MBTemail $BTSEMAIL < $1
	ret=$?
	if [ $? -ne 0  ] ; then
		echo sending $1 FAILED
	else
		echo sending $1 OK
	fi
	return $ret
}


####-------------------------------------
### main()

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 bug report via mail, but there is no \"mail\" program
	echo in your system.
	exit
fi

while [ $# -gt 0 ] ; do
	case "$1" in 
		"--help" )
			usage; exit;	;;
		"--spool" )
			spool; exit;	;;
		"--send" )
			shift;
			[ $# -gt 0 ] || echo no file name given ; exit
			send $1	
			exit;	;;
		* )
			echo unknown option. See --help
			exit	;;
	esac
done

[ -d ~/.pldbug/spool ]     || mkdir -p ~/.pldbug/spool
[ -d ~/.pldbug/sent ]      || mkdir -p ~/.pldbug/sent
[ -d ~/.pldbug/canceled ]  || mkdir -p ~/.pldbug/canceled


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

NOTEMPTY=0
for i in ~/.pldbug/spool/* ; do
	if [ -e $i ] ; then
		NOTEMPTY=1
	fi
done
if [ "$NOTEMPTY" = "1" ] ; then
	eval `dml << EOF
There are some reports in spool directory.<br>
Should I try do send them now ?<br>
<button id="yes" caption="Yes, send them"><br>
<button id="no" caption="No, I will send them later">
EOF`
	if [ $yes = "yes" ] ; then

		pager=${PAGER-less}
		spool | $pager
	fi
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-linux.org<br>
If you don't have account please visit http://bugs.pld-linux.org 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 is 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

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


eval `dml <<EOF
Bug report is now complete. Are you sure tahat 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
	cp $tmpname.1 ~/.pldbug/canceled/`basename $tmpname.1`
	eval `dml "Bug report canceled<br>Bug report has been saved in ~/.pldbug/canceled/. You can send<br>it by moving this file to ~/.pldbug/spool/ and then run 'pldbug --spool'<br><button>"`
	exit 
fi




mail -s MBTemail $BTSEMAIL < $tmpname.1

if [ $? -ne 0 ] ; then
	cp $tmpname.1 ~/.pldbug/spool/`basename $tmpname.1`
	eval `dml << EOF
	Bug report sending failed. Your bug report has been spooled<br>
	in ~/.pldbug/spool/ . Please configure yours mailing system and then <br>
	run pldbug --spool<br><button>
EOF`

else

cp $tmpname.1 ~/.pldbug/sent/`basename $tmpname.1`
eval `dml <<EOF
Your bug report has ben send to PLD via mail. <br>
You should receive email with confirmation soon.<br>
Your report has been also saved in ~/.pldbug/sent/ directory<br>
Thanks.<br>
<button>
EOF`
fi
