case $CONFIG in
'')
	if test -f config.sh; then TOP=.;
	elif test -f ../config.sh; then TOP=..;
	elif test -f ../../config.sh; then TOP=../..;
	elif test -f ../../../config.sh; then TOP=../../..;
	elif test -f ../../../../config.sh; then TOP=../../../..;
	else
		echo "Can't find config.sh."; exit 1
	fi
	. $TOP/config.sh
	;;
esac
case "$0" in
*/*) cd `expr X$0 : 'X\(.*\)/'` ;;
esac
echo "Extracting bin/packinit (with variable substitutions)"
cat >packinit <<!GROK!THIS!
# feed this into perl
	eval "exec perl -S \$0 \$*"
		if \$running_under_some_shell;

# $Id: packinit.SH,v 3.0.1.1 1993/08/19 06:41:53 ram Exp $
#
#  Copyright (c) 1991-1993, Raphael Manfredi
#  
#  You may redistribute only under the terms of the Artistic Licence,
#  as specified in the README file that comes with the distribution.
#  You may reuse parts of this distribution only within the terms of
#  that same Artistic Licence; a copy of which may be found at the root
#  of the source tree for dist 3.0.
#
# $Log: packinit.SH,v $
# Revision 3.0.1.1  1993/08/19  06:41:53  ram
# patch1: leading config.sh searching was not aborting properly
#
# Revision 3.0  1993/08/18  12:04:05  ram
# Baseline for dist 3.0 netwide release.
#

\$orgname='$orgname';
\$myhostname='$myhostname';
\$mydomain='$mydomain';
\$nametype='$nametype';
!GROK!THIS!
cat >>packinit <<'!NO!SUBS!'

print <<EOM;

This program designates the current directory as the top level directory
of a package on which you want to use the programs metaconfig, makedist,
or the patch generation programs.  It will not damage the current directory
in any way except to create a .package file.

EOM

$mypackver=3;	# Base revision number

if (-f '.package') {
	&readpackage;
	if ($packver > $mypackver) {
		die "This .package file was produced by a newer packinit than I am.\n".
		"Please find a packinit of version $packver or greater.\n";
	}
} elsif (-f '../.package' || -f '../../.package' || -f '../../../.package') {
	die "Run in top level directory only.\n";
}

# Now set up to do reads with possible shell escape.
sub myread {
	($rp,$dflt) = @_;
	$rp .= " [$dflt]";
	print "$rp ";
	$ans='!';
	while ($ans =~ /^!/) {
		$ans = <STDIN>;
		chop($ans);
		if ($ans eq '!') {
			system '/bin/sh';
			print "\n$rp ";
		} elsif ($ans =~ s/^!//) {
			system '/bin/sh', '-c', $ans;
			print "\n$rp ";
		}
	}
	$ans = $dflt if $ans eq '';
	$ans = '' if $ans eq 'none';
	$ans;
}

$dflt = $package;
($dflt) = (`pwd | tr "[A-Z]" "[a-z]"` =~ m|.*/(.*)|) unless $package;

print <<EOM;
Your package will be known by some name, which should be a single word.
Generally it is the name of the chief executable program.

EOM
$package = &myread('What is the name of your package?',$dflt);

