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    rm -f $TEMP
113  fi
114fi
115
116if [ "$ORIGINATOR" = "" ]; then
117  grep "^$LOGNAME:" /etc/passwd | cut -f5 -d':' | sed -e 's/,.*//' > $TEMP
118  ORIGINATOR="`cat $TEMP`"
119  rm -f $TEMP
120fi
121
122if [ -n "$ORGANIZATION" ]; then
123  if [ -f "$ORGANIZATION" ]; then
124    ORGANIZATION="`cat $ORGANIZATION`"
125  fi
126else
127  if [ -n "$DEFAULT_ORGANIZATION" ]; then
128    ORGANIZATION="$DEFAULT_ORGANIZATION"
129  elif [ -f $HOME/.organization ]; then
130    ORGANIZATION="`cat $HOME/.organization`"
131  elif [ -f $HOME/.signature ]; then
132    ORGANIZATION="`cat $HOME/.signature`"
133  fi
134fi
135
136# If they don't have a preferred editor set, then use
137if [ -z "$VISUAL" ]; then
138  if [ -z "$EDITOR" ]; then
139    EDIT=vi
140  else
141    EDIT="$EDITOR"
142  fi
143else
144  EDIT="$VISUAL"
145fi
146
147# Find out some information.
148SYSTEM=`( [ -f /bin/uname ] && /bin/uname -a ) || \
149        ( [ -f /usr/bin/uname ] && /usr/bin/uname -a ) || echo ""`
150ARCH=`[ -f /bin/arch ] && /bin/arch`
151MACHINE=`[ -f /bin/machine ] && /bin/machine`
152
153COMMAND=`echo $0 | sed -e 's,.*/,,'`
154## USAGE="Usage: $COMMAND [-PVL] [-t address] [-f filename] [--request-id] 
155USAGE="Usage: $COMMAND [-PVL]
156[--version]"
157REMOVE=
158BATCH=
159
160while [ $# -gt 0 ]; do
161  case "$1" in
162    -r) ;; 		# Ignore for backward compat.
163##     -t | --to) if [ $# -eq 1 ]; then echo "$USAGE"; exit 1; fi
164## 	shift ; GNATS_ADDR="$1"
165## 	EXPLICIT_GNATS_ADDR=true
166##         ;;
167##     -f | --file) if [ $# -eq 1 ]; then echo "$USAGE"; exit 1; fi
168## 	shift ; IN_FILE="$1"
169## 	if [ "$IN_FILE" != "-" -a ! -r "$IN_FILE" ]; then
170## 	  echo "$COMMAND: cannot read $IN_FILE"
171## 	  exit 1
172## 	fi
173## 	;;
174    -b | --batch) BATCH=true ;;
175    -p | -P | --print) PRINT=true ;;
176    -L | --list) FORMAT=norm ;;
177    -l | -CL | --lisp) FORMAT=lisp ;;
178##     --request-id) REQUEST_ID=true ;;
179    -h | --help) echo "$USAGE"; exit 0 ;;
180    -V | --version) echo "$VERSION"; exit 0 ;;
181    -*) echo "$USAGE" ; exit 1 ;;
182    *) echo "$USAGE" ; exit 1
183##        if [ -z "$USER_GNATS_SITE" ]; then
184## 	 if [ ! -r "$DATADIR/gnats/$1" ]; then
185## 	   echo "$COMMAND: the GNATS site $1 does not have a categories list."
186## 	   exit 1
187## 	 else
188## 	   # The site name is the alias they'll have to have created.
189## 	   USER_GNATS_SITE=$1
190## 	 fi
191##        else
192## 	 echo "$USAGE" ; exit 1
193##        fi
194       ;;
195 esac
196 shift
197done
198
199if [ -n "$USER_GNATS_SITE" ]; then
200  GNATS_SITE=$USER_GNATS_SITE
201  GNATS_ADDR=$USER_GNATS_SITE-gnats
202fi
203
204if [ "$SUBMITTER" = "unknown" -a -z "$REQUEST_ID" -a -z "$IN_FILE" ]; then
205  cat << '__EOF__'
206It seems that send-pr is not installed with your unique submitter-id.
207You need to run
208
209          install-sid YOUR-SID
210
211where YOUR-SID is the identification code you received with `send-pr'.
212`send-pr' will automatically insert this value into the template field
213`>Submitter-Id'.  If you've downloaded `send-pr' from the Net, use `net'
214for this value.  If you do not know your id, run `send-pr --request-id' to 
215get one from your support site.
216__EOF__
217  exit 1
218fi
219
220## if [ -r "$DATADIR/gnats/$GNATS_SITE" ]; then
221##   CATEGORIES=`grep -v '^#' $DATADIR/gnats/$GNATS_SITE | sort`
222## else
223##   echo "$COMMAND: could not read $DATADIR/gnats/$GNATS_SITE for categories list."
224##   exit 1
225## fi
226CATEGORIES="contrib cvs doc pcl-cvs portability"
227
228if [ -z "$CATEGORIES" ]; then
229  echo "$COMMAND: the categories list for $GNATS_SITE was empty!"
230  exit 1
231fi
232
233case "$FORMAT" in
234  lisp) echo "$CATEGORIES" | \
235        awk 'BEGIN {printf "( "}
236	     {printf "(\"%s\") ",$0}
237	     END {printf ")\n"}'
238        exit 0
239        ;;
240  norm) l=`echo "$CATEGORIES" | \
241	awk 'BEGIN {max = 0; }
242	     { if (length($0) > max) { max = length($0); } }
243	     END {print max + 1;}'`
244	c=`expr 70 / $l`
245	if [ $c -eq 0 ]; then c=1; fi
246	echo "$CATEGORIES" | \
247        awk 'BEGIN {print "Known categories:"; i = 0 }
248          { printf ("%-'$l'.'$l's", $0); if ((++i % '$c') == 0) { print "" } }
249            END { print ""; }'
250        exit 0
251        ;;
252esac
253
254ORIGINATOR_C='<name of the PR author (one line)>'
255ORGANIZATION_C='<organization of PR author (multiple lines)>'
256CONFIDENTIAL_C='<[ yes | no ] (one line)>'
257SYNOPSIS_C='<synopsis of the problem (one line)>'
258SEVERITY_C='<[ non-critical | serious | critical ] (one line)>'
259PRIORITY_C='<[ low | medium | high ] (one line)>'
260CATEGORY_C='<name of the product (one line)>'
261CLASS_C='<[ sw-bug | doc-bug | change-request | support ] (one line)>'
262RELEASE_C='<release number or tag (one line)>'
263ENVIRONMENT_C='<machine, os, target, libraries (multiple lines)>'
264DESCRIPTION_C='<precise description of the problem (multiple lines)>'
265HOW_TO_REPEAT_C='<code/input/activities to reproduce the problem (multiple lines)>'
266FIX_C='<how to correct or work around the problem, if known (multiple lines)>'
267
268# Catch some signals. ($xs kludge needed by Sun /bin/sh)
269xs=0
270trap 'rm -f $REF $TEMP; exit $xs' 0
271trap 'echo "$COMMAND: Aborting ..."; rm -f $REF $TEMP; xs=1; exit' 1 2 3 13 15
272
273# If they told us to use a specific file, then do so.
274if [ -n "$IN_FILE" ]; then
275  if [ "$IN_FILE" = "-" ]; then
276    # The PR is coming from the standard input.
277    if [ -n "$EXPLICIT_GNATS_ADDR" ]; then
278      sed -e "s;^[Tt][Oo]:.*;To: $GNATS_ADDR;" > $TEMP
279    else
280      cat > $TEMP
281    fi
282  else
283    # Use the file they named.
284    if [ -n "$EXPLICIT_GNATS_ADDR" ]; then
285      sed -e "s;^[Tt][Oo]:.*;To: $GNATS_ADDR;" $IN_FILE > $TEMP
286    else
287      cat $IN_FILE > $TEMP
288    fi
289  fi
290else
291
292  if [ -n "$PR_FORM" -a -z "$PRINT_INTERN" ]; then
293    # If their PR_FORM points to a bogus entry, then bail.
294    if [ ! -f "$PR_FORM" -o ! -r "$PR_FORM" -o ! -s "$PR_FORM" ]; then
295      echo "$COMMAND: can't seem to read your template file (\`$PR_FORM'), ignoring PR_FORM"
296      sleep 1
297      PRINT_INTERN=bad_prform
298    fi
299  fi
300
301  if [ -n "$PR_FORM" -a -z "$PRINT_INTERN" ]; then
302    cp $PR_FORM $TEMP || 
303      ( echo "$COMMAND: could not copy $PR_FORM" ; xs=1; exit )
304  else
305    for file in $TEMP $REF ; do
306      cat  > $file << '__EOF__'
307SEND-PR: -*- send-pr -*-
308SEND-PR: Lines starting with `SEND-PR' will be removed automatically, as
309SEND-PR: will all comments (text enclosed in `<' and `>').
310SEND-PR: 
311SEND-PR: Choose from the following categories:
312SEND-PR:
313__EOF__
314
315      # Format the categories so they fit onto lines.
316	l=`echo "$CATEGORIES" | \
317	awk 'BEGIN {max = 0; }
318	     { if (length($0) > max) { max = length($0); } }
319	     END {print max + 1;}'`
320	c=`expr 61 / $l`
321	if [ $c -eq 0 ]; then c=1; fi
322	echo "$CATEGORIES" | \
323        awk 'BEGIN {printf "SEND-PR: "; i = 0 }
324          { printf ("%-'$l'.'$l's", $0);
325	    if ((++i % '$c') == 0) { printf "\nSEND-PR: " } }
326            END { printf "\nSEND-PR:\n"; }' >> $file
327
328      cat >> $file << __EOF__
329To: $GNATS_ADDR
330Subject: 
331From: $FROM
332Reply-To: $REPLY_TO
333X-send-pr-version: $VERSION
334
335
336>Submitter-Id:   $SUBMITTER
337>Originator: 	 $ORIGINATOR
338>Organization:
339${ORGANIZATION-$ORGANIZATION_C}
340>Confidential:  $CONFIDENTIAL_C
341>Synopsis:	$SYNOPSIS_C
342>Severity:	$SEVERITY_C
343>Priority:	$PRIORITY_C
344>Category: 	$CATEGORY_C
345>Class:		$CLASS_C
346>Release:	${DEFAULT_RELEASE-$RELEASE_C}
347>Environment:
348    	$ENVIRONMENT_C
349`[ -n "$SYSTEM" ] && echo System: $SYSTEM`
350`[ -n "$ARCH" ] && echo Architecture: $ARCH`
351`[ -n "$MACHINE" ] && echo Machine: $MACHINE`
352>Description:
353	$DESCRIPTION_C
354>How-To-Repeat:
355	$HOW_TO_REPEAT_C
356>Fix:
357	$FIX_C
358__EOF__
359    done
360  fi
361
362  if [ "$PRINT" = true -o "$PRINT_INTERN" = true ]; then
363    cat $TEMP
364    xs=0; exit
365  fi
366
367  chmod u+w $TEMP
368  if [ -z "$REQUEST_ID" ]; then
369    eval $EDIT $TEMP
370  else
371    ed -s $TEMP << '__EOF__'
372/^Subject/s/^Subject:.*/Subject: request for a customer id/
373/^>Category/s/^>Category:.*/>Category: send-pr/
374w
375q
376__EOF__
377  fi
378
379  if cmp -s $REF $TEMP ; then
380    echo "$COMMAND: problem report not filled out, therefore not sent"
381    xs=1; exit
382  fi
383fi
384
385#
386#	Check the enumeration fields
387
388# This is a "sed-subroutine" with one keyword parameter 
389# (with workaround for Sun sed bug)
390#
391SED_CMD='
392/$PATTERN/{
393s|||
394s|<.*>||
395s|^[ 	]*||
396s|[ 	]*$||
397p
398q
399}'
400
401
402while [ -z "$REQUEST_ID" ]; do
403  CNT=0
404
405  # 1) Confidential
406  #
407  PATTERN=">Confidential:"
408  CONFIDENTIAL=`eval sed -n -e "\"$SED_CMD\"" $TEMP`
409  case "$CONFIDENTIAL" in
410    ""|yes|no) CNT=`expr $CNT + 1` ;;
411    *) echo "$COMMAND: \`$CONFIDENTIAL' is not a valid value for \`Confidential'." ;;
412  esac
413  #
414  # 2) Severity
415  #
416  PATTERN=">Severity:"
417  SEVERITY=`eval sed -n -e "\"$SED_CMD\"" $TEMP`
418  case "$SEVERITY" in
419    ""|non-critical|serious|critical) CNT=`expr $CNT + 1` ;;
420    *)  echo "$COMMAND: \`$SEVERITY' is not a valid value for \`Severity'."
421  esac
422  #
423  # 3) Priority
424  #
425  PATTERN=">Priority:"
426  PRIORITY=`eval sed -n -e "\"$SED_CMD\"" $TEMP`
427  case "$PRIORITY" in
428    ""|low|medium|high) CNT=`expr $CNT + 1` ;;
429    *)  echo "$COMMAND: \`$PRIORITY' is not a valid value for \`Priority'."
430  esac
431  #
432  # 4) Category
433  #
434  PATTERN=">Category:"
435  CATEGORY=`eval sed -n -e "\"$SED_CMD\"" $TEMP`
436  FOUND=
437  for C in $CATEGORIES
438  do
439    if [ "$C" = "$CATEGORY" ]; then FOUND=true ; break ; fi
440  done
441  if [ -n "$FOUND" ]; then
442    CNT=`expr $CNT + 1`	
443  else
444    if [ -z "$CATEGORY" ]; then
445      echo "$COMMAND: you must include a Category: field in your report."
446    else
447      echo "$COMMAND: \`$CATEGORY' is not a known category."
448    fi
449  fi
450  #
451  # 5) Class
452  #
453  PATTERN=">Class:"
454  CLASS=`eval sed -n -e "\"$SED_CMD\"" $TEMP`
455  case "$CLASS" in
456    ""|sw-bug|doc-bug|change-request|support) CNT=`expr $CNT + 1` ;;
457    *)  echo "$COMMAND: \`$CLASS' is not a valid value for \`Class'."
458  esac
459
460  [ $CNT -lt 5 -a -z "$BATCH" ] && 
461    echo "Errors were found with the problem report."
462
463  while true; do
464    if [ -z "$BATCH" ]; then
465      $ECHON1 "a)bort, e)dit or s)end? $ECHON2"
466      read input
467    else
468      if [ $CNT -eq 5 ]; then
469        input=s
470      else
471        input=a
472      fi
473    fi
474    case "$input" in
475      a*)
476	if [ -z "$BATCH" ]; then
477	  echo "$COMMAND: the problem report remains in $BAD and is not sent."
478	  mv $TEMP $BAD
479        else
480	  echo "$COMMAND: the problem report is not sent."
481	fi
482	xs=1; exit
483	;;
484      e*)
485        eval $EDIT $TEMP
486	continue 2
487	;;
488      s*)
489	break 2
490	;;
491    esac
492  done
493done
494#
495#	Remove comments and send the problem report
496#	(we have to use patterns, where the comment contains regex chars)
497#
498# /^>Originator:/s;$ORIGINATOR;;
499sed  -e "
500/^SEND-PR:/d
501/^>Organization:/,/^>[A-Za-z-]*:/s;$ORGANIZATION_C;;
502/^>Confidential:/s;<.*>;;
503/^>Synopsis:/s;$SYNOPSIS_C;;
504/^>Severity:/s;<.*>;;
505/^>Priority:/s;<.*>;;
506/^>Category:/s;$CATEGORY_C;;
507/^>Class:/s;<.*>;;
508/^>Release:/,/^>[A-Za-z-]*:/s;$RELEASE_C;;
509/^>Environment:/,/^>[A-Za-z-]*:/s;$ENVIRONMENT_C;;
510/^>Description:/,/^>[A-Za-z-]*:/s;$DESCRIPTION_C;;
511/^>How-To-Repeat:/,/^>[A-Za-z-]*:/s;$HOW_TO_REPEAT_C;;
512/^>Fix:/,/^>[A-Za-z-]*:/s;$FIX_C;;
513" $TEMP > $REF
514
515if $MAIL_AGENT < $REF; then
516  echo "$COMMAND: problem report sent"
517  xs=0; exit
518else
519  echo "$COMMAND: mysterious mail failure."
520  if [ -z "$BATCH" ]; then
521    echo "$COMMAND: the problem report remains in $BAD and is not sent."
522    mv $REF $BAD
523  else
524    echo "$COMMAND: the problem report is not sent."
525  fi
526  xs=1; exit
527fi
528