#############################################################################
# You need to do a successful 'make' in the bsd_src directory first before
# trying to run 'make' in this directory.  The bsd_src directory contains
# several files derived from those found in the ftp.uu.net archive.
# Run "make install" to install binaries.
# Run "make installm" to install man pages.
# Run "make de-install" to remove installed binaries and man pages.

#############################################################################
# This is where you want to install the binary
#
BIN=/usr/local

#############################################################################
# This is where you want to install the man pages, don't worry about it if
# you don't plan to install man pages.
#
MAN=/usr/local/man/man1

#############################################################################
# If you want to provide FSP service, this should be set to the working
# directory of the FSP server.  Don't worry about it and other server-related
# definitions if you do not plan to start a FSP server.
#
DEF_FSP_HOME=/usr/fsp/data

#############################################################################
# If you are going to use the logging option, you can specify a default
# file to recieve the log output here, and then just specify -l on the 
# command line.
DEF_FSP_LOG=/usr/fsp/logs/logfile

#############################################################################
# If the server is to run on a machine with file name length limit, set the
# following variable to the length.  Otherwise, set it to something big.
#
FILE_NAME_LIMIT=99999

#############################################################################
# If you want to provide FSP service, this should be set to the UDP socket
# port number the FSP server should use.
#
DEF_FSP_PORT=21

#############################################################################
# FSP servers normally keep a directory listing file in each directory it
# exports.  In situation where this is not appropriate, FSP can store the
# listing in another directory instead.  DEF_DIRECTORY_CACHE should contain
# a directory where such a listing is to be placed.  The path is relative
# to DEF_FSP_HOME unless it starts with a '/'.  FSP server also uses this
# directory to hold temporary files during upload, so this variable must
# refer to a valid directory even if all subdirectories are writable by
# the fspd process.
DEF_DIRECTORY_CACHE=../cache

# MAX_DIR_CACHE_COUNT should contain the number of listings to be cached in
# DEF_DIRECTORY_CACHE.  The number should be a power of 2.
MAX_DIR_CACHE_COUNT=32

# The following define sets the maximum number of files open at a time
# This helps ease the load on a system from constantly opening and closing
# files.  If the cache is full, the 'oldest' file in the cache will be flushed
# to make way for a new file.  The minimum value for this is 1 which will
# give behavior identical to the old fsp.
MAX_FILE_CACHE = 5

# ALWAYS_USE_CACHE_DIR should be 1 if you want it used as the default way to
# store directory listing files.  (Note, .FSP_CONTENT files will still be
# used if they alreay exist.
ALWAYS_USE_CACHE_DIR=1

#############################################################################
# This is where the version number string comes from:
#
VERSION_STR="Modified Caltech version 2.6.6.5, April 19 1993"

#############################################################################
# FSP uses BSD random/srandom functions.  If your system does not have
# them, include them from the bsd_src directory.
#
#RANDOM=bsd_src/random.o

#############################################################################
# Set the following to 1 if you wish the server to fork into the background
# after startup.
#############################################################################
DAEMONIZE = 0

#############################################################################
# Below are definitions related to the client utilities.
#############################################################################
# If your machine has either lockf or flock functions, you may be able to
# compile the client codes so that multiple client commands can be run
# concurrently.  Uncomment one of the following and set the LOCK_PREFIX
# string appropriately.  In most cases, they are prefixes used to create 
# a lock file in your file system.  There are some restrictions, for example
# on SunOS, lock files may not be on a file system of type tmp.  The most
# efficient locking choice is the one that uses SysV shared memory plus lockf.
# Note, it is possible for some UNIX to have hooks for the locking mechanism
# exist in the C library but unsupported in the kernel.  In such a case,
# compilation will complete normally, but you will get run time errors.
#
#LOCK_FLAGS= -DUSE_SHAREMEM_AND_LOCKF -DKEY_PREFIX=\"/usr/tmp/.FL\"
LOCK_FLAGS= -DUSE_FLOCK -DKEY_PREFIX=\"/usr/tmp/.FL\"
#LOCK_FLAGS= -DUSE_LOCKF -DKEY_PREFIX=\"/usr/tmp/.FL\"
#LOCK_FLAGS= -DNOLOCKING

