# [Makefile wk 6.6.91] W-Editor
#   Copyright (c) 1991 by Werner Koch (dd9jn)
# This file is part of the W-Editor.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#--------------------------------------------------
# Targets:
#
#   world     -- build all versions
#   install   -- install all needed file for a delivery
#
#   os2       -- build OS/2 versions
#   msdos     -- build MSDOS versions
#   doc       -- build documentation
#   linux     -- build linux-version
#   winnt     -- build win-nt version
#
#--------------------------------------------------
# History:
#  03.01.93 wk	GNU/EMX Support
#  02.11.93 wk	Umgestellt auf GNU-make


# Try to get some standard macros
-include $(WKLIBS)/wklib/config.mak
ifndef WKTOOLS_DIR
WKTOOLS_DIR := /sahara/apps/tools/src
endif

ifndef PROD
D=1
endif

include $(WKLIBS)/wklib/wklib.mak

X = $(exe_dir)/
O = $(obj_dir)/
C = $(target_prefix)/common/

ifdef host_bin_dir
IPF2DOC := $(host_bin_dir)/ipf2doc
else
IPF2DOC := ipf2doc
endif

########################################
# Infos for target "install":
########################################
ifdef host_is_os2
INSTALL_PATH=/usr/deliver
DOS4GW_PATH=c:/watcom/bin
TOOLS_SRC_PATH=/usr/apps/tools
TOOLS_EXE_PATH=/usr/apps/tools/WF3
else
BINDIR=/usr/local/bin
LIBDIR=/usr/local/lib
MANDIR=/usr/local/man
INSTALL_PATH=/vol1/OS2/D/usr/deliver
DOS4GW_PATH=/vol1/OS2/E/watcom/bin
TOOLS_SRC_PATH=/vol1/OS2/D/usr/apps/tools
TOOLS_EXE_PATH=/vol1/OS2/D/usr/apps/tools/WF3
endif
########################################

DEVEL_TOOLS := ldsh.c ldsh.cfg update.c ipf2doc.c jntex.c Makefile

#---- list of source files ------
SRCS := Makefile
SRCS += version.h
SRCS += w.c	    #-- main module
SRCS += w.h	    #-- main definitions
SRCS += wtailor.h   #-- tailoring file
SRCS += wkbddrv.c   #-- low level keyboard handler
SRCS += wkbddrv.h   #-- defs for low level keyboard handler
ifdef msdos_like_system
SRCS += wscrdrv.c   #-- Screen driver (PC based)
else
SRCS += xinterface.c
SRCS += wscrans.c   #-- Screen driver (ANSI based )
SRCS += vt_linux.c  #-- Linux virtual terminal interface
endif
# SRCS += wpmdrv.c    #-- Screen Driver (OS/2 GUI PM)
# SRCS += wscrdrvi.h  #-- Resource IDs
# SRCS += wscrcur.c   #-- Screen driver (Curses based )
ifeq ($(SYSTEM),os2)
SRCS += w.rc	    #-- Resource file
SRCS += w.def	    #-- link definitions
SRCS += wmini.def   #-- link definitions
endif
SRCS += wz.def	    #-- link definitions for ZPM
SRCS += wcmd.c	    #-- Command functions
SRCS += wcmdtbl.h   #-- internal to wcmd.c, commands
SRCS += wcmd.h	    #-- Command definitions
SRCS += wcmdpr.h    #-- Prototypes
SRCS += wproc.c     #-- Dispatcher
SRCS += wscreen.c   #-- Screen output
SRCS += wscreen.h   #-- Defs for screen output
SRCS += hilite.c
SRCS += hilite.h
SRCS += wfile.c     #-- File handling
SRCS += wfile.h     #-- Defs for File handling
SRCS += wmove.c     #-- Positioning
SRCS += wmove.h     #-- Defs for wmove.c
SRCS += wmark.c     #-- most of the mark operations
SRCS += wvar.c	    #-- variable processing
SRCS += wsrch.c     #-- Search processing
SRCS += wio.c	    #-- Input/output to file system
SRCS += wdos.c	    #-- DOS commands and support functions
SRCS += wedit1.c    #-- edit primitives 1
SRCS += wedit2.c    #-- edit primitives 2
SRCS += wedit3.c    #-- edit primitives 3 ( split / join )
SRCS += wedit4.c    #-- edit primitives 4 ( set tabs .... )
SRCS += wedit5.c    #-- edit primitives 5 ( reflow ... )
SRCS += wedit6.c    #-- edit primitives 6 (match block delim )
SRCS += wedit7.c    #-- edit primitives 7 (show tree)
SRCS += wedit8.c    #-- edit primitives 7 (copy/cut/paste)
SRCS += wbug.c	    #-- BugDB Support
SRCS += w.pro	    #-- default profile
SRCS += w.hlp	    #-- online help
SRCS += wmini.pro   #-- online help
ifdef W_MINI
SRCS += msstub.c    #-- stub for wmini
endif

