#
# pftp -- sends files from host to host through free choosable ports
#
# Copyright (C) 1996, 1997, 1998 Ben Schluricke
#
# This program 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 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the emplied warranty of MERCHANT-
# ABILITY OF 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 this program; if not, write to the Free Software Foundation,
# Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
# 
#    Written by Ben Schluricke
#    E-Mail:    bhor0533@lehr.chem.TU-Berlin.DE
#
# This program is dedicated to my girl-friend, Heather O'Rourke.
#
#
##################################################################

# Directory where you want to install the binary and the manual page
#PREFIX=$(HOME)
PREFIX=/usr/local
BINDIR=$(PREFIX)/bin
MANDIR=$(PREFIX)/man

# Choose your favorite options if you like or let
# this description file choose its favorite ones.
# (Do not comment out these options!)
CFLAGS=
CC=

# This is the default services port.  If you want to use the
# server started by inetd you have to add the following line
# in `/etc/services' (see services(5)):
#
#pftp      27/tcp
#
# and in `/etc/inetd.conf' the next line (see inetd(8)):
#
#pftp  stream  tcp  nowait  root /usr/local/bin/pftp pftp -ir
#
# DO NOT COMMENT OUT THIS LINE!
INETD_PORT=27

# If your system has shadow password uncomment
#HAVE_SHADOW=1

# If you do have POSIX threads but don't want to use it comment
# out the following option.  Otherwise leave it as it is.
USE_PTHREADS=yes

# If `pthread.h' is not in `/usr/include' and `libpthread.a' resp.
#`libgthreads.a' not in `/usr/lib' specify it's origin directory, 
# e.g. `$(HOME)/lib/include'.
#PTHREAD_H=$(HOME)/lib/include
#PTHREAD_A=$(HOME)/lib

##########################################################
# You shouldn't need to modify anything below this line.
##########################################################

.SUFFIXES:
.SUFFIXES: .1 .man

INSTALL=cp
WHEREIS=which
MANUAL= pftp.1
BIN=pftp
NROFF=nroff -man
RM=rm -f
SHELL=/bin/sh
SRCDIR=src
MKDIR=mkdir
UNAME=uname
AWK=awk

###################################################
# Successfully tested unames, compilers, and flags
### AIX
AIX_UNAME=AIX
AIX_CC=xlC
AIX_CFLAGS=-O2

### FreeBSD
FreeBSD_UNAME=FreeBSD
FreeBSD_CC=gcc
FreeBSD_CFLAGS=-O2

### HP-UX
HP_UNAME=HP-UX
HP_CC=cc
HP_CFLAGS=-Ae -O

### IRIX
IRIX_UNAME=IRIX
IRIX_CC=cc
IRIX_CFLAGS=-O2

### IRIX64
IRIX64_UNAME=IRIX64
IRIX64_CC=cc
IRIX64_CFLAGS=-O2

### Linux
Linux_UNAME=Linux
Linux_CC=gcc
Linux_CFLAGS= -O3 -ansi -Wall -fexpensive-optimizations -D_BSD_SOURCE -D_POSIX_SOURCE

### NEXTSTEP
NEXTSTEP_UNAME=NEXTSTEP
NEXTSTEP_CC=cc
NEXTSTEP_CFLAGS=-O2 -D_POSIX_SOURCE

### OSF1
OSF1_UNAME=OSF1
OSF1_CC=cc
OSF1_CFLAGS=-w0 -verbose -D_ANSI_C_SOURCE -D_POSIX_SOURCE -D_XOPEN_SOURCE -D_OSF_SOURCE

### OpenBSD
OpenBSD_UNAME=OpenBSD
OpenBSD_CC=gcc
OpenBSD_CFLAGS=-O2

### SunOS
SunOS_UNAME=SunOS
SunOS_CC=gcc
SunOS_CFLAGS=-O2 -fexpensive-optimizations

### ULTRIX
Ultrix_UNAME=ULTRIX
Ultrix_CC=cc
Ultrix_CFLAGS=-O2 -w

### unicos
unicos_UNAME=unicos
unicos_CC=cc
unicos_CFLAGS=-O2

