#! /bin/sh -
#
# Copyright (C) 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2004,
#               2005, 2006 John W. Eaton
#
# This file is part of Octave.
# 
# Octave 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 3 of the License, or (at
# your option) any later version.
# 
# Octave 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 Octave; see the file COPYING.  If not, see
# <http://www.gnu.org/licenses/>.

# octave-bug - create a bug report and mail it to the bug-octave
# mailing list.
#
# Patterned after the bashbug script from bash 1.14.

# Configuration:  these variables are filled in when running make to
# compile Octave.

config_opts="'--host=amd64-portbld-freebsd7.2' '--with-blas=-L/usr/local/lib -lblas' '--with-lapack=-llapack' '--enable-shared' '--prefix=/usr/local' '--mandir=/usr/local/man' '--infodir=/usr/local/info/' '--build=amd64-portbld-freebsd7.2' 'build_alias=amd64-portbld-freebsd7.2' 'host_alias=amd64-portbld-freebsd7.2' 'CC=gcc43' 'CFLAGS=-O2 -fno-strict-aliasing -pipe -I/usr/local/include -I/usr/local/include/metis' 'LDFLAGS= -L/usr/local/lib -pthread' 'CXX=g++43' 'CXXFLAGS=-O2 -fno-strict-aliasing -pipe -I/usr/local/include -I/usr/local/include/metis -I/usr/local/include -I/usr/local/include/metis' 'F77=gfortran43' 'FFLAGS=-O'"
VERSION="3.0.5"
SED="/usr/local/bin/gsed"
MACHINE="amd64-portbld-freebsd7.2"
F77="gfortran43"
FFLAGS="-O"
FPICFLAG="-fPIC"
FLIBS="-L/usr/local/lib -L/usr/local/lib/gcc-4.3.4/gcc/x86_64-portbld-freebsd7.2/4.3.4 -L/usr/local/lib/gcc-4.3.4/gcc/x86_64-portbld-freebsd7.2/4.3.4/../../.. -lhdf5 -lz -lgfortranbegin -lgfortran -lm -lpthread"
F2C="@F2C@"
F2CFLAGS="@F2CFLAGS@"
CPPFLAGS=""
INCFLAGS="-I. -I. -I./liboctave -I./src -I./libcruft/misc"
CC="gcc43"
CC_VERSION="4.3.4 20090405 (prerelease) (GCC) "
CFLAGS="-O2 -fno-strict-aliasing -pipe -I/usr/local/include -I/usr/local/include/metis"
CPICFLAG="-fPIC"
CXX="g++43"
CXX_VERSION="4.3.4"
CXXFLAGS="-O2 -fno-strict-aliasing -pipe -I/usr/local/include -I/usr/local/include/metis -I/usr/local/include -I/usr/local/include/metis"
CXXPICFLAG="-fPIC"
LD_CXX="g++43"
LDFLAGS="-L/usr/local/lib -pthread"
LIBFLAGS="-L."
RLD_FLAG="-Wl,-rpath -Wl,/usr/local/lib/octave-3.0.5"
LIBS="-lreadline  -lncurses -lhdf5 -lz -lm "
BLAS_LIBS="-llapack -L/usr/local/lib -lblas"
FFTW_LIBS="-lfftw3"
LEXLIB=""
LIBGLOB=""
DEFS="-DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DOCTAVE_SOURCE=1 -DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_UNISTD_H=1 -D__EXTENSIONS__=1 -D_ALL_SOURCE=1 -D_GNU_SOURCE=1 -D_POSIX_PTHREAD_SEMANTICS=1 -D_TANDEM_SOURCE=1 -D__EXTENSIONS__=1 -D_ALL_SOURCE=1 -D_GNU_SOURCE=1 -D_POSIX_PTHREAD_SEMANTICS=1 -D_TANDEM_SOURCE=1 -D__EXTENSIONS__=1 -D_ALL_SOURCE=1 -D_GNU_SOURCE=1 -D_POSIX_PTHREAD_SEMANTICS=1 -D_TANDEM_SOURCE=1 -DSEPCHAR=':' -DSEPCHAR_STR=\":\" -D__NO_MATH_INLINES=1 -DCXX_NEW_FRIEND_TEMPLATE_DECL=1 -DCXX_ISO_COMPLIANT_LIBRARY=1 -DCXX_ABI=unknown -DHAVE_LIBM=1 -DHAVE_QHULL=1 -DHAVE_REGEXEC=1 -DHAVE_REGEX=1 -DHAVE_REGEXEC=1 -DHAVE_ZLIB_H=1 -DHAVE_ZLIB=1 -DHAVE_HDF5_H=1 -DHAVE_HDF5=1 -DHAVE_H5GGET_NUM_OBJS=1 -DHAVE_FFTW3=1 -DHAVE_GLPK_H=1 -DHAVE_GLPK=1 -DHAVE_IEEE754_DATA_FORMAT=1 -DF77_FUNC(name,NAME)=name ## _ -DF77_FUNC_(name,NAME)=name ## _ -DHAVE_BLAS=1 -DHAVE_SUITESPARSE_UMFPACK_H=1 -DHAVE_UMFPACK=1 -DUMFPACK_SEPARATE_SPLIT=1 -DHAVE_SUITESPARSE_COLAMD_H=1 -DHAVE_COLAMD=1 -DHAVE_SUITESPARSE_CCOLAMD_H=1 -DHAVE_CCOLAMD=1 -DHAVE_SUITESPARSE_CHOLMOD_H=1 -DHAVE_CHOLMOD=1 -DHAVE_SUITESPARSE_CS_H=1 -DHAVE_CXSPARSE=1 -DHAVE_GETHOSTNAME=1 -DHAVE_GETPWNAM=1 -DHAVE_DEV_T=1 -DHAVE_INO_T=1 -DHAVE_NLINK_T=1 -DHAVE_NLINK_T=1 -DHAVE_LONG_LONG_INT=1 -DHAVE_UNSIGNED_LONG_LONG_INT=1 -DHAVE_SIGSET_T=1 -DHAVE_SIG_ATOMIC_T=1 -DSIZEOF_SHORT=2 -DSIZEOF_INT=4 -DSIZEOF_LONG=8 -DSIZEOF_LONG_LONG=8 -DHAVE_ALLOCA=1 -DNPOS=std::string::npos -DHAVE_PLACEMENT_DELETE=1 -DHAVE_DYNAMIC_AUTO_ARRAYS=1 -DSTDC_HEADERS=1 -DHAVE_DIRENT_H=1 -DTIME_WITH_SYS_TIME=1 -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INITE=1 -DHAVE_ISNAN=1 -DHAVE_ISINF=1 -DHAVE_COPYSIGN=1 -DHAVE_DECL_SIGNBIT=1 -DHAVE_ACOSH=1 -DHAVE_ASINH=1 -DHAVE_ATANH=1 -DHAVE_ERF=1 -DHAVE_ERFC=1 -DHAVE_EXP2=1 -DHAVE_STRUCT_STAT_ST_BLKSIZE=1 -DHAVE_STRUCT_STAT_ST_BLOCKS=1 -DHAVE_STRUCT_STAT_ST_RDEV=1 -DHAVE_STRUCT_TM_TM_ZONE=1 -DHAVE_TM_ZONE=1 -DUSE_READLINE=1 -DRETSIGTYPE=void -DHAVE_DECL_SYS_SIGLIST=1 -DHAVE_POSIX_SIGNALS=1 -DRETSIGTYPE_IS_VOID=1 -DHAVE_GETRUSAGE=1 -DHAVE_TIMES=1 -DYYTEXT_POINTER=1 -DGNUPLOT_BINARY=\"gnuplot\""