#----- end of source list -----

OBJS := $(strip $(addprefix $(obj_dir)/, \
			    $(patsubst %.c,%$o,$(filter %.c,$(SRCS)))))


ifeq ($(COMPANY),ztc)
###
####ifeq ($(MODEL),X)	# ztc definiert das OS bereist selbs (DOS386)
####CFLAGS_1 := -r -R -m$(MODEL) -w2  -c
####else
####CFLAGS_1 := -r -R -m$(MODEL) -w2  -c -D$(OS)
####endif
####ifdef W_MINI
####CFLAGS_1 := $(CFLAGS_1) -DW_MINI
####endif
####ifdef EMS_VERSION
####CFLAGS_1 := $(CFLAGS_1) -DUSE_EMS
####endif
####ifneq ($(MODEL),X)
####ifneq ($(MODEL),Z)
####CFLAGS_1 := $(CFLAGS_1) -g
####endif
####endif
####ifdef DEBUG
####CFLAGS = $(CFLAGS_1) -D -DMEM_DEBUG -o$@
####else
####CFLAGS = $(CFLAGS_1) -o -o$@
####endif
endif  #-- ZTC --

###ifeq ($(CCTYPE),IBM)
###
###ifdef DEBUG
###CFLAGS = -c -D$(OS) -DDEBUG -DMEM_DEBUG -Ti -Fo$@
###else
###CFLAGS = -c -D$(OS) -O -Fo$@
###endif
###
###endif



#-- some additional object modules --

###ifeq ($(CCTYPE),ZTC)
###ifeq ($(MODEL),X)
###ADD_OBJS := $(ADD_OBJS) cx$o
###endif
###
###ifdef W_MINI
###ADD_OBJS := $(ADD_OBJS) int$o
###endif
###
###ifeq ($(MODEL),L)
###ifeq ($(OS),MSDOS)
###ADD_OBJS := $(ADD_OBJS) _swapl$o
###endif
###endif
###
###endif #-- ZTC --


ifndef msdos_like_system
# ldsh append everything between double dashes to the linker (cc)
# without interpreting it.
ADD_LIBS := $(ADD_LIBS) -- -L/usr/X11R6/lib -lX11
endif

LDSH_FLAGS += -DAuthor=Werner.Koch@guug.de

#######  dependencies  ##########

.PHONY: main clean doc linux os2 winnt msdos world install linux install-linux dist \
	make-dirs

default: check-args main

main : $Xw$x

$Xw$x	: $(OBJS) w.def

$Xscan$x : $Oscan$o

$Xtinyw$x   : $Otinyw$o


#$(OBJPATH)/w.res : w.rc w.ico

### IBMs ipfc does only work in the current directory
### so we must move the file after processing (assume OS/2 host)
### ( we use copy, becaus move does not allow to overwrtite a file)
$Cw.inf: w.ipf w0.ipf w1.ipf w2.ipf w3.ipf w4.ipf w5.ipf w6.ipf w7.ipf
	 ipfc /inf w.ipf
	 update -f w.inf $Cw.inf
	 del w.inf

