#if 0
#makefile.unx 14-05-2003
#endif

VPATH=$(V_VPATH)

O=.o
C=.c

V_H=0
V_L=99
P_L=21

#define D_P_L 1
DISTRIB=mars_nwe
#if D_P_L
DISTRIBF=$(DISTRIB)-$(V_H).$(V_L).pl$(P_L)
PATCHF=$(DISTRIBF)
#else
P_L=0
DISTRIBF=$(DISTRIB)-$(V_H).$(V_L)
PATCHF=$(DISTRIBF).pl$(P_L)
#endif
STERN=*

MAKEPATCH=diff -rub --new-file

DESTMAKEFILE=Makefile.o
#if 0
all:	$(DESTMAKEFILE)
	$(MAKE) -f $(DESTMAKEFILE) n_$@

routed: $(DESTMAKEFILE)
	$(MAKE) -f $(DESTMAKEFILE) n_$@

clean:  $(DESTMAKEFILE)
	$(MAKE) -f $(DESTMAKEFILE) n_$@

distclean:  $(DESTMAKEFILE)
	$(MAKE) -f $(DESTMAKEFILE) n_$@

install:  $(DESTMAKEFILE)
	$(MAKE) -f $(DESTMAKEFILE) n_$@

reboot:  $(DESTMAKEFILE)
	 $(MAKE) -f $(DESTMAKEFILE) n_$@

install_ini:  $(DESTMAKEFILE)
	$(MAKE) -f $(DESTMAKEFILE) n_$@

diff:   $(DESTMAKEFILE)
	$(MAKE) -f $(DESTMAKEFILE) n_$@

showconf:   $(DESTMAKEFILE)
	$(MAKE) -f $(DESTMAKEFILE) n_$@

distrib:  $(DESTMAKEFILE)
	$(MAKE) -f $(DESTMAKEFILE) n_$@

distrib_bin:  $(DESTMAKEFILE)
	$(MAKE) -f $(DESTMAKEFILE) n_$@

bintgz: $(DESTMAKEFILE)
	$(MAKE) -f $(DESTMAKEFILE) n_$@

$(DESTMAKEFILE): $(VPATH)/makefile.unx $(VPATH)/config.h
	rm -f $@
	cp $(VPATH)/makefile.unx makefile.c
	$(CPP) $(HOSTCFLAGS) -I$(VPATH) makefile.c > $@
	rm -f makefile.c
#endif


PROG1=
PROG2=nwserv
PROG3=nwconn
PROG4=ncpserv
PROG5=nwclient
PROG6=nwbind
PROG7=nwrouted
PROG8=dbmtool
PROG9=ftrustee

#include "config.h"
#ifdef FILENAME_NW_INI
M_FILENAME_NW_INI=FILENAME_NW_INI
#else
M_FILENAME_NW_INI="./nw.ini"
#endif

#ifdef PATHNAME_PROGS
M_PATHNAME_PROGS=PATHNAME_PROGS
#else
M_PATHNAME_PROGS="."
#endif

M_PATHNAME_BINDERY=PATHNAME_BINDERY

#ifdef  LINUX
# ifdef FREEBSD
EMUTLIOBJ=emutli$(O) ipxif$(O)
NWROUTE_O=nwroute1$(O)
NWROUTED=
# else
#  ifndef INTERNAL_RIP_SAP
#	define INTERNAL_RIP_SAP  1
#  endif
EMUTLIOBJ=emutli$(O)
#  if INTERNAL_RIP_SAP
EMUTLIOBJ1=emutli1$(O)
NWROUTE_O=nwroute$(O)
#  else
EMUTLIOBJ1=
NWROUTE_O=nwroute1$(O)
NWROUTED=$(PROG7)
#  endif
# endif
#else
NWROUTE_O=nwroute1$(O)
NWROUTED=
#endif

INSTALLPROGS=$(PROG1) $(PROG2) $(PROG3) $(PROG4) $(PROG5) $(PROG6) $(NWROUTED)
PROGS=$(INSTALLPROGS) $(PROG8) $(PROG9)

OBJ1= $(EMUTLIOBJ) net1$(O) tools$(O)
OBJ2= $(OBJ1) $(EMUTLIOBJ1) $(NWROUTE_O)
OBJ3= $(OBJ1) connect$(O) namspace$(O) nwvolume$(O) nwfile$(O) unxfile$(O) \
nwqconn$(O) nameos2$(O) nwfname$(O) nwshare$(O) extpipe$(O) \
nwattrib$(O) trustee$(O)
OBJ4= $(OBJ1)
OBJ5= $(OBJ1)
OBJ6= $(OBJ1) nwdbm$(O) nwcrypt$(O) unxlog$(O) sema$(O) nwqueue$(O) unxfile$(O)
OBJ7= $(OBJ1) emutli1$(O)
OBJ8= $(OBJ6)
OBJ9= tools$(O) nwfname$(O) unxfile$(O) nwvolume$(O) nwattrib$(O) trustee$(O)

