#
# pftp -- sends files from host to host through free choosable ports
#
# Copyright (C) 1996, 1997 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 it's favorite ones
# (Do not comment out these options!)
CFLAGS=
CC=

# 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=install
MANUAL= pftp.1
NROFF=nroff -man
RM=rm -f
SHELL=/bin/sh
SRCDIR=src
MKDIR=mkdir

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

all :: run

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

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

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

install: install_bin install_man

run:
	@OS="`uname`"; \
	if test "$$OS" = "Linux" -o "$$OS" = "NEXTSTEP" -o "$$OS" = "FreeBSD"; then \
		O_SYSTEM="-D$$OS"; \
		if test -z "$(CC)"; then \
			if which gcc > /dev/null; then \
				CC="gcc"; \
			else CC="cc"; \
			fi; \
		else CC="$(CC)"; \
		fi; \
	elif test "$$OS" = "AIX"; then \
		if test -z "$(CC)"; then \
			CC="xlC"; \
		else CC="$(CC)"; \
		fi; \
	elif test "$$OS" = "SunOS"; then \
		if test -z "$(CC)"; then \
			if which gcc > /dev/null; then \
				CC="gcc"; \
			else CC=cc; \
			fi; \
		else CC="$(CC)"; \
		fi; \
	elif test "$$OS" = "HP-UX"; then \
		if test -z "$(CC)"; then \
			if gcc > /dev/null 2> /dev/null; then \
				CC="gcc"; \
			else \
				CC=cc; \
				O_SYSTEM="-DHP_UX"; \
			fi; \
		fi; \
	else \
		if test -z "$(CC)"; then \
			CC="cc"; \
		else CC="$(CC)"; \
		fi; \
	fi; \
	if test "$$OS" = "Linux"; then \
		if test -z "$(CFLAGS)"; then \
			CFLAGS=" -O3 -ansi -Wall -D_BSD_SOURCE -D_POSIX_SOURCE"; \
		else CFLAGS="$(CFLAGS)"; \
		fi; \
	elif test "$$OS" = "HP-UX"; then \
		if test -z "$(CFLAGS)"; then \
			CFLAGS="-Ae +Optrs_ansi -O"; \
		else CFLAGS="$(CFLAGS)"; \
		fi; \
	elif test -z "$(CFLAGS)"; then \
		CFLAGS="-O2"; \
	else CFLAGS="$(CFLAGS)"; \
	fi; \
	if test -n "$(USE_PTHREADS)"; then \
		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 /var/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; \
	fi; \
   if grep \*sys_errlist /usr/include/*.h > /dev/null; then \
      HAVE_SYS_ERRLIST="-DHAVE_SYS_ERRLIST"; \
   fi; \
	cd $(SRCDIR); \
	$(MAKE) program OS=$$O_SYSTEM "INSTALL=$(INSTALL)" "CFLAGS=$$CFLAGS" \
	"CC=$$CC" "PTHREAD=$$PTHREAD" "LIBPTHREAD=$$LIBPTHREAD" \
	"PTHREAD_HEADER=$$PTHREAD_HEADER" "HAVE_SYS_ERRLIST=$$HAVE_SYS_ERRLIST"

