1#! /bin/sh
2# Submit a problem report to a GNATS site.
3# Copyright (C) 1993 Free Software Foundation, Inc.
4# Contributed by Brendan Kehoe (brendan@cygnus.com), based on a
5# version written by Heinz G. Seidl (hgs@ide.com).
6#
7# This file is part of GNU GNATS.
8# Modified by Berliner for CVS.
9#
10# GNU GNATS is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2, or (at your option)
13# any later version.
14#
15# GNU GNATS is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU General Public License for more details.
19
20# The version of this send-pr.
21VERSION=3.2
22
23# The submitter-id for your site.
24SUBMITTER=net
25
26## # Where the GNATS directory lives, if at all.
27## [ -z "$GNATS_ROOT" ] && 
28## GNATS_ROOT=/usr/local/lib/gnats/gnats-db
29
30# The default mail address for PR submissions. 
31GNATS_ADDR=@PACKAGE_BUGREPORT@
32
33## # Where the gnats category tree lives.
34## DATADIR=/usr/local/lib
35
36## # If we've been moved around, try using GCC_EXEC_PREFIX.
37## [ ! -d $DATADIR/gnats -a -d "$GCC_EXEC_PREFIX" ] && DATADIR=${GCC_EXEC_PREFIX}..
38
39# The default release for this host.
40DEFAULT_RELEASE="@VERSION@"
41
42# The default organization.
43DEFAULT_ORGANIZATION="net"
44
45## # The default site to look for.
46## GNATS_SITE=unknown
47
48## # Newer config information?
49## [ -f ${GNATS_ROOT}/gnats-adm/config ] && . ${GNATS_ROOT}/gnats-adm/config
50
51# Hack mktemp on systems that don't have it.
52@MKTEMP_SH_FUNCTION@
53MKTEMP="@MKTEMP@"
54
55# What mailer to use.  This must come after the config file, since it is
56# host-dependent.
57SENDMAIL="@SENDMAIL@"
58MAIL_AGENT="$SENDMAIL -oi -t"
59MAILER=`echo $MAIL_AGENT | sed -e 's, .*,,'`
60if [ ! -f "$MAILER" ] ; then
61	echo "$COMMAND: Cannot find mail program \"$MAILER\"."
62	echo "$COMMAND: Please fix the MAIL_AGENT entry in the $COMMAND file."
63	exit 1
64fi
65
66if test "`echo -n foo`" = foo ; then
67  ECHON=bsd
68elif test "`echo 'foo\c'`" = foo ; then
69  ECHON=sysv
70else
71  ECHON=none
72fi
73
74if [ $ECHON = bsd ] ; then
75  ECHON1="echo -n"
76  ECHON2=
77elif [ $ECHON = sysv ] ; then
78  ECHON1=echo
79  ECHON2='\c'
80else
81  ECHON1=echo
82  ECHON2=
83fi
84
85#
86
87[ -z "$TMPDIR" ] && TMPDIR=/tmp
88
89TEMP="`$MKTEMP $TMPDIR/p.XXXXXX`"
90BAD="`$MKTEMP $TMPDIR/pbad.XXXXXX`"
91REF="`$MKTEMP $TMPDIR/pf.XXXXXX`"
92
93if [ -z "$LOGNAME" -a -n "$USER" ]; then
94  LOGNAME=$USER
95fi
96
97FROM="$LOGNAME"
98REPLY_TO="$LOGNAME"
99
100# Find out the name of the originator of this PR.
101if [ -n "$NAME" ]; then
102  ORIGINATOR="$NAME"
103elif [ -f $HOME/.fullname ]; then
104  ORIGINATOR="`sed -e '1q' $HOME/.fullname`"
105elif [ -f /bin/domainname ]; then
106  if [ "`/bin/domainname`" != "" -a -f /usr/bin/ypcat ]; then
107    # Must use temp file due to incompatibilities in quoting behavior
108    # and to protect shell metacharacters in the expansion of $LOGNAME
109    /usr/bin/ypcat passwd 2>/dev/null | cat - /etc/passwd | grep "^$LOGNAME:" |
110      cut -f5 -d':' | sed -e 's/,.*//' > $TEMP
111    ORIGINATOR="`cat $TEMP`"
112  fi
113fi
114
115if [ "$ORIGINATOR" = "" ]; then
116  grep "^$LOGNAME:" /etc/passwd | cut -f5 -d':' | sed -e 's/,.*//' > $TEMP
117  ORIGINATOR="`cat $TEMP`"
118fi
119
120if [ -n "$ORGANIZATION" ]; then
121  if [ -f "$ORGANIZATION" ]; then
122    ORGANIZATION="`cat $ORGANIZATION`"
123  fi
124else
125  if [ -n "$DEFAULT_ORGANIZATION" ]; then
126    ORGANIZATION="$DEFAULT_ORGANIZATION"
127  elif [ -f $HOME/.organization ]; then
128    ORGANIZATION="`cat $HOME/.organization`"
129  elif [ -f $HOME/.signature ]; then
130    ORGANIZATION="`cat $HOME/.signature`"
131  fi
132fi
133
134# If they don't have a preferred editor set, then use
135if [ -z "$VISUAL" ]; then
136  if [ -z "$EDITOR" ]; then
137    EDIT=vi
138  else
139    EDIT="$EDITOR"
140  fi
141else
142  EDIT="$VISUAL"
143fi
144
145# Find out some information.
146SYSTEM=`( [ -f /bin/uname ] && /bin/uname -a ) || \
147        ( [ -f /usr/bin/uname ] && /usr/bin/uname -a ) || echo ""`
148ARCH=`[ -f /bin/arch ] && /bin/arch`
149MACHINE=`[ -f /bin/machine ] && /bin/machine`
150
151COMMAND=`echo $0 | sed -e 's,.*/,,'`
152## USAGE="Usage: $COMMAND [-PVL] [-t address] [-f filename] [--request-id] 
153USAGE="Usage: $COMMAND [-PVL]
154[--version]"
155REMOVE=
156BATCH=
157
158while [ $# -gt 0 ]; do
159  case "$1" in
160    -r) ;; 		# Ignore for backward compat.
161##     -t | --to) if [ $# -eq 1 ]; then echo "$USAGE"; exit 1; fi
162## 	shift ; GNATS_ADDR="$1"
163## 	EXPLICIT_GNATS_ADDR=true
164##         ;;
165##     -f | --file) if [ $# -eq 1 ]; then echo "$USAGE"; exit 1; fi
166## 	shift ; IN_FILE="$1"
167## 	if [ "$IN_FILE" != "-" -a ! -r "$IN_FILE" ]; then
168## 	  echo "$COMMAND: cannot read $IN_FILE"
169## 	  exit 1
170## 	fi
171## 	;;
172    -b | --batch) BATCH=true ;;
173    -p | -P | --print) PRINT=true ;;
174    -L | --list) FORMAT=norm ;;
175    -l | -CL | --lisp) FORMAT=lisp ;;
176##     --request-id) REQUEST_ID=true ;;
177    -h | --help) echo "$USAGE"; exit 0 ;;
178    -V | --version) echo "$VERSION"; exit 0 ;;
179    -*) echo "$USAGE" ; exit 1 ;;
180    *) echo "$USAGE" ; exit 1
181##        if [ -z "$USER_GNATS_SITE" ]; then
182## 	 if [ ! -r "$DATADIR/gnats/$1" ]; then
183## 	   echo "$COMMAND: the GNATS site $1 does not have a categories list."
184## 	   exit 1
185## 	 else
186## 	   # The site name is the alias they'll have to have created.
187## 	   USER_GNATS_SITE=$1
188## 	 fi
189##        else
190## 	 echo "$USAGE" ; exit 1
191##        fi
192       ;;
193 esac
194 shift
195done
196
197if [ -n "$USER_GNATS_SITE" ]; then
198  GNATS_SITE=$USER_GNATS_SITE
199  GNATS_ADDR=$USER_GNATS_SITE-gnats
200fi
201
202if [ "$SUBMITTER" = "unknown" -a -z "$REQUEST_ID" -a -z "$IN_FILE" ]; then
203  cat << '__EOF__'
204It seems that send-pr is not installed with your unique submitter-id.
205You need to run
206
207          install-sid YOUR-SID
208
209where YOUR-SID is the identification code you received with `send-pr'.
210`send-pr' will automatically insert this value into the template field
211`>Submitter-Id'.  If you've downloaded `send-pr' from the Net, use `net'
212for this value.  If you do not know your id, run `send-pr --request-id' to 
213get one from your support site.
214__EOF__
215  exit 1
216fi
217
218## if [ -r "$DATADIR/gnats/$GNATS_SITE" ]; then
219##   CATEGORIES=`grep -v '^#' $DATADIR/gnats/$GNATS_SITE | sort`
220## else
221##   echo "$COMMAND: could not read $DATADIR/gnats/$GNATS_SITE for categories list."
222##   exit 1
223## fi
224CATEGORIES="contrib cvs doc pcl-cvs portability"
225
226if [ -z "$CATEGORIES" ]; then
227  echo "$COMMAND: the categories list for $GNATS_SITE was empty!"
228  exit 1
229fi
230
231case "$FORMAT" in
232  lisp) echo "$CATEGORIES" | \
233        awk 'BEGIN {printf "( "}
234	     {printf "(\"%s\") ",$0}
235	     END {printf ")\n"}'
236        exit 0
237        ;;
238  norm) l=`echo "$CATEGORIES" | \
239	awk 'BEGIN {max = 0; }
240	     { if (length($0) > max) { max = length($0); } }
241	     END {print max + 1;}'`
242	c=`expr 70 / $l`
243	if [ $c -eq 0 ]; then c=1; fi
244	echo "$CATEGORIES" | \
245        awk 'BEGIN {print "Known categories:"; i = 0 }
246          { printf ("%-'$l'.'$l's", $0); if ((++i % '$c') == 0) { print "" } }
247            END { print ""; }'
248        exit 0
249        ;;
250esac
251
252ORIGINATOR_C='<name of the PR author (one line)>'
253ORGANIZATION_C='<organization of PR author (multiple lines)>'
254CONFIDENTIAL_C='<[ yes | no ] (one line)>'
255SYNOPSIS_C='<synopsis of the problem (one line)>'
256SEVERITY_C='<[ non-critical | serious | critical ] (one line)>'
257PRIORITY_C='<[ low | medium | high ] (one line)>'
258CATEGORY_C='<name of the product (one line)>'
259CLASS_C='<[ sw-bug | doc-bug | change-request | support ] (one line)>'
260RELEASE_C='<release number or tag (one line)>'
261ENVIRONMENT_C='<machine, os, target, libraries (multiple lines)>'
262DESCRIPTION_C='<precise description of the problem (multiple lines)>'
263HOW_TO_REPEAT_C='<code/input/activities to reproduce the problem (multiple lines)>'
264FIX_C='<how to correct or work around the problem, if known (multiple lines)>'
265
266# Catch some signals. ($xs kludge needed by Sun /bin/sh)
267xs=0
268trap 'rm -f $REF $TEMP; exit $xs' 0
269trap 'echo "$COMMAND: Aborting ..."; rm -f $REF $TEMP; xs=1; exit' 1 2 3 13 15
270
271# If they told us to use a specific file, then do so.
272if [ -n "$IN_FILE" ]; then
273  if [ "$IN_FILE" = "-" ]; then
274    # The PR is coming from the standard input.
275    if [ -n "$EXPLICIT_GNATS_ADDR" ]; then
276      sed -e "s;^[Tt][Oo]:.*;To: $GNATS_ADDR;" > $TEMP
277    else
278      cat > $TEMP
279    fi
280  else
281    # Use the file they named.
282    if [ -n "$EXPLICIT_GNATS_ADDR" ]; then
283      sed -e "s;^[Tt][Oo]:.*;To: $GNATS_ADDR;" $IN_FILE > $TEMP
284    else
285      cat $IN_FILE > $TEMP
286    fi
287  fi
288else
289
290  if [ -n "$PR_FORM" -a -z "$PRINT_INTERN" ]; then
291    # If their PR_FORM points to a bogus entry, then bail.
292    if [ ! -f "$PR_FORM" -o ! -r "$PR_FORM" -o ! -s "$PR_FORM" ]; then
293      echo "$COMMAND: can't seem to read your template file (\`$PR_FORM'), ignoring PR_FORM"
294      sleep 1
295      PRINT_INTERN=bad_prform
296    fi
297  fi
298
299  if [ -n "$PR_FORM" -a -z "$PRINT_INTERN" ]; then
300    cp $PR_FORM $TEMP || 
301      ( echo "$COMMAND: could not copy $PR_FORM" ; xs=1; exit )
302  else
303    for file in $TEMP $REF ; do
304      cat  > $file << '__EOF__'
305SEND-PR: -*- send-pr -*-
306SEND-PR: Lines starting with `SEND-PR' will be removed automatically, as
307SEND-PR: will all comments (text enclosed in `<' and `>').
308SEND-PR: 
309SEND-PR: Choose from the following categories:
310SEND-PR:
311__EOF__
312
313      # Format the categories so they fit onto lines.
314	l=`echo "$CATEGORIES" | \
315	awk 'BEGIN {max = 0; }
316	     { if (length($0) > max) { max = length($0); } }
317	     END {print max + 1;}'`
318	c=`expr 61 / $l`
319	if [ $c -eq 0 ]; then c=1; fi
320	echo "$CATEGORIES" | \
321        awk 'BEGIN {printf "SEND-PR: "; i = 0 }
322          { printf ("%-'$l'.'$l's", $0);
323	    if ((++i % '$c') == 0) { printf "\nSEND-PR: " } }
324            END { printf "\nSEND-PR:\n"; }' >> $file
325
326      cat >> $file << __EOF__
327To: $GNATS_ADDR
328Subject: 
329From: $FROM
330Reply-To: $REPLY_TO
331X-send-pr-version: $VERSION
332
333
334>Submitter-Id:   $SUBMITTER
335>Originator: 	 $ORIGINATOR
336>Organization:
337${ORGANIZATION-$ORGANIZATION_C}
338>Confidential:  $CONFIDENTIAL_C
339>Synopsis:	$SYNOPSIS_C
340>Severity:	$SEVERITY_C
341>Priority:	$PRIORITY_C
342>Category: 	$CATEGORY_C
343>Class:		$CLASS_C
344>Release:	${DEFAULT_RELEASE-$RELEASE_C}
345>Environment:
346    	$ENVIRONMENT_C
347`[ -n "$SYSTEM" ] && echo System: $SYSTEM`
348`[ -n "$ARCH" ] && echo Architecture: $ARCH`
349`[ -n "$MACHINE" ] && echo Machine: $MACHINE`
350>Description:
351	$DESCRIPTION_C
352>How-To-Repeat:
353	$HOW_TO_REPEAT_C
354>Fix:
355	$FIX_C
356__EOF__
357    done
358  fi
359
360  if [ "$PRINT" = true -o "$PRINT_INTERN" = true ]; then
361    cat $TEMP
362    xs=0; exit
363  fi
364
365  chmod u+w $TEMP
366  if [ -z "$REQUEST_ID" ]; then
367    eval $EDIT $TEMP
368  else
369    ed -s $TEMP << '__EOF__'
370/^Subject/s/^Subject:.*/Subject: request for a customer id/
371/^>Category/s/^>Category:.*/>Category: send-pr/
372w
373q
374__EOF__
375  fi
376
377  if cmp -s $REF $TEMP ; then
378    echo "$COMMAND: problem report not filled out, therefore not sent"
379    xs=1; exit
380  fi
381fi
382
383#
384#	Check the enumeration fields
385
386# This is a "sed-subroutine" with one keyword parameter 
387# (with workaround for Sun sed bug)
388#
389SED_CMD='
390/$PATTERN/{
391s|||
392s|<.*>||
393s|^[ 	]*||
394s|[ 	]*$||
395p
396q
397}'
398
399
400while [ -z "$REQUEST_ID" ]; do
401  CNT=0
402
403  # 1) Confidential
404  #
405  PATTERN=">Confidential:"
406  CONFIDENTIAL=`eval sed -n -e "\"$SED_CMD\"" $TEMP`
407  case "$CONFIDENTIAL" in
408    ""|yes|no) CNT=`expr $CNT + 1` ;;
409    *) echo "$COMMAND: \`$CONFIDENTIAL' is not a valid value for \`Confidential'." ;;
410  esac
411  #
412  # 2) Severity
413  #
414  PATTERN=">Severity:"
415  SEVERITY=`eval sed -n -e "\"$SED_CMD\"" $TEMP`
416  case "$SEVERITY" in
417    ""|non-critical|serious|critical) CNT=`expr $CNT + 1` ;;
418    *)  echo "$COMMAND: \`$SEVERITY' is not a valid value for \`Severity'."
419  esac
420  #
421  # 3) Priority
422  #
423  PATTERN=">Priority:"
424  PRIORITY=`eval sed -n -e "\"$SED_CMD\"" $TEMP`
425  case "$PRIORITY" in
426    ""|low|medium|high) CNT=`expr $CNT + 1` ;;
427    *)  echo "$COMMAND: \`$PRIORITY' is not a valid value for \`Priority'."
428  esac
429  #
430  # 4) Category
431  #
432  PATTERN=">Category:"
433  CATEGORY=`eval sed -n -e "\"$SED_CMD\"" $TEMP`
434  FOUND=
435  for C in $CATEGORIES
436  do
437    if [ "$C" = "$CATEGORY" ]; then FOUND=true ; break ; fi
438  done
439  if [ -n "$FOUND" ]; then
440    CNT=`expr $CNT + 1`	
441  else
442    if [ -z "$CATEGORY" ]; then
443      echo "$COMMAND: you must include a Category: field in your report."
444    else
445      echo "$COMMAND: \`$CATEGORY' is not a known category."
446    fi
447  fi
448  #
449  # 5) Class
450  #
451  PATTERN=">Class:"
452  CLASS=`eval sed -n -e "\"$SED_CMD\"" $TEMP`
453  case "$CLASS" in
454    ""|sw-bug|doc-bug|change-request|support) CNT=`expr $CNT + 1` ;;
455    *)  echo "$COMMAND: \`$CLASS' is not a valid value for \`Class'."
456  esac
457
458  [ $CNT -lt 5 -a -z "$BATCH" ] && 
459    echo "Errors were found with the problem report."
460
461  while true; do
462    if [ -z "$BATCH" ]; then
463      $ECHON1 "a)bort, e)dit or s)end? $ECHON2"
464      read input
465    else
466      if [ $CNT -eq 5 ]; then
467        input=s
468      else
469        input=a
470      fi
471    fi
472    case "$input" in
473      a*)
474	if [ -z "$BATCH" ]; then
475	  echo "$COMMAND: the problem report remains in $BAD and is not sent."
476	  mv $TEMP $BAD
477        else
478	  echo "$COMMAND: the problem report is not sent."
479	fi
480	xs=1; exit
481	;;
482      e*)
483        eval $EDIT $TEMP
484	continue 2
485	;;
486      s*)
487	break 2
488	;;
489    esac
490  done
491done
492#
493#	Remove comments and send the problem report
494#	(we have to use patterns, where the comment contains regex chars)
495#
496# /^>Originator:/s;$ORIGINATOR;;
497sed  -e "
498/^SEND-PR:/d
499/^>Organization:/,/^>[A-Za-z-]*:/s;$ORGANIZATION_C;;
500/^>Confidential:/s;<.*>;;
501/^>Synopsis:/s;$SYNOPSIS_C;;
502/^>Severity:/s;<.*>;;
503/^>Priority:/s;<.*>;;
504/^>Category:/s;$CATEGORY_C;;
505/^>Class:/s;<.*>;;
506/^>Release:/,/^>[A-Za-z-]*:/s;$RELEASE_C;;
507/^>Environment:/,/^>[A-Za-z-]*:/s;$ENVIRONMENT_C;;
508/^>Description:/,/^>[A-Za-z-]*:/s;$DESCRIPTION_C;;
509/^>How-To-Repeat:/,/^>[A-Za-z-]*:/s;$HOW_TO_REPEAT_C;;
510/^>Fix:/,/^>[A-Za-z-]*:/s;$FIX_C;;
511" $TEMP > $REF
512
513if $MAIL_AGENT < $REF; then
514  echo "$COMMAND: problem report sent"
515  xs=0; exit
516else
517  echo "$COMMAND: mysterious mail failure."
518  if [ -z "$BATCH" ]; then
519    echo "$COMMAND: the problem report remains in $BAD and is not sent."
520    mv $REF $BAD
521  else
522    echo "$COMMAND: the problem report is not sent."
523  fi
524  xs=1; exit
525fi
526