#
# The contents of this file are subject to the AOLserver Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://aolserver.com/.
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is AOLserver Code and related documentation
# distributed by AOL.
# 
# The Initial Developer of the Original Code is America Online,
# Inc. Portions created by AOL are Copyright (C) 1999 America Online,
# Inc. All Rights Reserved.
#
# Alternatively, the contents of this file may be used under the terms
# of the GNU General Public License (the "GPL"), in which case the
# provisions of GPL are applicable instead of those above.  If you wish
# to allow use of your version of this file only under the terms of the
# GPL and not to allow others to use your version of this file under the
# License, indicate your decision by deleting the provisions above and
# replace them with the notice and other provisions required by the GPL.
# If you do not delete the provisions above, a recipient may use your
# version of this file under either the License or the GPL.
# 
#
# $Header: /cvsroot/aolserver/aolserver/include/ns.mak.in,v 1.5 2006/06/27 17:00:55 jgdavidson Exp $

#
# ns.mak.in --
#
#	Makefile include for AOLserver modules, libraries, and programs.  Variables to set
#	before including this file include:
#
#	AOLSERVER	AOLserver install directory
#	DLL		Root name of dynamic library (without lib prefix or extension)
#	OBJS		List of library object files (required with DLL)
#	DLLINIT		Dynamic library entry point
#	DLLLIBS		Extra dynamic library link libraries
#	MOD		Name of module
#	MODOBJS		List of module object files (required with MOD)
#	MODINIT		Module entry point
#	MODLIBS		Extra module link libraries
#	PGM		Name of executable program
#	PGMOBJS		List of program object files (required with PGM)
#	PGMLIBS		Extra program link libraries
#	HDRS		Header files which objects depend on
#	INSTALL		Extra install directive(s)
#	CLEAN		Extra clean directive(s)
#
#	At least one of PGM, MOD, or DLL must be defined.
#
#	See ../nsd/Makefile and ../nsdb/Makefile for examples.
#	

#
# Install directories.
#

AOLSERVER	= @AOLSERVER@
TCLSH		= @TCLSH@
INSTBIN		= $(AOLSERVER)/bin
INSTLIB		= $(AOLSERVER)/lib
INSTINC		= $(AOLSERVER)/include
INSTTCL         = $(AOLSERVER)/modules/tcl
INSTSRV         = $(AOLSERVER)/servers/server1
INSTSRVMOD      = $(INSTSRV)/modules
INSTSRVPAG      = $(INSTSRV)/pages
SH		= /bin/sh

#
# Compiler and linker options.
#

LIBEXT		= @TCL_SHLIB_SUFFIX@
LDLIB		= @LDLIB@
LDSO		= @LDSO@
CCRFLAG         = @CCRFLAG@
LDRFLAG         = @LDRFLAG@
CCRPATH         += @CCRPATH@
LDRPATH         += @LDRPATH@
CC		= $(PURIFY) @TCL_CC@
CFLAGS_DEBUG	= @TCL_CFLAGS_DEBUG@
CFLAGS_OPTIMIZE = @TCL_CFLAGS_OPTIMIZE@
CFLAGS_WARNING	= @TCL_CFLAGS_WARNING@
CFLAGS_INCLUDE	= -I$(INCDIR) @TCL_INCLUDE_SPEC@
CFLAGS_EXTRA	= @TCL_SHLIB_CFLAGS@ @TCL_EXTRA_CFLAGS@
DEFS      	= -DNO_CONST @TCL_DEFS@ @DEFS@
CFLAGS		+= @CFLAGS_DEFAULT@ $(CFLAGS_WARNING) $(CFLAGS_EXTRA) $(CFLAGS_INCLUDE) @CPPFLAGS@ $(DEFS)
ifndef SRCDIR
    UTILS	 = $(AOLSERVER)/bin
    LIBS	+= -L$(AOLSERVER)/lib -lnsd -lnsthread
    INCDIR	 = $(AOLSERVER)/include
else
    UTILS	 = $(SRCDIR)/util
    INCDIR	 = $(SRCDIR)/include
    ifneq (nsthread,$(DLL))
    	ifneq (nsd,$(DLL))
            ifneq (nsthreadtest,$(PGM))
                LIBS += -L$(SRCDIR)/nsd -lnsd
            endif
    	endif
    	LIBS += -L$(SRCDIR)/nsthread -lnsthread
    endif
endif
LIBS		+= @TCL_LIB_SPEC@ @TCL_LIBS@ @LDFLAGS@ @LIBS@
CCLIBS		 = $(LIBS) @TCL_LD_FLAGS@

#
# Shell utilities.
#

RANLIB	  = @TCL_RANLIB@
MAKEALL	  = $(TCLSH) $(UTILS)/nsmakeall.tcl
RM        = $(TCLSH) $(UTILS)/nsremove.tcl
INST	  = $(TCLSH) $(UTILS)/nsinstall.tcl
INSTMAN   = $(SH) $(UTILS)/nsinstall-man.sh -v -s
INSTALL_SH = $(UTILS)/install-sh -c
INSTALL_DATA = $(INSTALL_SH) -m 644