: ${USER=$LOGNAME}

CC_AND_VERSION=
if test -n "$CC_VERSION"; then
  CC_AND_VERSION="$CC, version $CC_VERSION"
fi

CXX_AND_VERSION=
if test -n "$CXX_VERSION"; then
  CXX_AND_VERSION="$CXX, version $CXX_VERSION"
fi

PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:$PATH
export PATH

TEMP=/tmp/octave-bug.$$

if [ -z "$DEFEDITOR" ] && [ -z "$EDITOR" ]; then
  if [ -x /usr/bin/editor ]; then
    DEFEDITOR=editor
  elif [ -x /usr/local/bin/ce ]; then
    DEFEDITOR=ce
  elif [ -x /usr/local/bin/emacs ]; then
    DEFEDITOR=emacs
  elif [ -x /usr/contrib/bin/emacs ]; then
    DEFEDITOR=emacs
  elif [ -x /usr/bin/emacs ]; then
    DEFEDITOR=emacs
  elif [ -x /usr/bin/xemacs ]; then
    DEFEDITOR=xemacs
  elif [ -x /usr/contrib/bin/jove ]; then
    DEFEDITOR=jove
  elif [ -x /usr/local/bin/jove ]; then
    DEFEDITOR=jove
  elif [ -x /usr/bin/vi ]; then
    DEFEDITOR=vi
  else
    echo "octave-bug: No default editor found: attempting to use vi" >&2
    DEFEDITOR=vi
  fi