if ($baserev ne '') {
	$dflt=$baserev;
} else {
	print "\n";
	$dflt='';
	print "Checking RCS files for current version...";
	@rcs = <RCS/*,v *,v>;
	$rcs = shift(@rcs);
	if (! -f $rcs) {
		$rcs = shift(@rcs);
		if (! -f $1) {
			print "I don't see any RCS files there (yet).";
			$dflt='1.1';
		}
	}
	$revs=0;
	if ($dflt eq '') {
		$rlog = `rlog $rcs`;
		($dflt) = ($rlog =~ /\nhead:\s*(\d+\.\d+)/);
		$rlog = `rlog -r$dflt.1- -r$dflt.2- -r$dflt.3- -r$dflt.4- $rcs`;
		($revs) = ($rlog =~ /selected revisions:\s*([\d.]+)/);
		$dflt='1.1' if $dflt eq '';
	}

	print <<EOM;

To use the patch generating portion of the system, you must have RCS.  You
must begin with a freshly checked-in trunk revision (n.n) and choose a branch
number for patches.  The trunk revision upon which the patch branch is based
is called the base revision.

EOM

	if ($revs > 1) {
		print <<EOM;
(The current revision ($dflt) has branches--you may have to check in a new
trunk revision, or use patbase to mark the base revisions.)
EOM
		($first,$second) = split(/\./,$dflt);
		++$second;
		$dflt="$first.$second";
	}
}

print "\n";
$foo = &myread('What is or will be the base revision number?', $dflt);
if ($baserev ne $foo) {
	$baserev = $foo;
	$ftpdir = '';		# invalidate ftp directory on version change
}

print "\n";
$dflt = ($patchbranch ne '' ? $patchbranch : 1);
$patchbranch =
	&myread('What branch number do you want to use for patches?', $dflt);

$dflt = $copyright eq ' ' ? 'n' : 'y';
print <<EOM;

If you wish, you may use a generic copyright. This is appropriate if all
your source file have to be distributed under the same conditions. Using
this facility means you will not be able to change the copyright notice
between patches (i.e. while in the same version number). All you need to
do is write a copyright file containing the proper copyright notice, and
then use @COPYRIGHT@ within your source code (before any RCS marker). That
will be extended to the full copyright, preserving any leading comment.

EOM
$use_copyright = &myread('Do you wish to use a generic copyright?', $dflt);
if ($use_copyright =~ /^y/i) {
	print "\n";
	$copyright = '' if $copyright eq ' ';	# They changed their mind!
	$dflt = $copyright || 'COPYRIGHT';
	$copyright = &myread('What file do you wish to store it in?', $dflt);
} else {
	$copyright = ' ';		# Remember they don't want it
}
	
if ($use_copyright =~ /^y/i) {
	$ans = 'yes';			# Yes, use our own diff (have to expand copyright)
} else {
	print "\n";
	$dflt = ($mydiff eq '' ? 'n' : 'y');
	$ans = &myread(
	'Do you want to use your own diff (rather than rcsdiff -c) for patches?',
		$dflt);
}
if ($ans =~ /^n/i) {
	$mydiff='';
} else {
	print "\n";
	$dflt = $mydiff || 'diff -c';
	$mydiff = &myread('What diff command do you want to use?', $dflt);
}

print <<EOM;

Over the lifetime of the $package package, people will want to request
distribution kits and patches.  In particular, automatically generated
patches will say where to get previous patches from.

EOM
$logname = &getlogname;
$dflt = $maintname || &getfullname($logname);
$maintname = &myread('Who should requests be sent to (full name)?', $dflt);

print <<EOM;

Now you need to give a one-line network mailing address for $maintname.
It does not need to be parseable by machine, but can be of the form:

	{name1,name2,name3}!myhost!myname

	or

	myname@myhost.domain

EOM
$dflt = $maintloc || "$logname@$myhostname$mydomain";
$maintloc = &myread('What is the network mailing address?', $dflt);

print "\n";
$dflt = $ftpsite ? 'y' : 'n';
$ans = &myread(
	'Will you put patches where they can be acquired by anonymous FTP?',$dflt);
if ($ans =~ /^n/i) {
	$ftpsite='';
} else {
	print "\n";
	$dflt = $ftpsite;
	($dflt = $maintloc) =~ s/.*@([^\s,()]*).*/$1/ unless $dflt;
	$ftpsite = &myread('What is the Internet sitename for that?',$dflt);
}

print <<EOM;

If you have mailagent installed (posted in comp.sources.misc), you may choose
to distribute patches via e-mail and have all the issued patches mention
this, as well as instructions telling how to get those (missing) patches.

EOM
$dflt = $mailagent eq 'true' ? 'y' : 'n';
$mailagent = &myread(
	'Do you wish to let people retrieve patches via mailagent?', $dflt);
$mailagent = ($mailagent =~ /^y/i) ? 'true' : 'false';

print "\n";
$dflt = $orgname || $ENV{'ORGANIZATION'};
$orgname = &myread('Organization:',$dflt);
$orgname = &tilda_expand($orgname);		# Can specify filename with ~user

print "\n";
$dflt = $newsgroups || 'comp.sources.bugs';
$newsgroups = &myread('Newsgroup(s) to post patches to:', $dflt);

print "\n";
$dflt = $recipients || 'source-archives@mirror.TMC.COM';
$recipients = &myread('Recipient(s) to send patches to:',$dflt);

if ($ftpsite) {
	print "\n";
	$dflt = $ftpdir || "/usr/spool/ftp/pub/$package.$baserev/patches";
	$ftpdir = &myread('FTP directory to copy patches to:',$dflt);
} else {
	$ftpdir = '';
}

print "\nCreating .package...\n";
open(PACKAGE, '>.package') || die "Can't create .package";
chop($date = `date`);
print PACKAGE <<EOF;
: This file was created by running packinit on $date.
: Do not hand edit; run packinit again if changes are to be made.
packver='$mypackver'
: Basic variables
package=$package
baserev=$baserev
patchbranch=$patchbranch
copyright='$copyright'
mydiff='$mydiff'
maintname='$maintname'
maintloc='$maintloc'
ftpsite='$ftpsite'
orgname='$orgname'
newsgroups='$newsgroups'
recipients='$recipients'
ftpdir='$ftpdir'
mailagent='$mailagent'
: Derivative variables -- do not change
revbranch=\"\$baserev.\$patchbranch\"
EOF

!NO!SUBS!
$grep -v '^;#' ../pl/package.pl >>packinit
$grep -v '^;#' ../pl/logname.pl >>packinit
$grep -v '^;#' ../pl/fullname.pl >>packinit
$grep -v '^;#' ../pl/tilde.pl >>packinit
chmod 755 packinit
$eunicefix packinit