#############################################################################
# If you wish your clients to time out while not in the middle of a transfer
# uncomment the following line.  Read the ChangeLog file for version 2.6.5jt7
# for details.
CLIENT_TIMEOUT=-DCLIENT_TIMEOUT
#CLIENT_TIMEOUT=

#############################################################################
# This is the path for a system wide default .fsprc file (including filename).
# It is only used by fhostcmd if there is neither "./.fsp_prof" nor
# "~/.fsp_prof"
FSPRCDEFAULT=-DFSPRC=\"/usr/local/fsp/fsp_prof\"

#############################################################################
# Uncomment the following if you wish your fhostcmd to perform a
# mini-nslookup for finding IP numbers or site names.
HOST_LOOKUP = -DHOST_LOOKUP


#############################################################################
# The following variable defines the compiler to use in your compilation.
CC=gcc

#############################################################################
# Uncomment the following if you have dirent.h
# Leave it alone if you don't.
DIRENT = -DDIRENT

#############################################################################
# Set up your compiler flags here.  If you want optimized code, leave it
# the way it is now.
CFLAGS=-g ${DIRENT}
#CFLAGS= -O ${DIRENT}
#CFLAGS= -ggdb ${DIRENT}

#############################################################################
# If you need to link in any additional libraries, do it here.
# AT&T 3B2/600G + AT&T Unix Sys V R 3.2.3 + WIN/TCP R 3.2 use -lnet -lnsl_s 
LIB=

SHELL=/bin/sh

PROGS=	flscmd fcdcmd fgetcmd frmcmd frmdircmd fprocmd \
	fmkdir fput fver fcatcmd fgrabcmd fducmd fhostcmd

IPROGS=	${BIN}/flscmd ${BIN}/fcdcmd \
	${BIN}/fgetcmd ${BIN}/frmcmd ${BIN}/frmdircmd ${BIN}/fprocmd \
	${BIN}/fmkdir ${BIN}/fput ${BIN}/fver ${BIN}/fcatcmd ${BIN}/fgrabcmd \
	${BIN}/fducmd ${BIN}/fhostcmd

IMANS=	${MAN}/fcatcmd.1 ${MAN}/fcat.1 ${MAN}/fcdcmd.1 ${MAN}/fcd.1 \
	${MAN}/fgetcmd.1 ${MAN}/fget.1 ${MAN}/flscmd.1 ${MAN}/fls.1 \
	${MAN}/fmkdir.1 ${MAN}/fprocmd.1 ${MAN}/fpro.1 ${MAN}/fput.1 \
	${MAN}/frmcmd.1 ${MAN}/frm.1 ${MAN}/frmdircmd.1 ${MAN}/frmdir.1 \
	${MAN}/fspd.1 ${MAN}/fver.1 ${MAN}/fgrab.1 ${MAN}/fgrabcmd.1 \
	${MAN}/fducmd.1 ${MAN}/fdu.1 ${MAN}/fhostcmd.1 ${MAN}/fhost.1

all:
	@ cd bsd_src; make CC="${CC}" CFLAGS="${CFLAGS}"
	@ make all1

merge:
	@ cd bsd_src; make CC="${CC}" CFLAGS="${CFLAGS}"
	@ make fspd fsp_client_m

all1:	  ${PROGS} fspd
install:  all ${IPROGS} ${BIN}/fspd
installm: ${IMANS}

install-merge: ${BIN}/fspd fsp_client_m
	- rm -f ${IPROGS} ${BIN}/fsp_client_m
	cp fsp_client_m ${BIN}/fsp_client_m
	for A in ${IPROGS} ; do ln ${BIN}/fsp_client_m $$A; done
	rm -f ${BIN}/fsp_client_m