fi

: ${EDITOR=$DEFEDITOR}

if [ -z "$DEFPAGER" ] && [ -z "$PAGER" ]; then
  if [ -x /usr/bin/pager ]; then
    DEFPAGER=pager
  elif [ -x /usr/bin/less ]; then
    DEFPAGER=less
  elif [ -x /bin/less ]; then
    DEFPAGER=less
  elif [ -x /usr/local/bin/less ]; then
    DEFPAGER=less
  elif [ -x /usr/bin/more ]; then
    DEFPAGER=more
  elif [ -x /bin/more ]; then
    DEFPAGER=more
  elif [ -x /usr/bin/pg ]; then
    DEFPAGER=pg
  elif [ -x /bin/pg ]; then
    DEFPAGER=pg
  else
    echo "octave-bug: No default pager found: attempting to use more" >&2
    DEFPAGER=more
  fi
fi

: ${PAGER=$DEFPAGER}

trap 'rm -f $TEMP $TEMP.x; exit 1' 1 2 3 13 15
trap 'rm -f $TEMP $TEMP.x' 0

UN=
if (uname) > /dev/null 2>&1; then
  UN=`uname -a`
fi

HAVE_FMT=false
if (fmt < /dev/null) > /dev/null 2>&1; then
  HAVE_FMT=true
fi

# Check whether to use -n or \c to keep echo from printing a newline
# character.  Stolen from autoconf, which borrowed the idea from dist 3.0.

if (echo "testing\c"; echo 1,2,3) | grep c >/dev/null; then
  # Stardent Vistra SVR4 grep lacks -e, says ghazi@caip.rutgers.edu.
  if (echo -n testing; echo 1,2,3) | $SED s/-n/xn/ | grep xn >/dev/null; then
    echo_n=
    echo_c='
'
  else
    echo_n=-n
    echo_c=
  fi
else
  echo_n=
  echo_c='\c'
fi

ss_p=`echo $VERSION | grep "^ss-"`
if test -n "$ss_p"; then
  BUGADDR="maintainers@octave.org"
else
  pretest_p=`echo $VERSION \
    | $SED 's,.*\.\([0-9]*\).*,\1,' \
    | grep -v '\.' \
    | grep '[0-9]'`

  if test -n "$pretest_p" && test "$pretest_p" -ge 90; then
    BUGADDR="maintainers@octave.org"
  else
    BUGADDR="bug@octave.org"
  fi
fi

SUBJECT="[50 character or so descriptive subject here (for reference)]"
if test $# -gt 0; then
  case "$1" in
    -s)
      shift
      if test $# -gt 0; then
        SUBJECT="$1"
        shift
      else
        echo "usage: octave-bug [-s subject]"
        exit 1
      fi
    ;;
  esac
fi

cat > $TEMP << EOF
To: $BUGADDR
EOF
if test -n "$USER"; then
cat >> $TEMP << EOF
Cc: $USER
EOF
fi
cat >> $TEMP << EOF
Subject: $SUBJECT
--------
Bug report for Octave $VERSION configured for $MACHINE

Description:
-----------

  * Please replace this item with a detailed description of the
    problem.  Suggestions or general comments are also welcome.

Repeat-By:
---------

  * Please replace this item with a description of the sequence of
    events that causes the problem to occur. 

Fix:
---

  * If possible, replace this item with a description of how to
    fix the problem (if you don't have a fix for the problem, don't
    include this section, but please do submit your report anyway).



Configuration (please do not edit this section):
-----------------------------------------------

uname output:     $UN
configure opts:   $config_opts
Fortran compiler: $F77
FFLAGS:           $FFLAGS
F2C:              $F2C
F2CFLAGS:         $F2CFLAGS
FLIBS:            $FLIBS
CPPFLAGS:         $CPPFLAGS
INCFLAGS:         $INCFLAGS
C compiler:       $CC_AND_VERSION
CFLAGS:           $CFLAGS
CPICFLAG:         $CPICFLAG
C++ compiler:     $CXX_AND_VERSION
CXXFLAGS:         $CXXFLAGS
CXXPICFLAG:       $CXXPICFLAG
LD_CXX:           $LD_CXX
LDFLAGS:          $LDFLAGS
LIBFLAGS:         $LIBFLAGS
RLD_FLAG:         $RLD_FLAG
BLAS_LIBS:        $BLAS_LIBS
FFTW_LIBS:        $FFTW_LIBS
LIBS:             $LIBS
LEXLIB:           $LEXLIB
LIBGLOB:          $LIBGLOB
SED:              $SED
DEFS:

