#
# File:		Makefile
# Author:	G. Paul Ziemba
# Date:		93.01.25
# SCCS:		@(#)Makefile	1.30 9/29/94
#

#################################################################
#			BEGIN CONFIGURATION			#
#################################################################
#
# Details about INSTALLATION
#
INST_PATH=		/tools-master/sun4-4.1/local/bin
INST_OWNER=		paul
INST_GROUP=		tools
INST_MODE=		0755
INST_MAN=		/tools-master/share/man
INST_MAN_MODE=		0664
MANSECT=		1

#
# Details about EXECUTION
#

#
# Default config file name for server. If this is not specified,
# default config file is /etc/tcpr.conf. The default config file
# name may be overridden at run-time with a command-line argument.
#
CONFIGFILE=

#
# The name of the relaying host
#
RELAYHOST=		gw

#
# Set this to non-zero to enable ftp .netrc spoofing
#
PCLIENT_SPOOFNETRC=	1

#
# define this to be a single hyphen if you want the client telnet
# invoked as "telnet host -port" instead of "telnet host port".
# Some telnets interpret the leading hyphen on the port argument
# as a request to force character mode if the port is not 23.
#
# If you do not want this feature (some telnets will break),
# leave the definition blank. If in doubt, leave it out.
#
#CHARMODE_PORT=-

#
# Where does the tcprelay executable live
#
#EXEC_PATH=		${INST_PATH}
EXEC_PATH=		/n/local/bin

#
# Where is perl?
#
PERL_PATH=		/n/ucs/bin/perl

#
# The following paths are used by tcprelay to find various
# system programs. You may specify each as a colon-separated
# list of directories, as a single directory, or as a full
# pathname of a program to execute.
#
# If you leave a path spec empty, tcprelay will use a built-in
# default search path.
#
ZPATH=	/bin:/usr/bin:/etc:/usr/etc:/usr/ucb

# Path for "netstat"
NETSTATPATH=	$(ZPATH)

# Path for "ifconfig"
IFCONFIGPATH=	$(ZPATH)

# Path for "logger"
LOGGERPATH=	$(ZPATH)

# Needed only for releasing
PACKAGE_NAME=	tcpr
#################################################################
#			END CONFIGURATION			#
#################################################################
MODULE_ARCHIVE_PATH=	/n/sccs/tools/public/tcpr
SRCS_PL=		pclient tcprpmd tcprelay
SRCS_NON_PL=		README HOW-IT-WORKS INSTALL \
			Makefile ChangeLog README-debug util.pl
PCLIENT_LINKS=		pftp ptelnet pfinger pwhois

all:	pclient tcprpmd tcprelay

pclient:	pclient.pl
	rm -f tmp
	echo '#!$(PERL_PATH)'				>>tmp
	echo '$$RELAYHOST="$(RELAYHOST)";'		>>tmp
	echo '$$CHARMODE_PORT="$(CHARMODE_PORT)";'	>>tmp
	echo '$$EXEC_PATH="$(EXEC_PATH)";'		>>tmp
	echo '$$PCLIENT_SPOOFNETRC="$(PCLIENT_SPOOFNETRC)";'		>>tmp
	cat $@.pl					>>tmp
	chmod +x tmp
	$(PERL_PATH) -c tmp
	mv tmp $@

tcprpmd:	tcprpmd.pl util.pl
	rm -f tmp
	echo '#!$(PERL_PATH)'				>>tmp
	echo '$$CONFIGFILE="$(CONFIGFILE)";'		>>tmp
	echo '$$RELAYHOST="$(RELAYHOST)";'		>>tmp
	echo '$$CHARMODE_PORT="$(CHARMODE_PORT)";'	>>tmp
	echo '$$EXEC_PATH="$(EXEC_PATH)";'		>>tmp
	echo '$$LOGGERPATH="$(LOGGERPATH)";'		>>tmp
	cat $@.pl					>>tmp
	cat util.pl					>>tmp
	chmod +x tmp
	$(PERL_PATH) -c tmp
	mv tmp $@

tcprelay:	tcprelay.pl util.pl
	rm -f tmp
	echo '#!$(PERL_PATH)'				>>tmp
	echo '$$CONFIGFILE="$(CONFIGFILE)";'		>>tmp
	echo '$$NETSTATPATH="$(NETSTATPATH)";'		>>tmp
	echo '$$IFCONFIGPATH="$(IFCONFIGPATH)";'	>>tmp
	echo '$$LOGGERPATH="$(LOGGERPATH)";'		>>tmp
	cat $@.pl					>>tmp
	cat util.pl					>>tmp
	chmod +x tmp
	$(PERL_PATH) -c tmp
	mv tmp $@