$Cw.ref $Cw.rfi: w.ipf w0.ipf w1.ipf w2.ipf w3.ipf w4.ipf w5.ipf w6.ipf w7.ipf
	$(IPF2DOC) -i$Cw.rfi w.ipf >$Cw.ref

$Owcmd$o   :  wcmdtbl.h wkbddrv.h

$Owkbddrv$o   : wkbddrv.h

$Ovt_linux$o  : fonts.h

$(OBJS) : w.h version.h


clean:
	-$(RM) *$o   out out2 *.tmp w.inf



world : check-args
	$(MAKE) -C $(WKLIBS)/wklib PROD=1 world
ifdef host_is_os2
	$(MAKE) PROD=1 os2 msdos doc
else
	$(MAKE) PROD=1 linux winnt doc
endif

msdos : check-args
	$(MAKE) SYSTEM=msdos   CPU=i86 MODEL=L
	$(MAKE) SYSTEM=msdos   CPU=i386

os2   : check-args
	$(MAKE) SYSTEM=os2

winnt : check-args
	$(MAKE) TARGET=i386--mingw32

linux:	doc
	$(MAKE)
	ln -sf $(target_dir)/w$x   ../we
	ln -sf $(target_dir)/w$x   ../wx

make-dirs:
	for f in $(target_prefix) $(target_prefix)/common \
		 $(target_prefix)/$(target) $(obj_dir); do \
	   test -d $$f || mkdir $$f; \
	done

ifdef host_is_os2
doc:  $Cw.inf $Cw.ref  $Cw.rfi
else
doc:  $Cw.ref $Cw.rfi
endif

