#!/bin/sh
#
# Copyright (c) 2002 Michal Moskal <malekith@pld-linux.org>.
# 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 Michal Moskal.
# 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 MICHAL MOSKAL 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.
#
# $Id: ui-bugreport,v 1.3 2002/07/29 11:15:59 malekith Exp $
#

SERVER_ADDRESS="193.219.28.21 17419"

export PATH=.:..:$PATH
add_title="_Bug _Report"
. installer-functions
. ui-functions

dial <<EOF
$(nls "This program will let you write and supply bug report to us.")<br>
$(nls "Bug report will be sent to our server, and immediately to")<br>
$(nls "pld-installer@pld.org.pl and to Cc:'ed to address you will")<br>
$(nls "supply.")<br>
<br>
$(nls "Alternatively if you don't have network connection available,")<br>
$(nls "or simply don't want to send it now, you can save the bug")<br>
$(nls "report to diskette and send it later.")<br>
<br>
$(nls "Bug report include all configuration of installer and log files.")<br>
$(nls "pld-installer@pld.org.pl is public mailing list, with archives")<br>
$(nls "available, so if you have some confidential information in")<br>
$(nls "installer config files, consider removing it, before sending")<br>
$(nls "bug report. (You'll be given chance to edit it).")<br>
<br>
$(nls "You can press Ctrl-C at any time to abandon creation of bug report.")
<br>
<br>
                          <button>
EOF

dial <<EOF
$(nls "This bootdisk contains e3 editor, that can emulate few")<br>
$(nls "other editors (to some limited extent of course).")<br>
$(nls "Which emulation would you like to use?")<br><br>
<menu id=editor res=1>
<item id=vi>_Vi
<item id=emacs>_Emacs
<item id=pico>_Pico
<item id=ne>_Ne
<item id=ws>_WordStar
</menu>
EOF

case $editor in
  vi ) 
    exit="<ESC>:q<ENTER>"
    ;;
  emacs )
    exit="Ctrl-X Ctrl-C"
    ;;
  ne )
    exit="Ctrl-Q"
    ;;
  pico )
    exit="Ctrl-X"
    ;;
  ws )
    exit="Ctrl-K X"
    ;;
esac

(
cat <<EOF
BUG: ----------------------------------------------------------------------
BUG: $(nls "Please write your bug report here (i.e. what happened, that you think")
BUG: $(nls "is a bug).")
BUG: $(nls "Lines starting with BUG: are automatically removed from the message.")
BUG: $(nls "In order to leave this editor (you'll be asked if you want to save")
BUG: $(nls "the file) press %s." "$exit")
BUG: ----------------------------------------------------------------------




BUG: $(nls "Don't forget to write your email somewhere here, in case we'll")
BUG: $(nls "have to contact you!")


BUG: ----------------------------------------------------------------------
BUG: $(nls "Below is contest of few configuration files, log files and output")
BUG: $(nls "of few commands. Please check if they doesn't contain confidential")
BUG: $(nls "information. OTOH: this files will probably help us diagnose your")
BUG: $(nls "problem.")
BUG: ----------------------------------------------------------------------
EOF

for f in /etc/installer.conf /etc/installer.sysconf /etc/installer.pkgs \
	 /var/log/installer /var/log/messages ; do
  echo
  echo
  echo
  echo
  echo "================================================================="
  echo "=== $f"
  echo "================================================================="
  
  cat $f

  echo "=== EOF $f"
done

for cmd in dmesg ; do
  echo
  echo
  echo
  echo
  echo "================================================================="
  echo "=== \`$cmd\`"
  echo "================================================================="
  
  $cmd

  echo "=== EOF \`$cmd\`"
done
) > /tmp/bug.txt

old_md5=$(md5sum /tmp/bug.txt | (read sum file; echo $sum) )
$editor /tmp/bug.txt

if [ $(md5sum /tmp/bug.txt | (read sum file; echo $sum) ) = $old_md5 ] ; then
  dial <<EOF
$(nls "You havn't changed the bug report (or didn't save it). Aborting.")
<br><br>
<button>
EOF
  exit 1
fi

rm -f /tmp/bug.txt.orig

net=$(nls "<br>Note: You don't seem to have network configured,<br>so sending bug report is not likely to work.<br>")

if test -x /sbin/ip ; then
  if [ "X$(/sbin/ip r)" != X ] ; then
    net=
  fi
fi

dial <<EOF
$(nls "Now, after you have edited your bug report you can")<br>
$(nls "send it, save it or drop it.")<br>
$net<br>
<menu id=method res=1>
<item id=save>$(nls "_Save it to diskette")
<item id=send>$(nls "_Send it to us!")
<item id=cancel>$(nls "_Abandon")
</menu>
EOF

ask_for_floppy () {
}

save_report () {
  cp /tmp/bug.txt /mnt
  cp_res=$?
  if umount /mnt && [ $cp_res = 0 ] ; then
    dial <<EOF
$(nls "Bug report saved to file %s. Please send it to" bug.txt)<br>
$(nls "feedback@pld.org.pl, or directly to pld-installer@pld.org.pl")<br>
$(nls "if you are subscribed there. Thank you!")<br>
<br>
            <button>
EOF
    return 0
  else
    dial <<EOF
$(nls "Problems saving bug report.")<br>
<br>
        <button>
EOF
    return 1
  fi
}

ask_and_save () {
  dial <<EOF
$(nls "Please insert diskette in drive and press")<br>
$(nls "enter when ready.")<br><br>
       <button>
EOF
  if mount -t vfat /dev/fd0 /mnt ; then
    :
  else
    dial <<EOF
$(nls "Cannot mount floppy. Bug report is in %s." /tmp/bug.txt)
<br><br>
         <button>
EOF
    exit 1
  fi
  
  if save_report ; then
    : ok
  else
    dial <<EOF
$(nls "Problems saving bug report.")<br>
$(nls "Bug report in file '%s'." /tmp/bug.txt)<br>
<br>
       <button>
EOF
  fi
}

save () {
  # try saving without asking
  if mount -t vfat /dev/fd0 /mnt ; then
    if save_report ; then
      : ok
    else
      # but if saving fails:
      ask_and_save
    fi
  else
    # but if mouting fails:
    ask_and_save
  fi
}

send () {
  dial <<EOF
$(nls "Please enter your address here, if you would like")<br>
$(nls "to be sent carbon copy (cc:) of bug report message.")<br>
$(nls "Otherwise leave it blank.")<br><br>
$(nls "Cc:") <input id=cc><br>
<button>
EOF
  if (echo "$cc"; cat /tmp/bug.txt) | gzip | nc $SERVER_ADDRESS ; then
    dial <<EOF
$(nls "Message sent. Thank you!")<br>
     <button>
EOF
  else
    if yes_no yes "$(nls "I was unable to send this bug report.<br>Would you like to save it to diskette<br>to send it later?")" ; then
      save
    fi
  fi
}

case $method in
  save )
    save
    ;;
    
  send )
    send
    ;;
esac

