#!/bin/bash

# replace this url with the address to your bugzilla, or possibly
# some other bug management system.
bugzillaurl='http://frodo/cgi-bin/bugzilla/show_bug.cgi?id='


cd "$1/db/transactions/$2"*
if [ -e props ]; then
	true
else
	echo "uh oh, there is no props file"
	exit 0
fi

svnlog=$(grep "^svn:log$" props)
if [ "$svnlog" == "" ]; then
	echo "uh oh, there is no svn:log property"
	exit 0
fi
findsvnproperties=$(grep -A1 "^svn:" props | tail -2)
isitsvnlog=$(echo $findsvnproperties | sed "s/^svn:log V [0-9]*//")
if [ "$isitsvnlog" != "" ]; then
	echo "uh oh, svnlog is not the last thing, we exit"
	exit 0
fi
# the codegroup above is to make sure that we can cut from the
# svn:log line to the END line in the props file.


newpropsfile=$(tempfile)
wheredoesthelogstart=$(grep -n "^svn:log$" props | cut -d":" -f1)
cat props | sed "$wheredoesthelogstart,\$ d" >> $newpropsfile
echo "svn:log" >> $newpropsfile
# The codegroup above copies the beginning of the old propsfile.


log=$(tempfile)
oldlogsize=$(echo $findsvnproperties | sed "s/^svn:log V //")
cat props | sed -e "1,$wheredoesthelogstart d" -e "$ d" | sed -e "1 d" -e "$ d" >> $log
if [ "$oldlogsize" -ne "$(wc -c $log | cut -d" " -f1)" ]; then
	echo "uh of, the specified logsize and the calculated logsize differs, we exit"
	exit 0
fi
# the code above is just an extra precausion to avoid changes if
# the logsize specified in the props file and the extracted logsize
# does not match. The reason is that if there is a match, the log
# was extracted right from the props file, but if the size differs,
# then the extraction of the log message was wrong, and we want to
# avoid trashing the props file
# This could possible catch future changes to the props file.


tf=$(tempfile)
echoornot="false"
for ord in $(cat $log | sed -e "s/[,|\.|#|*|+]/ /g"); do
        if [ "$echoornot" == "true" ]; then
                tal=$(echo $ord | tr -d 0-9)
                if [ "$tal" == "" ]; then
                        echo $bugzillaurl$ord >> $tf
                else
                        echoornot="false"
                fi
        fi
        bugornot=$(echo "$ord" | tr A-Z a-z)
        if [ "$bugornot" = "bug" ]; then
                echoornot="true"
        fi
done
anybugs=$(wc -l $tf | cut -d" " -f1)
if [ "$anybugs" -gt "0" ]; then
	echo "" >> $log
	echo "Automatic parsed bugzilla - MAY be incorrect:" >> $log
	cat $tf >> $log
fi
# the codegroup above adds bugzilla urls to the logmessage if
# any bug numbers was found. The code should catch any sort of
# specification with one or more bug numbers. The committer must
# write something along "fix of bug 83434" or "fixes bug #344834, 98434"
# it does NOT work if one writes the word "and" between the 2
# bug numbers.


newlogsize=$(wc -c $log | cut -d" " -f1)
echo "V $newlogsize" >> $newpropsfile
# now we calculate the new logmessage size and update it in the
# props file. The reason is that since the size is in the file,
# someone might need it for other scripts, or possibly subversion
# itself checks if the size and the log message fits.


cat $log >> $newpropsfile
echo "" >> $newpropsfile
echo "END" >> $newpropsfile
cat $newpropsfile > props
rm $newpropsfile $tf $log

exit 0