SERVER_OBJ= server_main.o server_lib.o server_host.o udp_io.o server_file.o \
	    filecache.o
CLIENT_OBJ= client_lib.o udp_io.o client_util.o client_lock.o
GLOB_OBJ= bsd_src/glob.o
UTIL_LS_OBJ= bsd_src/cmp.o bsd_src/ls.o bsd_src/print.o bsd_src/util.o 

#############################################################################

fspd: ${SERVER_OBJ} ${RANDOM}
	${CC} ${CFLAGS} -o fspd ${SERVER_OBJ} ${RANDOM} ${LIB}

fgetcmd: fgetcmd.o ${CLIENT_OBJ} ${GLOB_OBJ}
	${CC} ${CFLAGS} -o fgetcmd fgetcmd.o ${CLIENT_OBJ} ${GLOB_OBJ} ${LIB}

fgrabcmd: fgrabcmd.o ${CLIENT_OBJ} ${GLOB_OBJ}
	${CC} ${CFLAGS} -o fgrabcmd fgrabcmd.o ${CLIENT_OBJ} ${GLOB_OBJ} ${LIB}

fcatcmd: fcatcmd.o ${CLIENT_OBJ} ${GLOB_OBJ}
	${CC} ${CFLAGS} -o fcatcmd fcatcmd.o ${CLIENT_OBJ} ${GLOB_OBJ} ${LIB}

fput: fput.o ${CLIENT_OBJ}
	${CC} ${CFLAGS} -o fput fput.o ${CLIENT_OBJ} ${LIB}

frmcmd: frmcmd.o ${CLIENT_OBJ} ${GLOB_OBJ}
	${CC} ${CFLAGS} -o frmcmd frmcmd.o ${CLIENT_OBJ} ${GLOB_OBJ} ${LIB}

flscmd: flscmd.o ${CLIENT_OBJ} ${UTIL_LS_OBJ}
	${CC} ${CFLAGS} -o flscmd flscmd.o ${CLIENT_OBJ} ${UTIL_LS_OBJ} ${GLOB_OBJ} ${LIB}

fcdcmd: fcdcmd.o ${CLIENT_OBJ} ${GLOB_OBJ}
	${CC} ${CFLAGS} -o fcdcmd fcdcmd.o ${CLIENT_OBJ} ${GLOB_OBJ} ${LIB}

frmdircmd: frmdircmd.o ${CLIENT_OBJ} ${GLOB_OBJ}
	${CC} ${CFLAGS} -o frmdircmd frmdircmd.o ${CLIENT_OBJ} ${GLOB_OBJ} \
									${LIB}

fprocmd: fprocmd.o ${CLIENT_OBJ} ${GLOB_OBJ}
	${CC} ${CFLAGS} -o fprocmd fprocmd.o ${CLIENT_OBJ} ${GLOB_OBJ} ${LIB}

fmkdir: fmkdir.o ${CLIENT_OBJ}
	${CC} ${CFLAGS} -o fmkdir fmkdir.o ${CLIENT_OBJ} ${LIB}

fver: fver.o ${CLIENT_OBJ}
	${CC} ${CFLAGS} -o fver fver.o ${CLIENT_OBJ} ${LIB}

fver.o: fver.c client_def.h common_def.h Makefile
	${CC} ${CFLAGS} -c -DVERSION_STR=\"${VERSION_STR}\" fver.c

fducmd: fducmd.o ${CLIENT_OBJ} ${GLOB_OBJ}
	${CC} ${CFLAGS} -o fducmd fducmd.o ${CLIENT_OBJ} ${GLOB_OBJ} ${LIB}

fhostcmd: fhostcmd.o ${CLIENT_OBJ}
	${CC} ${CFLAGS} -o fhostcmd fhostcmd.o ${CLIENT_OBJ} ${LIB}

#############################################################################