install:	$(SRCS_PL)
	for file in $(SRCS_PL) ; do\
	    install -o $(INST_OWNER) -g $(INST_GROUP) -m $(INST_MODE) \
		$${file} $(INST_PATH);\
	done
	for name in $(PCLIENT_LINKS) ; do \
	    rm -f $(INST_PATH)/$${name} ;\
	    ln -s pclient $(INST_PATH)/$${name} ;\
	done

install-man:
	install -o $(INST_OWNER) -g $(INST_GROUP) -m $(INST_MAN_MODE) \
	    pclient.1 $(INST_MAN)/man$(MANSECT) ;\
	echo .so man$(MANSECT)/pclient.1 > $(INST_MAN)/man$(MANSECT)/pftp.1
	echo .so man$(MANSECT)/pclient.1 > $(INST_MAN)/man$(MANSECT)/ptelnet.1

#################################################################
#		Stuff for building releases			#
#################################################################
shar:	$(PACKAGE_NAME).shar
tar:	$(PACKAGE_NAME).tar

$(PACKAGE_NAME).shar:	MANIFEST
	shar -v -s -x -dRalph_the_Wonder_Llama \
	    $(SRCS_NON_PL) $(patsubst %,%.pl,$(SRCS_PL)) MANIFEST >$@

$(PACKAGE_NAME).tar:	MANIFEST
	tar cf $@ $(SRCS_NON_PL) $(patsubst %,%.pl,$(SRCS_PL)) MANIFEST

clean:
	rm -f $(SRCS_PL)

MANIFEST:	FRC
	@/bin/rm -f MANIFEST
	@$(MAKE) whatfiles
	# if SNAPSHOTS doesn't match current files, do release
	# copy SNAPSHOTS to MANIFEST
	@if sed -e '1,3d' <SNAPSHOTS | diff - whatfiles >/dev/null; then \
		: ;\
	else \
		echo "SNAPSHOTS doesn't match current directory, unreleased?" ;\
		exit 1 ;\
	fi
	cp SNAPSHOTS MANIFEST

release:	whatfiles
	@/bin/rm -f _tw
	@echo -n "SCCS: %W" >_tw
	@echo -n "% %G" >>_tw
	@echo "%" >>_tw
	@echo -n "RELEASE: " >>_tw
	@sccs -p. -d$(MODULE_ARCHIVE_PATH) edit -p SNAPSHOTS |\
	    perl \
		-e 'while (<>) {' \
		-e '  if (/^RELEASE:\s+([\d\.]+)$$/) {' \
		-e '    split(/\./, $$1);' \
		-e '    if ("$(ROLL)" ne "") {' \
		-e '        $$_[1]++; print join(".", @_[0,1]), "\n"; exit 0;'\
		-e '    }' \
		-e '    $$_[2]++; print join(".", @_[0,1,2]), "\n"; exit 0;' \
		-e '  }' \
		-e '}' \
		-e 'print STDERR "No previous release number\n";' \
		-e 'exit 1;' >> _tw
	@echo '' >>_tw
	@cat whatfiles >>_tw
	@/bin/mv -f _tw SNAPSHOTS
	@REL=`grep RELEASE: SNAPSHOTS` ;\
	sccs -p. -d$(MODULE_ARCHIVE_PATH) delget -y"$${REL}" SNAPSHOTS

whatfiles:	FRC
	@rm -f $@
	@for file in $(patsubst %,%.pl,$(SRCS_PL)) $(SRCS_NON_PL) ; do \
		what -s $${file} |\
		    perl \
			-e 'while (<STDIN>) {' \
			-e '  next if ($$. != 2);' \
			-e '  tr/()/  /;' \
			-e '  s/^\s+//;' \
			-e '  split(/\s+/, $$_);' \
			-e '  print "$$_[1]\t$$_[0]\n";' \
			-e '  exit 0;' \
			-e '}' \
			-e 'print STDERR "$$ARGV[0]: No revision number\n";' \
			-e 'exit 1;' \
			$${file} >> $@ ;\
	done

FRC: ;
