#!/bin/sh
# refdb-bug - collects information about the RefDB installation and
# writes a report in the file refdb-bug.txt in the current working directory
# Markus Hoenicka <markus@mhoenicka.de> 011114

# output goes to this file. This unflexible hardwired approach was chosen
# deliberately to allow users to file a bug report without the foggiest
# notion of Unix concepts like redirection.
outfile="refdb-bug.tmp"
reportfile="refdb-bug.txt"
sysconfdir="/etc/refdb"
REFDBLIB="/usr/share/refdb"

# which which should we use?
which which 2>&1 > /dev/null
if [ $? = 1 ]; then
    mywhich="type -a"
else
    mywhich="which"
fi

uname -a > $outfile
echo "RefDB binaries and scripts in the path:" >> $outfile
$mywhich refdbd 2>&1 >> $outfile
$mywhich refdba 2>&1 >> $outfile
$mywhich refdbc 2>&1 >> $outfile
$mywhich refdbib 2>&1 >> $outfile
$mywhich med2ris 2>&1 >> $outfile
$mywhich marc2ris 2>&1 >> $outfile
$mywhich bib2ris 2>&1 >> $outfile

echo "and their version numbers:" >> $outfile
refdbd -v 2>&1 | grep refdb | sed 's/^\(ref[^ ]* [^ ]*\) .*/\1/' >> $outfile
refdba -v 2>&1 | grep refdb | sed 's/^\(ref[^ ]* [^ ]*\) .*/\1/' >> $outfile
refdbc -v 2>&1 | grep refdb | sed 's/^\(ref[^ ]* [^ ]*\) .*/\1/' >> $outfile
refdbib -v 2>&1 | grep refdb | sed 's/^\(ref[^ ]* [^ ]*\) .*/\1/' >> $outfile
bib2ris -v 2>&1 | grep bib2ris | sed 's/^\(bib[^ ]* [^ ]*\) .*/\1/' >> $outfile 

echo "DSSSL engines in the path:" >> $outfile
$mywhich jade 2>&1 >> $outfile
$mywhich openjade 2>&1 >> $outfile

echo "and their version numbers:" >> $outfile
echo "test" | jade -v 2>&1 | grep version >> $outfile
echo "test" | openjade -v 2>&1 | grep version >> $outfile

echo >> $outfile
echo "\$REFDBLIB is "$REFDBLIB >> $outfile
echo "global refdbd config file:" >> $outfile
cat $sysconfdir/refdbdrc | grep "^[^#]" >> $outfile  2>&1
echo >> $outfile
echo "global refdba config file:" >> $outfile
cat $sysconfdir/refdbarc | grep "^[^#]" >> $outfile 2>&1
echo >> $outfile
echo "global refdbc config file:" >> $outfile
cat $sysconfdir/refdbcrc | grep "^[^#]" >> $outfile 2>&1
echo >> $outfile
echo "global refdbc config file (cgi):" >> $outfile
cat $sysconfdir/refdbcgirc | grep "^[^#]" >> $outfile 2>&1
echo >> $outfile
echo "global refdbib config file:" >> $outfile
cat $sysconfdir/refdbibrc | grep "^[^#]" >> $outfile 2>&1
echo >> $outfile
echo "global bib2ris config file:" >> $outfile
cat $sysconfdir/bib2risrc | grep "^[^#]" >> $outfile 2>&1
echo >> $outfile
echo "\$HOME is "$HOME >> $outfile
echo "user refdba config file:" >> $outfile
cat $HOME/refdbarc | grep "^[^#]" >> $outfile 2>&1
echo >> $outfile
echo "user refdba config file (hidden):" >> $outfile
cat $HOME/.refdbarc | grep "^[^#]" >> $outfile 2>&1
echo >> $outfile
echo "user refdbc config file:" >> $outfile
cat $HOME/refdbcrc | grep "^[^#]" >> $outfile 2>&1
echo >> $outfile
echo "user refdbc config file (hidden):" >> $outfile
cat $HOME/.refdbcrc | grep "^[^#]" >> $outfile 2>&1
echo >> $outfile
echo "user refdbib config file:" >> $outfile
cat $HOME/refdbibrc | grep "^[^#]" >> $outfile 2>&1
echo >> $outfile
echo "user refdbib config file (hidden):" >> $outfile
cat $HOME/.refdbibrc | grep "^[^#]" >> $outfile 2>&1
echo >> $outfile
echo "user bib2ris config file:" >> $outfile
cat $HOME/bib2risrc | grep "^[^#]" >> $outfile 2>&1
echo >> $outfile
echo "user bib2ris config file (hidden):" >> $outfile
cat $HOME/.bib2risrc | grep "^[^#]" >> $outfile 2>&1
echo ""
echo "************************************************************" >&2
echo "WARNING: ./$reportfile might contain plain-text passwords" >&2
echo "Do you want me to protect these passwords from prying eyes?" >&2
echo "************************************************************" >&2
echo -n "[Y/n]" >&2
read STRIP

if [ "$STRIP" = "n" ] || [ "$STRIP" = "N" ];then
    mv $outfile $reportfile
else
# the square brackets contain a space and a tab
    sed 's/^\(passwd[ 	]*\).*/\1<protected>/' < $outfile > $reportfile && rm $outfile
fi

echo "************************************************************" >&2
echo "Output was written to ./$reportfile" >&2
echo "Please append this file to all bug reports" >&2
echo "************************************************************" >&2