### Default Compiler
Default_CC=cc
Default_CFLAGS=-O
###################################################

.man.1:
	$(NROFF) $< > $*.1

all :: $(SRCDIR)/$(BIN)

clean:
	$(RM) $(MANUAL)
	@cd $(SRCDIR); $(MAKE) clean

install_bin: $(SRCDIR)/$(BIN)
	@cd $(SRCDIR); $(MAKE) install BINDIR=$(BINDIR) "INSTALL=$(INSTALL)"

install_man: $(MANUAL)
	@if test ! -d "$(MANDIR)"; then \
		$(MKDIR) $(MANDIR); \
		echo $(MKDIR) $(MANDIR); \
	fi; \
	if test ! -d "$(MANDIR)/man1"; then \
		$(MKDIR) $(MANDIR)/man1; \
		echo $(MKDIR) $(MANDIR)/man1; \
	fi; \
	if $(INSTALL) $(MANUAL) $(MANDIR)/man1/$(MANUAL); then \
		echo "$(INSTALL) $(MANUAL) $(MANDIR)/man1/$(MANUAL)"; \
		chmod 644 $(MANDIR)/man1/$(MANUAL); \
		echo "chmod 644 $(MANDIR)/man1/$(MANUAL)"; \
	else \
		echo "** Can't install \`$(MANUAL)' in \`$(MANDIR)/man1'!"; \
	fi

install: install_bin install_man