MERGE_OBJ = fcatcmd_m.o fcdcmd_m.o fgetcmd_m.o fgrabcmd_m.o \
	    flscmd_m.o fmkdir_m.o fprocmd_m.o fput_m.o \
	    frmcmd_m.o frmdircmd_m.o fver_m.o fducmd_m.o fhostcmd_m.o

fgetcmd_m.o: fgetcmd.c
	echo '#include "fgetcmd.c"' > fgetcmd_m.c
	${CC} ${CFLAGS} -c -Dmain=fgetcmd_main fgetcmd_m.c
	rm -f fgetcmd_m.c

fgrabcmd_m.o: fgrabcmd.c
	echo '#include "fgrabcmd.c"' > fgrabcmd_m.c
	${CC} ${CFLAGS} -c -Dmain=fgrabcmd_main fgrabcmd_m.c
	rm -f fgrabcmd_m.c

fcatcmd_m.o: fcatcmd.c
	echo '#include "fcatcmd.c"' > fcatcmd_m.c
	${CC} ${CFLAGS} -c -Dmain=fcatcmd_main fcatcmd_m.c
	rm -f fcatcmd_m.c

fput_m.o: fput.c
	echo '#include "fput.c"' > fput_m.c
	${CC} ${CFLAGS} -c -Dmain=fput_main fput_m.c
	rm -f fput_m.c

frmcmd_m.o: frmcmd.c
	echo '#include "frmcmd.c"' > frmcmd_m.c
	${CC} ${CFLAGS} -c -Dmain=frmcmd_main frmcmd_m.c
	rm -f frmcmd_m.c

flscmd_m.o: flscmd.c
	echo '#include "flscmd.c"' > flscmd_m.c
	${CC} ${CFLAGS} -c -Dmain=flscmd_main flscmd_m.c
	rm -f flscmd_m.c

fcdcmd_m.o: fcdcmd.c
	echo '#include "fcdcmd.c"' > fcdcmd_m.c
	${CC} ${CFLAGS} -c -Dmain=fcdcmd_main fcdcmd_m.c
	rm -f fcdcmd_m.c

frmdircmd_m.o: frmdircmd.c
	echo '#include "frmdircmd.c"' > frmdircmd_m.c
	${CC} ${CFLAGS} -c -Dmain=frmdircmd_main frmdircmd_m.c
	rm -f frmdircmd_m.c

fprocmd_m.o: fprocmd.c
	echo '#include "fprocmd.c"' > fprocmd_m.c
	${CC} ${CFLAGS} -c -Dmain=fprocmd_main fprocmd_m.c
	rm -f fprocmd_m.c

fmkdir_m.o: fmkdir.c
	echo '#include "fmkdir.c"' > fmkdir_m.c
	${CC} ${CFLAGS} -c -Dmain=fmkdir_main fmkdir_m.c
	rm -f fmkdir_m.c

fver_m.o: fver.c Makefile
	echo '#include "fver.c"' > fver_m.c
	${CC} ${CFLAGS} -c -Dmain=fver_main -DVERSION_STR=\"${VERSION_STR}\" \
				fver_m.c
	rm -f fver_m.c

fducmd_m.o: fducmd.c Makefile
	echo '#include "fducmd.c"' > fducmd_m.c
	${CC} ${CFLAGS} -c -Dmain=fducmd_main fducmd_m.c
	rm -f fducmd_m.c

fhostcmd_m.o: fhostcmd.c Makefile
	echo '#include "fhostcmd.c"' > fhostcmd_m.c
	${CC} ${CFLAGS} ${FSPRCDEFAULT} ${HOST_LOOKUP} -c \
	-Dmain=fhostcmd_main fhostcmd_m.c
	rm -f fhostcmd_m.c

fsp_client_m: merge.o ${MERGE_OBJ} ${CLIENT_OBJ} ${UTIL_LS_OBJ} ${GLOB_OBJ}
	${CC} -o fsp_client_m merge.o ${MERGE_OBJ} \
		${CLIENT_OBJ} ${UTIL_LS_OBJ} ${GLOB_OBJ} ${LIB}

