#
#  Makefile for Libra FTP daemon
#
#  Copyright (C) 1999   Andrey Savochkin (saw@saw.sw.com.sg)
#                       Maxim Schesterikoff (libraftp@narod.ru)
#
#  See GPL for the license terms.
#
#  $Id: Makefile,v 1.46 2008/05/23 10:44:01 shesmax Exp $
#

VERSION = 1.3.6

CC = gcc
COPTFLAGS = -g -O2
CFLAGS = $(strip -W -Wall -Wno-parentheses $(COPTFLAGS) $(CFLAGS_$(basename $(@))))
SGML2MAN = sgml2txt -man
SGML2HTML= sgml2html
SGML2TXT = sgml2txt

all: ftps doc

#
# Determination of byte order
#

.byteorder: little_endian
	@if ./little_endian; then \
		echo LOWFIRST > .byteorder; \
	else \
		echo HIGHFIRST > .byteorder; \
	fi
	
little_endian: little_endian.o
	$(CC) -o $@ little_endian.o

#
# Making executable file
#

OBJECTS = libra.o parser.o pi.o \
	  dtp.o dataopen.o dataretr.o dtpls.o port_pasv.o inet6_xtoy.o chkport.o
ifndef NO_UPLOADS
OBJECTS += datastor.o
else
OBJECTS += datanostor.o
endif
OBJECTS += accsctrl.o \
	  dir.o file.o listdir.o \
	  site.o mlsx.o \
	  log.o sockets.o xbuf.o md5.o utf8.o

ftps: $(OBJECTS)
	$(CC) -o $(@) -static $(OBJECTS)

CFLAGS_pi = -DLIBRA_VERSION="\"$(VERSION) $(shell date '+%Y%m%d')\""
CFLAGS_md5 = -D$(shell cat .byteorder)

%.o: %.c
	$(CC) -c $(CFLAGS) -o $(@) $(<)

pi.o: Makefile
md5.o: .byteorder

-include .depend

#
# Manual dependency make
#

dep:
	$(CC) -MM *.c > .depend

#
# Making documentation
#

doc: man html 
man: ftps.man
html: approaches.html ftps.html

%.man: %.sgml
	$(SGML2MAN) $(<)
%.html: %.sgml
	$(SGML2HTML) $(<)

ftps.man: ftps.sgml
approaches.html: approaches.sgml
ftps.html: ftps.sgml

cxref:
	$(CXREF) $(patsubst %.o,%.c,$(OBJECTS)) *.h -O ../libra-doc \
		-xref-file -xref-func -xref-var \
		-index-file -index-func -index-var -html20-src

#
# Releasing
#

release: sources binaries

sources:
	dir=`basename $${PWD}`; \
	tar c -C .. -zf libra-ftpd-$(VERSION).tgz \
      --exclude CVS --exclude '*.tgz' \
		--exclude ftps --exclude little_endian --exclude .byteorder \
		--exclude '*.o' --exclude '*.man' --exclude '*.html' --exclude .depend \
		$${dir}

binaries:
	dir=`basename $${PWD}`; \
	tar c -C .. -zf libra-ftpd-$(VERSION).`uname -m`.tgz \
		--exclude '*.c' --exclude '*.h' --exclude '*.o' --exclude '*.sgml' \
      --exclude Makefile --exclude Problems \
      --exclude '*.spec' --exclude CVS --exclude CVSLog --exclude GPL \
      --exclude '*.tgz' --exclude little_endian \
      --exclude .byteorder --exclude .vimrc --exclude .depend \
		$${dir}

clean:
	rm -f ftps little_endian .byteorder .depend *.{o,man,html}

cvslog:
	rcs2log -u "shesmax	Maxim Shesterikov	libraftp@narod.ru" \
   -u "saw	Andrey Savochkin	saw@saw.sw.com.sg" \
   >CVSLog
   
.PHONY: all doc man html dep release sources binaries clean cxref cvslog