$(SRCDIR)/$(BIN): $(SRCDIR)/*.c $(SRCDIR)/*.h
	@if test "`echo -n H`" = "H"; then \
		ECHO="echo -n"; \
	else \
		ECHO="echo"; \
		NEWLINE="\c"; \
	fi; \
	$$ECHO "Building pftp on $$NEWLINE"; \
	OS="`$(UNAME)`"; \
	echo "$$OS"...; \
	$$ECHO "Checking C compiler...$$NEWLINE"; \
	if test -z "$(CC)"; then \
		if test "$$OS" = "$(AIX_UNAME)"; then \
			if test -x "`$(WHEREIS) $(AIX_CC)`" 2> /dev/null; then \
				CC="$(AIX_CC)"; \
				if test -z "$(CFLAGS)"; then \
					CFLAGS="$(AIX_CFLAGS)"; \
				fi; \
			fi; \
		elif test "$$OS" = "$(FreeBSD_UNAME)"; then \
			O_SYSTEM="-D$$OS"; \
			if test -x "`$(WHEREIS) $(FreeBSD_CC)`" 2> /dev/null; then \
				CC="$(FreeBSD_CC)"; \
				if test -z "$(CFLAGS)"; then \
					CFLAGS="$(FreeBSD_CFLAGS)"; \
				fi; \
			fi; \
		elif test "$$OS" = "$(OpenBSD_UNAME)"; then \
			O_SYSTEM="-D$$OS"; \
			if test -x "`$(WHEREIS) $(OpenBSD_CC)`" 2> /dev/null; then \
				CC="$(OpenBSD_CC)"; \
				if test -z "$(CFLAGS)"; then \
					CFLAGS="$(OpenBSD_CFLAGS)"; \
				fi; \
			fi; \
		elif test "$$OS" = "$(HP_UNAME)"; then \
			O_SYSTEM="-DHP_UX"; \
			if test -x "`$(WHEREIS) $(HP_CC)`" 2> /dev/null; then \
				CC="$(HP_CC)"; \
				if test -z "$(CFLAGS)"; then \
					CFLAGS="$(HP_CFLAGS)"; \
				fi; \
			elif test -x "`$(WHEREIS) gcc`" 2> /dev/null; then \
				CC="gcc"; \
				if test -z "$(CFLAGS)"; then \
					CFLAGS="-O"; \
				else CFLAGS="$(CFLAGS)"; \
				fi; \
			fi; \
		elif test "$$OS" = "$(IRIX_UNAME)"; then \
			if test -x "`$(WHEREIS) $(IRIX_CC)`" 2> /dev/null; then \
				CC="$(IRIX_CC)"; \
				if test -z "$(CFLAGS)"; then \
					CFLAGS="$(IRIX_CFLAGS)"; \
				fi; \
			fi; \
		elif test "$$OS" = "$(IRIX64_UNAME)"; then \
			if test -x "`$(WHEREIS) $(IRIX64_CC)`" 2> /dev/null; then \
				CC="$(IRIX64_CC) -64"; \
				if test -z "$(CFLAGS)"; then \
					CFLAGS="$(IRIX64_CFLAGS)"; \
				fi; \
			fi; \
		elif test "$$OS" = "$(Linux_UNAME)"; then \
			O_SYSTEM="-D$$OS"; \
			if test -x "`$(WHEREIS) $(Linux_CC)`" 2> /dev/null; then \
				CC="$(Linux_CC)"; \
				if test -z "$(CFLAGS)"; then \
					CFLAGS="$(Linux_CFLAGS)"; \
				fi; \
			fi; \
		elif test "$$OS" = "$(NEXTSTEP_UNAME)"; then \
			O_SYSTEM="-D$$OS"; \
			if test -x "`$(WHEREIS) $(NEXTSTEP_CC)`" 2> /dev/null; then \
				CC="$(NEXTSTEP_CC)"; \
				if test -z "$(CFLAGS)"; then \
					CFLAGS="$(NEXTSTEP_CFLAGS)"; \
				fi; \
			fi; \
		elif test "$$OS" = "$(OSF1_UNAME)"; then \
			O_SYSTEM="-D$$OS"; \
			CC="$(OSF1_CC)"; \
			if test -z "$(CFLAGS)"; then \
				CFLAGS="$(OSF1_CFLAGS)"; \
			fi; \
		elif test "$$OS" = "$(SunOS_UNAME)"; then \
			O_SYSTEM="-D$$OS"; \
			if test -x "`$(WHEREIS) $(SunOS_CC)`" 2> /dev/null; then \
				CC="$(SunOS_CC)"; \
				if test -z "$(CFLAGS)"; then \
					CFLAGS="$(SunOS_CFLAGS)"; \
				fi; \
			fi; \
		elif test "$$OS" = "$(Ultrix_UNAME)"; then \
			O_SYSTEM="-D$$OS"; \
			if test -x "`$(WHEREIS) $(Ultrix_CC)`" 2> /dev/null; then \
				CC="$(Ultrix_CC)"; \
				if test -z "$(CFLAGS)"; then \
					CFLAGS="$(Ultrix_CFLAGS)"; \
				fi; \
			fi; \
		elif test "$$OS" = "$(unicos_UNAME)"; then \
			O_SYSTEM="-D$$OS"; \
			if test -z "$(CC)"; then \
				CC="$(unicos_CC)"; \
			fi; \
			if test -z "$(CFLAGS)"; then \
				CFLAGS="$(unicos_CFLAGS)"; \
			fi; \
		fi; \
		if test -n "$$CC"; then \
			echo found "$$CC".; \
		fi; \
	else \
		if test -x "`$(WHEREIS) $(CC)`"; then \
			CC="$(CC)"; \
			echo found "$$CC".; \
			if test -n "$(CFLAGS)"; then \
				CLFAGS="$(CFLAGS)"; \
			else \
				CFLAGS="$(Default_CFLAGS)"; \
			fi; \
		else \
			echo "cannot find executable \`$(CC)'!"; \
			exit; \
		fi; \
	fi; \
	if test -z "$$CC"; then \
		if test -x "`$(WHEREIS) $(Default_CC)`"; then \
			CC="$(Default_CC)"; \
			echo found "$$CC".; \
			if test -z "$(CFLAGS)"; then \
				CFLAGS="$(Default_CFLAGS)"; \
			else \
				CFLAGS="$(CFLAGS)"; \
			fi; \
		else \
			echo "cannot find a C compiler!"; \
			exit; \
		fi; \
	fi; \
	if test "$$OS" = "$(NEXTSTEP_UNAME)"; then \
		echo "Checking sys_errlist...skipped."; \
		echo "Checking usleep...skipped."; \
	else \
		$$ECHO "Checking sys_errlist...$$NEWLINE"; \
		if grep sys_errlist /usr/include/stdio.h /usr/include/errno.h > /dev/null ; then \
			HAVE_SYS_ERRLIST="-DHAVE_SYS_ERRLIST"; \
			echo "found."; \
		else \
			echo "not found."; \
		fi; \
		$$ECHO "Checking usleep...$$NEWLINE"; \
		if grep usleep /usr/include/unistd.h > /dev/null ; then \
			HAVE_USLEEP="-DHAVE_USLEEP"; \
			echo "found."; \
		else \
			echo "not found."; \
		fi; \
	fi; \
	$$ECHO "Checking for shadow password...$$NEWLINE"; \
	if test -f "/etc/shadow" || test -n "$(HAVE_SHADOW)" ; then \
		echo "yes"; \
		HAS_SHADOW_PASSWD="-DHAS_SHADOW_PASSWD"; \
	else \
		echo "no"; \
	fi; \
	if test -n "$(USE_PTHREADS)"; then \
		$$ECHO "Checking POSIX threads...$$NEWLINE"; \
		if test -n "$(PTHREAD_H)"; then \
			PTHREAD_HEADER="-I$(PTHREAD_H)"; \
		fi; \
		for libdir in $(PTHREAD_A) /lib /usr/lib /usr/local/lib \
			$(HOME)/lib; do \
			if test -f "$$libdir/libpthread.a"; then \
				PTHREAD="-DUSE_POSIX_THREAD"; \
				LIBPTHREAD="-L$$libdir -lpthread"; \
			elif test -f "$$libdir/libgthreads.a"; then \
				PTHREAD="-DUSE_POSIX_THREAD"; \
				LIBPTHREAD="-L$$libdir -lgthreads -lmalloc"; \
			fi; \
		done; \
		if test -n "$$PTHREAD"; then \
			echo "installed."; \
		else \
			echo "not installed."; \
		fi; \
	fi; \
	$$ECHO "Checking for glib...$$NEWLINE"; \
	for libdir in libglib.a /lib /usr/lib ; do \
		if test -f "$$libdir/libglib.a"; then \
			HAVE_GLIB="-DHAVE_GLIB"; \
		fi; \
	done; \
	if test -n "$$HAVE_GLIB"; then \
		echo "found."; \
	else \
		echo "not found."; \
	fi; \
	$$ECHO "Checking for libcrypt...$$NEWLINE"; \
	for libdir in libcrypt.a /lib /usr/lib \
		$(HOME)/lib; do \
		if test -f "$$libdir/libcrypt.a"; then \
			LIBCRYPT="-lcrypt"; \
			HAVE_LIBCRYPT="-DHAVE_LIBCRYPT"; \
		fi; \
	done; \
	if test -n "$$LIBCRYPT"; then \
		echo "found."; \
	else \
		echo "not found."; \
	fi; \
	if test -n "$(INETD_PORT)"; then \
		INETD_PORT="-DINETD_PORT=$(INETD_PORT)"; \
	fi; \
	cd $(SRCDIR); \
	DEFS="$$O_SYSTEM $$PTHREAD $$PTHREAD_HEADER $$HAVE_SYS_ERRLIST $$HAVE_GLIB \
	$$HAS_SHADOW_PASSWD $$HAVE_LIBCRYPT $$INETD_PORT $$HAVE_USLEEP"; \
	LIBS="$$LIBPTHREAD $$LIBCRYPT"; \
	$(MAKE) program "CC=$$CC" "CFLAGS=$$CFLAGS" "DEFS=$$DEFS" "LIBS=$$LIBS" \
	"DEBUG=$(DEBUG)"

show_entry:
	@$(AWK) 'BEGIN { NUM = 2; printf "pftp" } /^telnet/ \
	{ while ( NUM + 1 < NF ) { printf "  %s", $$NUM; NUM++; } } \
	END { printf "  $(BINDIR)/pftp  pftp -ir\n" }' /etc/inetd.conf 