#############################################################################


${BIN}/fspd     : fspd      ; cp fspd      ${BIN}/fspd
${BIN}/fgetcmd  : fgetcmd   ; cp fgetcmd   ${BIN}/fgetcmd
${BIN}/fgrabcmd : fgrabcmd  ; cp fgrabcmd  ${BIN}/fgrabcmd
${BIN}/fcatcmd  : fcatcmd   ; cp fcatcmd   ${BIN}/fcatcmd
${BIN}/fput     : fput      ; cp fput      ${BIN}/fput
${BIN}/frmcmd   : frmcmd    ; cp frmcmd    ${BIN}/frmcmd
${BIN}/flscmd   : flscmd    ; cp flscmd    ${BIN}/flscmd
${BIN}/fcdcmd   : fcdcmd    ; cp fcdcmd    ${BIN}/fcdcmd
${BIN}/frmdircmd: frmdircmd ; cp frmdircmd ${BIN}/frmdircmd
${BIN}/fprocmd  : fprocmd   ; cp fprocmd   ${BIN}/fprocmd
${BIN}/fmkdir   : fmkdir    ; cp fmkdir    ${BIN}/fmkdir
${BIN}/fver     : fver      ; cp fver      ${BIN}/fver
${BIN}/fducmd   : fducmd    ; cp fducmd    ${BIN}/fducmd
${BIN}/fhostcmd  : fhostcmd   ; cp fhostcmd   ${BIN}/fhostcmd


#############################################################################

${MAN}/fcat.1     : fcatcmd.1  ; cp fcat.1      ${MAN}/fcat.1
${MAN}/fcatcmd.1  : fcatcmd.1  ; cp fcatcmd.1   ${MAN}/fcatcmd.1
${MAN}/fcd.1      : fcdcmd.1   ; cp fcd.1       ${MAN}/fcd.1
${MAN}/fcdcmd.1   : fcdcmd.1   ; cp fcdcmd.1    ${MAN}/fcdcmd.1
${MAN}/fget.1     : fgetcmd.1  ; cp fget.1      ${MAN}/fget.1
${MAN}/fgetcmd.1  : fgetcmd.1  ; cp fgetcmd.1   ${MAN}/fgetcmd.1
${MAN}/fgrab.1    : fgrabcmd.1 ; cp fgrab.1     ${MAN}/fgrab.1
${MAN}/fgrabcmd.1 : fgrabcmd.1 ; cp fgrabcmd.1  ${MAN}/fgrabcmd.1
${MAN}/fls.1      : flscmd.1   ; cp fls.1       ${MAN}/fls.1
${MAN}/flscmd.1   : flscmd.1   ; cp flscmd.1    ${MAN}/flscmd.1
${MAN}/fmkdir.1   : fmkdir.1   ; cp fmkdir.1    ${MAN}/fmkdir.1
${MAN}/fpro.1     : fprocmd.1  ; cp fpro.1      ${MAN}/fpro.1
${MAN}/fprocmd.1  : fprocmd.1  ; cp fprocmd.1   ${MAN}/fprocmd.1
${MAN}/fput.1     : fput.1     ; cp fput.1      ${MAN}/fput.1
${MAN}/frm.1      : frmcmd.1   ; cp frm.1       ${MAN}/frm.1
${MAN}/frmcmd.1   : frmcmd.1   ; cp frmcmd.1    ${MAN}/frmcmd.1
${MAN}/frmdir.1   : frmdircmd.1; cp frmdir.1    ${MAN}/frmdir.1
${MAN}/frmdircmd.1: frmdircmd.1; cp frmdircmd.1 ${MAN}/frmdircmd.1
${MAN}/fspd.1     : fspd.1     ; cp fspd.1      ${MAN}/fspd.1
${MAN}/fver.1     : fver.1     ; cp fver.1      ${MAN}/fver.1
${MAN}/fducmd.1   : fducmd.1   ; cp fducmd.1    ${MAN}/fducmd.1
${MAN}/fdu.1      : fdu1       ; cp fdu.1       ${MAN}/fdu.1
${MAN}/fhostcmd.1: fhostcmd.1; cp fhostcmd.1 ${MAN}/fhostcmd.1
${MAN}/fhost.1 : fhost.1 ; cp fhost.1  ${MAN}/fhost.1