#
# Platform-specific options.
#

uname = $(shell uname -a)

# Solaris 2.6+
ifneq (,$(findstring SunOS,$(uname)))
    ifneq (,$(findstring 5.6,$(uname)))
        LIBS+=-lthread -lposix4
    else
        LIBS+=-lrt
    endif
endif

# Mac OS X
ifneq (,$(findstring Darwin,$(uname)))
    LDFLAGS     += @TCL_EXTRA_CFLAGS@
endif

# Add dynamic library entry point flags.
ifdef DLLINIT
    ifneq (,$(findstring Darwin,$(uname)))
	INIT	 =  _$(DLLINIT)
	LDLIB	+= -init _$(INIT)
    else
	INIT	 =  _init
    endif
endif
 
ifdef MOD
    DLL		 = $(MOD)
    MODBIN	 = $(MOD).so
    BUILD	+= $(MODBIN)
    INSTALL 	+= install-mod
    CLEAN	+= clean-mod
    ifdef MODINIT
	MODSRC	 = $(INCDIR)/nsmodinit.c
	MODOBJ	 = $(MOD)_modinit.o
	MODOBJS	+= $(MODOBJ)
    endif
endif

ifdef DLL
    DLLBIN	 = lib$(DLL)$(LIBEXT)
    DLLLIB	 = lib$(DLL).a
    OBJS	+= $(UNIXOBJS)
    ifdef DLLINIT
	LIBSRC	 = $(INCDIR)/nslibinit.c
	LIBOBJ	 = $(DLL)_libinit.o
    endif
    BUILD	+= $(DLLLIB) $(DLLBIN)
    INSTALL	+= install-dll
    CLEAN  	+= clean-dll
    MODLIBS	+= -L. -l$(DLL)
    PGMLIBS	+= -L. -l$(DLL)
endif

ifdef PGM
    BUILD	+= $(PGM)
    INSTALL	+= install-pgm
    CLEAN	+= clean-pgm
endif

ifdef MAN
    INSTALL	+= install-man install-html
endif

all build: $(BUILD)

install: $(INSTALL)

clean: $(CLEAN)

$(DLLBIN): $(OBJS) $(LIBOBJ)
	$(RM) $(DLLBIN)
	$(LDLIB) $(LDFLAGS) -o $(DLLBIN) $(OBJS) $(LIBOBJ) \
		$(DLLLIBS) $(LIBS) @LDRPATHS@

$(DLLLIB): $(OBJS)
	$(AR) rv $(DLLLIB) $(OBJS)
	$(RANLIB) $(DLLLIB)

$(MODBIN): $(MODOBJS) $(DLLBIN)
	$(RM) $(MODBIN)
	$(LDSO) $(LDFLAGS) -o $(MODBIN) $(MODOBJS) $(MODLIBS) $(LIBS) @LDRPATHS@

$(MODOBJ): $(MODSRC)
	$(CC) $(CFLAGS) -DNS_MODINIT=$(MODINIT) -c -o $(MODOBJ) $(MODSRC)

$(LIBOBJ): $(LIBSRC)
	$(CC) $(CFLAGS) -DNS_INIT=$(INIT) -DNS_LIBINIT=$(DLLINIT) -c -o $(LIBOBJ) $(LIBSRC)

$(PGM): $(PGMOBJS) $(DLLLIB) $(DLLBIN)
	$(RM) $(PGM)
	$(CC) $(LDFLAGS) -o $(PGM) $(PGMOBJS) \
		$(PGMLIBS) $(CCLIBS) @CCRPATHS@

$(MODOBJS) $(OBJS) $(PGMOBJS): $(HDRS) $(INCDIR)/ns.h $(INCDIR)/nsthread.h

install-mod: $(MODBIN)
	$(INST) -d $(INSTBIN) -e $(MODBIN)

install-dll: $(DLLBIN)
	$(INST) -d $(INSTLIB) -e $(DLLBIN)
	$(INST) -d $(INSTLIB) $(DLLLIB) 
	$(RANLIB) $(INSTLIB)/$(DLLLIB)

install-pgm: $(PGM)
	$(INST) -d $(INSTBIN) -e $(PGM)

install-man:
	for i in $(MAN); do \
		$(INSTMAN) $$i $(AOLSERVER)/man; \
	done

install-html:
	for i in $(MAN); do \
		$(INSTMAN) -h $$i $(AOLSERVER)/html; \
	done

clean-mod:
	$(RM) $(MODBIN) $(MODOBJS)

clean-dll:
	$(RM) $(DLLBIN) $(DLLLIB) $(OBJS) $(LIBOBJ)

clean-pgm:
	$(RM) $(PGM) $(PGMOBJS)

.PHONY: clean-mod clean-dll clean-pgm install-man1 install-man3 install-mann