OBJS= $(EMUTLIOBJ) net1$(O) tools$(O) \
	  $(EMUTLIOBJ1) $(NWROUTE_O) \
	  connect$(O) namspace$(O) nwvolume$(O) nwfile$(O) unxfile$(O)\
	  nwqconn$(O) nameos2$(O) nwfname$(O) nwshare$(O) extpipe$(O) \
	  nwattrib$(O) trustee$(O) \
	  nwdbm$(O) nwcrypt$(O) unxlog$(O) sema$(O) nwqueue$(O)  \
	  $(PROG2)$(O) $(PROG3)$(O) $(PROG4)$(O) $(PROG5)$(O) $(PROG6)$(O) \
	  $(PROG7)$(O) $(PROG8)$(O) $(PROG9)$(O)

HOBJ3= $(PROG3)$(O) connect$(O) namspace$(O) nwvolume$(O) nwfile$(O) \
	   unxfile$(O)

HOBJ6= $(PROG6)$(O) sema$(O)

#if 0
#$(PROG1):  $(PROG1)$(O) $(OBJ1)
#	   $(CC) -o $(VPATH)/$(PROG1) $(PROG1)$(O) $(OBJ1) $(CRYPTLIB) $(NSLLIB)
#endif

$(PROG2):  $(PROG2)$(O) $(OBJ2)
	   $(CC) -o $(VPATH)/$(PROG2) $(PROG2)$(O) $(OBJ2) $(CRYPTLIB) $(NSLLIB)

#if 0
#$(CC) -o $(VPATH)/$(PROG2) $(PROG2)$(O) $(OBJ2) $(NDBMLIB) $(CRYPTLIB) $(NSLLIB)
#endif

$(PROG3):  $(PROG3)$(O) $(OBJ3)
	   $(CC) -o $(VPATH)/$(PROG3) $(PROG3)$(O) $(OBJ3) $(CRYPTLIB) $(NSLLIB)

$(PROG4):  $(PROG4)$(O) $(OBJ4)
	   $(CC) -o $(VPATH)/$(PROG4) $(PROG4)$(O) $(OBJ4) $(CRYPTLIB) $(NSLLIB)

$(PROG5):  $(PROG5)$(O) $(OBJ5)
	   $(CC) -o $(VPATH)/$(PROG5) $(PROG5)$(O) $(OBJ5) $(CRYPTLIB) $(NSLLIB)

$(PROG6):  $(PROG6)$(O) $(OBJ6)
	   $(CC) -o $(VPATH)/$(PROG6) $(PROG6)$(O) $(OBJ6) $(NDBMLIB) $(CRYPTLIB) $(NSLLIB)

$(PROG7):  $(PROG7)$(O) $(OBJ7)
	   $(CC) -o $(VPATH)/$(PROG7) $(PROG7)$(O) $(OBJ7) $(CRYPTLIB) $(NSLLIB)

$(PROG8):  $(PROG8)$(O) $(OBJ8)
	   $(CC) -o $(VPATH)/$(PROG8) $(PROG8)$(O) $(OBJ8) $(NDBMLIB) $(CRYPTLIB) $(NSLLIB)

$(PROG9):  $(PROG9)$(O) $(OBJ9)
	   $(CC) -o $(VPATH)/$(PROG9) $(PROG9)$(O) $(OBJ9) $(CRYPTLIB) $(NSLLIB)

$(HOBJ3):  namspace.h connect.h nwvolume.h nwfile.h
$(HOBJ6):  nwbind.h sema.h
$(OBJS):   net.h config.h
$(PROG7)$(O): nwserv.c nwroute.c
tools$(O): $(DESTMAKEFILE)

$(C)$(O):
	$(CC) -c $(CFLAGS) $(HOSTCFLAGS)\
 -D_VERS_H_=$(V_H) -D_VERS_L_=$(V_L) -D_VERS_P_=$(P_L) $<

n_all:   $(PROGS)
	 @echo "don't forget to do a 'make install' as root !" >> $(VPATH)/.mk.notes
	 @echo "please take a look into doc/NEWS !" >> $(VPATH)/.mk.notes
	 @-head -n 5 $(VPATH)/doc/NEWS >> $(VPATH)/.mk.notes

n_routed: $(PROG7)

n_install_ini:
	 cd $(VPATH) && $(INSTALL) -m 664 nw.ini $(M_FILENAME_NW_INI) && cd $(OBJDIR)