#############################################################################

server_main.o: server_main.c server_def.h common_def.h Makefile
	${CC} ${CFLAGS} -DDEF_FSP_PORT=${DEF_FSP_PORT}	\
		     -DDEF_FSP_HOME=\"${DEF_FSP_HOME}\"	\
		     -DDEF_FSP_LOG=\"${DEF_FSP_LOG}\" \
		     -DVERSION_STR=\"${VERSION_STR}\"	\
		     -DFILE_NAME_LIMIT=${FILE_NAME_LIMIT} \
		     -DFSP_FILE_CACHE=${MAX_FILE_CACHE} \
		     -DDAEMONIZE=${DAEMONIZE} \
		     -DALWAYS_USE_CACHE_DIR=${ALWAYS_USE_CACHE_DIR} \
		     -DMAX_DIR_CACHE_COUNT=${MAX_DIR_CACHE_COUNT} \
		     -DDEF_DIRECTORY_CACHE=\"${DEF_DIRECTORY_CACHE}\"	\
		     -c server_main.c

server_lib.o: server_lib.c server_def.h common_def.h
	${CC} ${CFLAGS} -DFSP_FILE_CACHE=${MAX_FILE_CACHE} -c server_lib.c

server_host.o: server_host.c server_def.h common_def.h
	${CC} ${CFLAGS} -DFSP_FILE_CACHE=${MAX_FILE_CACHE} -c server_host.c

server_file.o: server_file.c server_def.h common_def.h
	${CC} ${CFLAGS} -DFSP_FILE_CACHE=${MAX_FILE_CACHE} -c server_file.c

filecache.o: filecache.c server_def.h
	${CC} ${CFLAGS} -DFSP_FILE_CACHE=${MAX_FILE_CACHE} -c filecache.c

client_lock.o: client_lock.c client_def.h common_def.h Makefile
	${CC} ${CFLAGS} ${LOCK_FLAGS} -c client_lock.c

client_lib.o: client_lib.c client_def.h common_def.h
	${CC} ${CFLAGS} ${CLIENT_TIMEOUT} -c client_lib.c

fhostcmd.o: fhostcmd.c common_def.h
	${CC} ${CFLAGS} ${FSPRCDEFAULT} ${HOST_LOOKUP} -c fhostcmd.c

client_util.o: client_util.c client_def.h common_def.h
fcatcmd.o: fcatcmd.c client_def.h common_def.h
fcdcmd.o: fcdcmd.c client_def.h common_def.h
fgetcmd.o: fgetcmd.c client_def.h common_def.h
fgrabcmd.o: fgrabcmd.c client_def.h common_def.h
flscmd.o: flscmd.c client_def.h common_def.h
fmkdir.o: fmkdir.c client_def.h common_def.h
fprocmd.o: fprocmd.c client_def.h common_def.h
fput.o: fput.c client_def.h common_def.h
frmcmd.o: frmcmd.c client_def.h common_def.h
fver.o: fver.c client_def.h common_def.h
frmdircmd.o: frmdircmd.c client_def.h common_def.h
fducmd.o: fducmd.c client_def.h common_def.h
udp_io.o: udp_io.c common_def.h

clean:
	rm -f *.o fspd fsp_client_m ${PROGS}
	@ cd bsd_src; make clean

de-install:
	rm -f ${BIN}/fspd ${IPROGS} ${IMANS}
