#!/bin/sh
# Create a send-pr distribution for a GNATS site.
# Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
# Contributed by Brendan Kehoe (brendan@cygnus.com).
#
# This file is part of GNU GNATS.
#
# GNU GNATS 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 2, or (at your option)
# any later version.
#
# GNU GNATS 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 GNU GNATS; see the file COPYING.  If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

GNATS_ROOT=${GNATS_ROOT:-/usr/local/share/gnats/gnats-db}; export GNATS_ROOT
GNATS_SITE=openbsd
DATADIR=/usr/local/share

FILES="COPYING README INSTALL MANIFEST send-pr.sh install-sid.sh \
	send-pr-el.in send-pr.texi send-pr.1 send-pr.info texinfo.tex \
	categ.texi fields.texi s-usage.texi states.texi version.texi \
	categories"

SUBMITTER=unknown

# Newer config information?
[ -f ${GNATS_ROOT}/gnats-adm/config ] && . ${GNATS_ROOT}/gnats-adm/config

USAGE="usage: $0 [--submitter=SUBMITTER] [--release=RELEASE] directory"

SAVEOPTS=

while [ $# -gt 0 ]; do
  case "$1" in
    -s | --submitter)
      if [ $# -eq 1 ]; then echo "$USAGE"; exit 1; fi
      shift ; SUBMITTER="$1" ;;
    --submitter=*) SUBMITTER="`echo $1 | sed 's/^[-a-z]*=//'`" ;;
    -r | --release)
      if [ $# -eq 1 ]; then echo "$USAGE"; exit 1; fi
      shift ; DEFAULT_RELEASE="$1" ;;
    --release=*) DEFAULT_RELEASE="`echo $1 | sed 's/^[-a-z]*=//'`" ;;
    -*)  echo "$USAGE" ; exit 1 ;;
    *) 
      if [ -z "$SAVEOPTS" ]
      then
        SAVEOPTS="$1"
      else
        SAVEOPTS="${SAVEOPTS}:$1"
      fi
  esac
  shift
done

_IFS="${IFS}"
IFS=:
set -- ${SAVEOPTS}
IFS="${_IFS}"
# shift

if [ $# -eq 0 ]; then echo "$USAGE"; exit 1; fi
if [ $# -gt 1 ]; then 
  DEFAULT_RELEASE="$2"; 
elif [ -z "$DEFAULT_RELEASE" ]; then
  echo "$USAGE"; exit 1
fi
if [ $# -gt 2 ]; then SUBMITTER="$3"; fi

if [ ! -d ${DATADIR}/gnats/dist ]; then
  echo "$0: GNATS send-pr distribution directory ${DATADIR}/gnats/dist does not exist."
  exit 1
fi

if [ ! -d $1 ]; then
  echo "$0: no such destination directory"
  exit 1
fi

for f in $FILES; do
  if [ ! -f ${DATADIR}/gnats/dist/$f ]; then
    if [ ! -f ${DATADIR}/gnats/dist/$f.gz ]; then
      echo "$0: $f does not exist."
      exit 1
    else
      rm -f $1/$f
      gzip -dc ${DATADIR}/gnats/dist/$f >$1/$f
    fi
  else
    rm -f $1/$f
    cp ${DATADIR}/gnats/dist/$f $1
  fi
  rm -f $1/$f
  cp ${DATADIR}/gnats/dist/$f $1
done

cat > $1/Makefile << '__EOF__'
#
# Makefile for building a standalone send-pr.
#
RELEASE=YRELEASEY
SUBMITTER=YSUBMITTERY

EMACS=emacs

prefix = /usr/local
bindir = $(prefix)/bin
datadir = $(prefix)/lib
infodir = $(prefix)/info
lispdir = $(prefix)/lib/emacs/site-lisp
mandir = $(prefix)/man
man1dir = $(mandir)/man1

all: send-pr send-pr.el install-sid

send-pr: send-pr.sh
	sed -e 's,@DATADIR@,$(datadir),g' \
	    -e 's,@SUBMITTER@,$(SUBMITTER),g' \
	    -e 's/@DEFAULT_RELEASE@/$(RELEASE)/g' send-pr.sh > send-pr

send-pr.el: send-pr-el.in
	sed -e 's,@DATADIR@,$(datadir),g' \
	    -e 's,@SUBMITTER@,$(SUBMITTER),g' \
	    -e 's/@DEFAULT_RELEASE@/$(RELEASE)/g' send-pr-el.in > send-pr.el

send-pr.elc: send-pr.el
	-$(EMACS) -batch -q -f batch-byte-compile send-pr.el

install-sid: install-sid.sh
	sed -e 's,@BINDIR@,$(bindir),g' install-sid.sh > install-sid

install: all
	if [ -d $(prefix) ]; then true ; else mkdir $(prefix) ; fi
	if [ -d $(bindir) ]; then true ; else mkdir $(bindir) ; fi
	cp send-pr $(bindir)
	chmod 755 $(bindir)/send-pr
	cp install-sid $(bindir)
	chmod 755 $(bindir)/install-sid
	-parent=`echo $(lispdir)|sed -e 's@/[^/]*$$@@'`; \
	if [ -d $$parent ] ; then true ; else mkdir $$parent ; fi
	if [ -d $(lispdir) ] ; then true ; else mkdir $(lispdir) ; fi
	cp send-pr.el* $(lispdir)
	chmod 644 $(lispdir)/send-pr.el*
	if [ -d $(infodir) ] ; then true ; else mkdir $(infodir) ; fi
	cp send-pr.info $(infodir)/send-pr.info
	chmod 644 $(infodir)/send-pr.info
	if [ -d $(datadir) ] ; then true ; else mkdir $(datadir) ; fi
	if [ -d $(datadir)/gnats ] ; then true ; else mkdir $(datadir)/gnats ; fi
	cp categories $(datadir)/gnats/YGNATS_SITEY
	chmod 644 $(datadir)/gnats/YGNATS_SITEY
	-parent=`echo $(man1dir)|sed -e 's@/[^/]*$$@@'`; \
	if [ -d $$parent ] ; then true ; else mkdir $$parent ; fi
	if [ -d $(man1dir) ] ; then true ; else mkdir $(man1dir) ; fi
	cp send-pr.1 $(man1dir)/send-pr.1
	chmod 644 $(man1dir)/send-pr.1

clean:
	rm -f install-sid send-pr send-pr.el*

__EOF__

sed -e "s/YGNATS_SITEY/$GNATS_SITE/g" \
    -e "s/YSUBMITTERY/$SUBMITTER/" \
    -e "s/YRELEASEY/$DEFAULT_RELEASE/" $1/Makefile > $1/Makefile.tmp
mv -f $1/Makefile.tmp $1/Makefile

exit 0
