#!/bin/sh
#
# $Id: tkeditpr,v 1.3 1993/11/16 22:19:30 jason Exp $
#

### this section is configured by the Makefile 
GNATS_ROOT=/var/lib/gnats/gnats-db; ##GNATS_ROOT##
LIBDIR=/usr/lib; ##LIBDIR##
TKGNATSLIB=/usr/lib/tkgnats; ##TKGNATSLIB##
###

PR_EDIT=$LIBDIR/gnats/pr-edit

error () {
    2>&1 echo "$*"
}

### get full id of pr
if [ "$1" = "" ]; then
    msgDialog "tkeditpr: no PR id supplied" "Usage:	tkeditpr <PR id>"
    exit 1
fi
case "$1" in
*/*)
    full_id=$1
    ;;
*)
    full_id=`grep "/$1:" $GNATS_ROOT/gnats-adm/index | awk -F: '{print $1}' -`
    ;;
esac
shift

pr=$GNATS_ROOT/$full_id
if [ "$full_id" = "" ]; then
  error "tkeditpr: file not found" ;
  exit 1
else
  if [ ! -f $pr -o ! -r $pr ]; then
    error "tkeditpr: cannot read PR $full_id"
    exit 1
  fi
fi

### find a username@hostname
if [ "$USER" != "" ]; then
  me=$USER
else
  if [ "$LOGNAME" != "" ]; then
    me=$LOGNAME
  else
    msgDialog "tkeditpr: no user name found set LOGNAME." ; exit 1
  fi
fi
if [ -z "$HOSTNAME" ]; then
  if [ -f /bin/hostname ] ; then HOSTNAME=`/bin/hostname`
  elif [ -f /usr/bin/hostname ] ; then HOSTNAME=`/usr/bin/hostname`
  # Solaris et al.
  elif [ -f /usr/ucb/hostname ] ; then HOSTNAME=`/usr/ucb/hostname`
  # Irix
  elif [ -f /usr/bsd/hostname ] ; then HOSTNAME=`/usr/bsd/hostname`
  fi
fi
if [ -n "$HOSTNAME" ]; then
  me="$me@$HOSTNAME"
fi

###

### do some traps before we start to make sure the pr gets unlocked
### if something goes wrong
locked=
trap 'rm -f /tmp/u$$ $new ; exit 0' 0
trap 'if [ "$locked" != "" ]; then \
	$PR_EDIT --unlock $full_id ; \
		locked= ; \
      fi ; \
	rm -f /tmp/u$$; exit 1' 1 2 3 13 15

### lock a copy of the PR
new=/tmp/ep$$
cp $pr $new
$PR_EDIT --lock $me $full_id 2> /tmp/u$$
locked=t

### if the lock failed bail out
if [ -s /tmp/u$$ ]; then
  if [ "`grep exists /tmp/u$$`" = "" ]; then
    msgDialog "tkeditpr: PR $full_id is locked by `sed 's/.*by //g' /tmp/u$$`"
  else
    msgDialog "tkeditpr: GNATS is presently locked, try again in a moment"
  fi
  rm -f /tmp/u$$
  exit 1
fi

#################### start of the wish section ########################

wish $TKGNATSLIB/tkeditpr.tcl $* $full_id $new

#
# end of tcl script, We are back into /bin/sh land at this point
#
$PR_EDIT --unlock $full_id

#
# If needed. unlock the file here...
#