EOF

if $HAVE_FMT; then
  echo $DEFS | fmt | $SED 's/^/  /' >> $TEMP
else
  echo $DEFS >> $TEMP
fi

if test $# -gt 0; then
  if test -f "$1"; then
    cat >> $TEMP << EOF

User-preferences (please do not edit this section):
--------------------------------------------------

EOF
    cat $1 >> $TEMP
  fi
fi

chmod u+w $TEMP
cp $TEMP $TEMP.x

status=0

editing=true

while $editing; do
  if $EDITOR $TEMP; then
    while $editing; do
      echo $echo_n "(a)bort, (e)dit, (l)ist, (s)end? $echo_c"
      read ans
      case "$ans" in
        a* | A*)
          status=1
          editing=false
        ;;
        e* | E*)
          break;
        ;;
        l* | L*)
          $PAGER $TEMP
        ;;
        s* | S*)
          editing=false
        ;;
      esac
    done
  else
    echo "problems with edit -- no bug report submitted"
    status=1
    editing=false
  fi
done

if test $status -eq 0; then
  if cmp -s $TEMP $TEMP.x; then
    echo "file not changed -- no bug report submitted"
    status=1
  elif test `wc $TEMP | awk '{print $1}'` -eq 0; then
    echo "empty bug report file -- not submitted"
    status=1
  else

# Try to extract the recipient address, in case the To: line in the
# message template has been changed.  Also get cc: lines.

    TO_ADDR=`$SED -e '/^--------[ \t]*$/q' $TEMP | $SED -n -e 's/^[Tt][Oo]://p'`
    CC_ADDR=`$SED -e '/^--------[ \t]*$/q' $TEMP | $SED -n -e 's/^[Cc][Cc]://p'`

    if test -z "$TO_ADDR"; then
      echo "no valid \`To:' field found in header -- using $BUGADDR instead"
    else
      BUGADDR="$TO_ADDR"      
    fi

    BUGADDR="$BUGADDR $CC_ADDR"

    TMP_SUB=`$SED -e '/^--------[ \t]*$/q' $TEMP | $SED -n -e 's/^Subject://p'`

    if test -n "$TMP_SUB"; then
      SUBJECT="$TMP_SUB"
    fi

# Delete the `--------' separator in the message.

# Don't pretty-print this.  Odd whitespace kills Ultrix AWK!

    awk 'BEGIN{in_header=1;} /^--------[ \t]*$/ {
      if (in_header) { in_header=0; print ""; next; }
    } { print $0; }' $TEMP > $TEMP.x

# Now try to mail it.

    # indicate that we have not yet sent email successfully
    status=11

    if test $status -ne 0; then
      ( mailx -s "$SUBJECT" $BUGADDR < $TEMP.x ) > /dev/null 2>&1
      status=$?
      if test $status -ne 0; then
        ( Mail -s "$SUBJECT" $BUGADDR < $TEMP.x ) > /dev/null 2>&1
        status=$?
        if test $status -ne 0; then
          ( /usr/ucb/mail -s "$SUBJECT" $BUGADDR < $TEMP.x ) > /dev/null 2>&1
          status=$?
          # make /bin/mail our last resort -- it ignores the subject line
          if test $status -ne 0; then
            ( /bin/mail $BUGADDR < $TEMP.x ) > /dev/null 2>&1
            status=$?
            if test $status -ne 0; then
              echo "unable to send mail..."
            fi
          fi
        fi
      fi
    fi
  fi
fi

if test $status -ne 0; then
  dead_bug_file=$HOME/dead-octave-bug
  looking_for_file=true;
  n=1
  while $looking_for_file; do
    if test -f "$dead_bug_file-$n"; then
      n=`expr $n + 1`
    else
      looking_for_file=false
      dead_bug_file=$dead_bug_file-$n
    fi
  done
  echo "saving message in $dead_bug_file";
  cat $TEMP >> $dead_bug_file;
  exit 1
else
  echo "bug report sent to: $TO_ADDR"
  echo "             cc to: $CC_ADDR"
fi

exit $status