# "update" is a tool to copy only newer files and has some other features
install:
	@update -b README README.Linux $(WKLIBS)/COPYING w.pro w.hlp \
		ws.pro ws.hlp whatsnew.doc $(INSTALL_PATH)/w/
	@update $C w.ref w.rfi $(INSTALL_PATH)/w/
	@update -b $(target_prefix)/i386-wtc-os2/s/w.exe \
		   $(WKLIBS)/lib/wk16wrap.dll \
		   $(DOS4GW_PATH)/dos4gw.exe	\
		   $(DOS4GW_PATH)/pminfo.exe	\
		   $(DOS4GW_PATH)/rminfo.exe	\
		   $(DOS4GW_PATH)/privatxm.exe	\
		   $(DOS4GW_PATH)/../dos4gw.doc \
		   $(INSTALL_PATH)/w/
	update -f $(target_prefix)/i386-wtc-msdos/s/w.exe \
				  $(INSTALL_PATH)/w/wd.exe
	update -f $(target_prefix)/i86-wtc-msdos/l/w.exe \
				 $(INSTALL_PATH)/w/wd16.exe
	update -f $(target_prefix)/i86-ztc-msdos/l/w.exe \
				 $(INSTALL_PATH)/w/wd16s.exe
	update -f $(target_prefix)/i386-gnu-linux/s/w \
				 $(INSTALL_PATH)/w/we
	update -b ../contrib/*	$(INSTALL_PATH)/w/contrib/
	wkdutil -s $(INSTALL_PATH)/  zip -u -r w.zip w


install-linux:
	-test -d $(BINDIR) || mkdir $(BINDIR)
	-mv -f $(BINDIR)/we $(BINDIR)/we.old 2>/dev/null
	cp $(exe_dir)/w $(BINDIR)/we
	strip $(BINDIR)/we
	chmod 755 $(BINDIR)/we
	cd $(BINDIR); ln -sf we wx
	-test -d $(LIBDIR) || mkdir $(LIBDIR)
	-test -d $(LIBDIR)/w-edit || mkdir $(LIBDIR)/w-edit
	for f in w.pro w.hlp ws.pro ws.hlp  ; do \
	  -mv -f $(LIBDIR)/w-edit/$$f $(LIBDIR)/w-edit/$$f.old 2>/dev/null ; \
	  cp $$f $(LIBDIR)/w-edit/$$f; \
	  chmod 644 $(LIBDIR)/w-edit/$$f; \
	done
	cd ../common; for f in w.ref w.rfi ; do \
	  -mv -f $(LIBDIR)/w-edit/$$f $(LIBDIR)/w-edit/$$f.old 2>/dev/null ; \
	  cp $$f $(LIBDIR)/w-edit/$$f; \
	  chmod 644 $(LIBDIR)/w-edit/$$f; \
	done
	-test -d $(MANDIR)/man1 || mkdir -p $(MANDIR)/man1
	cp we.1 $(MANDIR)/man1/we.1


# Make a source distribution
ifdef host_is_os2
dist:
	@echo cannot make a source distribution on an OS/2 host.
else
dist:
	-test -d ../temp || mkdir ../temp
	@cd .. || exit 2; homedir=$$(pwd); \
	version=$$(sed -e '/VERSION/s/.* "\([0-9.]*\)"/\1/' src/version.h); \
	echo making wedit-$$version.tar.gz; \
	rm -rf temp/wedit-$$version; \
	mkdir temp/wedit-$$version || exit 2; \
	mkdir temp/wedit-$$version/w || exit 2; \
	mkdir temp/wedit-$$version/w/src || exit 2; \
	mkdir temp/wedit-$$version/w/contrib || exit 2; \
	mkdir temp/wedit-$$version/wklib || exit 2; \
	mkdir temp/wedit-$$version/wk || exit 2; \
	mkdir temp/wedit-$$version/tools || exit 2; \
	mkdir temp/wedit-$$version/tools/src || exit 2; \
	cd src || exit 2; \
	cvs diff || exit 1; \
	ln $$(awk -F / '/^\//  {print $$2}' CVS/Entries) $$homedir/temp/wedit-$$version/w/src; \
	cd ../contrib || exit 2; \
	cvs diff || exit 1; \
	ln $$(awk -F / '/^\//  {print $$2}' CVS/Entries) $$homedir/temp/wedit-$$version/w/contrib; \
	cd $(WKLIBS)/wklib || exit 2; \
	cvs diff || exit 1; \
	ln $$(awk -F / '/^\//  {print $$2}' CVS/Entries) $$homedir/temp/wedit-$$version/wklib; \
	cd $(WKLIBS)/wk || exit 2; \
	cvs diff || exit 1; \
	ln $$(awk -F / '/^\//  {print $$2}' CVS/Entries) $$homedir/temp/wedit-$$version/wk; \
	cd $(WKTOOLS_DIR) || exit 2; \
	ln $(DEVEL_TOOLS)  $$homedir/temp/wedit-$$version/tools/src; \
	cd $$homedir/src || exit 2; \
	cat README.Linux  >$$homedir/temp/wedit-$$version/README; \
	cat Makefile.dist >$$homedir/temp/wedit-$$version/Makefile; \
	cat $(WKLIBS)/COPYING  >$$homedir/temp/wedit-$$version/COPYING; \
	cd $$homedir/temp || exit 2; \
	tagver=wedit-$$(echo $$version| sed -e 's/\./-/g'); \
	echo Tagging the CVS archives with $$tagver; \
	for i in $$homedir/src \
		 $$homedir/contrib \
		 $(WKLIBS)/wklib \
		 $(WKLIBS)/wk \
		 $(WKTOOLS_DIR) \
	   ; do \
	   echo "$$i ..."; \
	   cd $$i || exit 2; \
	   cvs -Q tag $$tagver || exit 2; \
	done; \
	echo Creating tar file; \
	cd $$homedir/temp || exit 2; \
	tar chzf wedit-$$version.tar.gz wedit-$$version


endif

#---- end of makefile ----#