n_install:
	cd $(VPATH)  && $(INSTALL) -d $(M_PATHNAME_PROGS) && cd $(OBJDIR)
	cd $(VPATH)  && $(INSTALL) $(INSTALLPROGS) $(M_PATHNAME_PROGS) && cd $(OBJDIR)
	@-if [ -r /sbin/nwserv ] && [ -r /usr/sbin/nwserv ] ; then \
		  echo "remove old version in /sbin ?" ; \
		  (cd /sbin && rm -i nwserv nwbind ncpserv nwconn nwclient nwrouted) ; \
		fi ;
	@-if [ -r /etc/nwobj.pag ] ; then \
		  ( \
	  if [ ! -r $(M_PATHNAME_BINDERY) ] ; then \
		   if $(VPATH)/tools.sh yesno "move bindery files from /etc to $(M_PATHNAME_BINDERY) ?" ; then \
			 mkdir -p $(M_PATHNAME_BINDERY); \
			 (cd /etc && mv \
			 nwiobj.pag nwiobj.dir nwobj.pag nwobj.dir \
			 nwprop.pag nwprop.dir nwval.pag nwval.dir \
			 $(M_PATHNAME_BINDERY) ) ; \
		   fi; \
		  fi; \
		  ) \
		fi ;
	@cd $(VPATH) && (if [ -r $(M_FILENAME_NW_INI) ] ; then \
echo ""; \
echo "********************************************************"; \
echo ""; \
echo "$(M_FILENAME_NW_INI) exist."; \
echo "to overwrite it with nw.ini, make install_ini";\
echo ""; \
echo "********************************************************"; \
echo ""; \
else \
$(INSTALL) -m 664 nw.ini $(M_FILENAME_NW_INI); \
echo ""; \
echo "********************************************************"; \
echo ""; \
echo "$(M_FILENAME_NW_INI) created from nw.ini"; \
echo ""; \
echo "********************************************************"; \
echo ""; \
fi; \
echo "If you have problems, please read 'doc/BUGS' before"; \
echo "sending mail to mailinglist or me."; \
echo ""; \
echo "********************************************************"; \
cd $(OBJDIR) )

n_reboot: n_install
	  -(nwserv -k ; nwserv)&

clean_d:
	cd $(VPATH) && (\
   find $(DISTRIB) \( -name .e.pck -o -name '~*' -o -name '*~' -o -name core \) \
   -exec rm {} \; \
 ; cd $(OBJDIR))

n_clean:
	rm -f *.o
	cd $(VPATH) && (rm -f $(PROGS) $(PROG7) \
; rm -rf $(OBJDIR)/$(VPATH)/$(DISTRIB) \
; cd $(OBJDIR) )

n_distclean: n_clean clean_d
	cd $(VPATH) && (rm -f *.dir *.pag; cd $(OBJDIR))

make_dir:
	cd $(VPATH) && (rm -rf $(OBJDIR)/$(VPATH)/$(DISTRIB) \
; mkdir $(DISTRIB) \
; mkdir $(DISTRIB)/examples \
; mkdir $(DISTRIB)/doc \
; ln -f  \
 $(STERN).[ch] \
 makefile.unx \
 tools.sh \
 Makefile \
 COPYING  \
 README  \
 $(DISTRIB)/. \
; rm -f  $(DISTRIB)/config.h \
; ln -f  \
 examples/$(STERN) \
 $(DISTRIB)/examples/. \
; ln -f  \
 doc/$(STERN) \
 $(DISTRIB)/doc/. \
; cd $(OBJDIR) )

n_diff: make_dir clean_d
	cd $(VPATH) && ( \
 $(MAKEPATCH) org/$(DISTRIB) $(DISTRIB) > $(PATCHF) \
; gzip -9 -f $(PATCHF) \
; cd $(OBJDIR) )

n_distrib: n_diff
	-mkdir /tmp/x
	cd $(VPATH) && ./mk_lsm "$(V_H).$(V_L).pl$(P_L)" $(DISTRIBF) \
	&& (tar cvzf $(DISTRIBF).tgz $(DISTRIB) \
; uue $(DISTRIBF).tgz; mv -f $(DISTRIB)-$(V_H).uue $(DISTRIBF).uue \
; cp -a $(DISTRIB)/doc/$(DISTRIB).lsm /tmp/yy \
; echo "" >> /tmp/yy \
; echo "" >> /tmp/yy \
; cat $(DISTRIBF).uue >> /tmp/yy \
; chmod 664 /tmp/yy \
; rm  $(DISTRIBF).uue \
; mv  $(DISTRIBF).tgz /tmp/x/. \
; cp -a $(DISTRIB)/doc/$(DISTRIB).lsm /tmp/x/. \
; mv $(PATCHF).gz /tmp/x/. \
; cd $(OBJDIR) )
	-rm -f /tmp/mars_nwe.tgz
	ln -s /tmp/x/$(DISTRIBF).tgz /tmp/mars_nwe.tgz

n_distrib_bin:
	cd $(VPATH) && (/usr/local/bin/cdar cb /tmp/mars_nwb.cda \
 $(PROGS) \
 examples \
 doc \
; cd $(OBJDIR))

n_bintgz:
	cd $(VPATH) && (tar cvzf /tmp/mars_nwb.tgz \
 $(PROGS) \
; cd $(OBJDIR))

n_showconf:
	echo "#" $(M_FILENAME_NW_INI)
	grep "^[ \t]*[0-9]" $(M_FILENAME_NW_INI)




