# Make file for ftpd directory
#
# Things you might want to change.  Pass them in on the command line:
# ROOT - should point to the root where the commands will be installed
#        (e.g. /deliver/INETSVCS/indnetwk)

ROOT=

PRODUCTS=/usr/lbin/ftpd 

# Defaults file containing the shell, compiler options, etc
BUILDRULES=/source/INETSVCS/$(LOGNAME)/config/defaults

include ${BUILDRULES}

INCLUDES=-I. -I../include -I/usr/include

SRCS=   ftpd.c vers.c ftpcmd.c glob.c popen.c ftpd_sec.c logwtmp.c
OBJS=   ftpd.o vers.o ftpcmd.o glob.o popen.o ftpd_sec.o logwtmp.o

COMMON_OBJS= ftpd.o ftpcmd.o vers.o

LF_FLAGS=-D__STDC_EXT__ -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64

CCFLAGS=$(INCLUDES) -DNEW_LOGGING -DAUDIT -DSETPROCTITLE $(CFLAGS) \
	$(OTHER_CCFLAGS) -Ae -Dunix -Dhpux

LDLIBRARIES=    -lsec

LD_KRB5_LIBS=    -lsec -lgssapi_krb5 -lkrb5 -lcrypto -lisode -lcom_err

#Commands used by this Makefile

MV=/usr/bin/mv
YACC=/opt/langtools/bin/yacc

#Install database name
INSTALLDB=/source/INETSVCS/$(LOGNAME)/config/installdb

#Script called to parse the install deliverable
INSTALLSCPT=/source/INETSVCS/$(LOGNAME)/config/install_script

default: all

all: clean build install

clean:
	$(RM) -f *.o
	$(RM) -f core
	$(RM) -f ftpd
	$(RM) -f kftpd
	$(RM) -f ftpcmd.c
	$(RM) -f vers.c
	
build:  ftpd 

# The ftpd and kftpd targets share common source that differ only
# in their use of compilation flags.  Both targets will re-compile 
# the sources for COMMON_OBJS to ensure that the source files have 
# been compiled with the correct flags.  This is safe, but not optimum.

ftpd:   ftpd_source $(OBJS)
	$(ECHO) "Building ftpd..."
	if \
		grep -q -E "(inet_mlp|inet_100)" RMTBRANCH ; \
	then \
		$(CC) -DNO_LARGE_UID $(LDFLAGS) $(CCFLAGS) -o $@ $(OBJS) $(LDLIBRARIES) -lV3; \
	elif \
		grep -q inet_wc RMTBRANCH ; \
	then \
		$(CC) -DNO_LARGE_UID $(LDFLAGS) $(CCFLAGS) -o $@ $(OBJS) $(LDLIBRARIES) ; \
	else \
		$(CC) $(LDFLAGS) $(CCFLAGS) $(LF_FLAGS) -o $@ $(OBJS) \
		$(LDLIBRARIES) ; \
	fi

ftpd_source: 
	$(RM) -f $(COMMON_OBJS)

kftpd:  kftpd_source $(OBJS) 
	$(ECHO) "Building secure kftpd..."
	if \
		grep -q -E "(inet_mlp|inet_100)"  RMTBRANCH ; \
	then \
		echo "KFTPD NOT AVAILABLE FOR PRE WC" ; \
	elif \
		grep -q inet_wc  RMTBRANCH ; \
	then \
		$(CC) $(LDFLAGS) $(CCFLAGS) -o $@ $(OBJS) -L ../klib $(LD_KRB5_LIBS) ; \
	else \
		$(CC) $(LDFLAGS) $(CCFLAGS) $(LF_FLAGS) -o $@ $(OBJS) \
		$(LD_KRB5_LIBS) ; \
	fi

kftpd_source: 
	$(RM) -f $(COMMON_OBJS)
	make $(COMMON_OBJS) OTHER_CCFLAGS=-DGSSAPI

vers.c:
	$(SHELL) newvers.sh
	$(CC) $(CCFLAGS) $(LF_FLAGS) -c vers.c

ftpcmd.c: ftpcmd.y
	$(YACC) ftpcmd.y
	$(MV) y.tab.c $@
	
install:
	for file in $(PRODUCTS) ; do \
		$(INSTALLSCPT) $$file $(INSTALLDB) ;  \
	done

.c.o:
	if \
		grep -q -E "(inet_mlp|inet_100)"  RMTBRANCH ; \
	then \
		$(CC) $(CCFLAGS) -c $< ; \
	elif \
		grep -q inet_wc  RMTBRANCH ; \
	then \
		$(CC) $(CCFLAGS) -c $< ; \
	else \
		$(CC) $(CCFLAGS) $(LF_FLAGS) -c $< ; \
	fi

versionlog:
	$(RM) -f version.log
	VERSION=`awk '{print $$2}' version` ; \
	$(ECHO) "Revision $$VERSION contains the following ftpd sources:\n" >version.log
	for i in $(SRCS) ftpcmd.y ; do \
		if [ "$$i" = "vers.c" ] ; then \
			continue; \
		fi ; \
		ident $$i | head -2 | grep Revision | \
		awk ' {printf ("%-20s %-20s\n", FILE, $$2)}' FILE=$$i ; \
	done | sort | pr -2 -t >>version.log
