# Copyright (C) 1996,1997 Robert H”hne, see COPYING.RH for details 
# This file is part of RHIDE. 
#
# $Id: makefile.src,v 1.7 1997-09-29 20:47:03+00 rho Exp rho $
#

srcdir=$(RHIDESRC)
VPATH=$(srcdir)
projects=gpr2mak rhide_ rhide
project=rhide
cfg_files=gpr2mak.gpr gpr2mak.mak rhide_.gpr rhide_.mak
update_files=gpr2mak.gpr gpr2mak.mak rhide_.gpr rhide_.mak rhide.gpr rhide.mak


po_files=\
	gpr2mak.cc idecolor.cc idedebug.cc idedefau.cc ideexter.cc \
	ideflags.cc ideforma.cc idegcc.cc idehelp.cc idehints.cc \
	idemain.cc idemak.cc idemenu.cc idemsg.cc ideopt.cc ideprj.cc \
	idestatu.cc idestrm.cc ideutil.cc ideversi.cc openedit.cc \
	ideuser.cc idecomp.cc idespec.cc idesupp.cc idebug.cc idesynta.cc \
	abstorel.cc rhassert.c ideenv.cc fstrcmp.c ideapp.cc idedial.cc \
	pal.cp idercs.cc commands.cc

src_files=$(po_files) makefile common.mak README.IDE RHIDE.BIN RHIDE.SRC \
	COPYING.RH COPYING wrong.c copyrite.c makefile.src \
	gpr2mak.gpr rhide_.gpr rhide.gpr gpr2mak.mak rhide_.mak rhide.mak \
	rhide_ch.log edit_ch.log todo pal.c zipcomm.bin zipcomm.src rhide.env \
	LINUX.TXT rhide.var INSTALL.LINUX INSTALL.MAK VCSA.SH common.var \
	rhide.lsm ideversi.in readme.key rhide_.env rhide.spec \
	rhide.terminfo

ifeq ($(rhide_OS),DJGPP)
ADD_SUBDIRS=diffs
else
ADD_SUBDIRS=
endif

subdirs=libide librhgdb libtvgdb libtvuti include doc libgdb tvision \
	rhgdb debobj nodebobj po tvdemo libset tvpatch tv2_src librhuti \
	$(ADD_SUBDIRS)
my_subdirs=libide librhgdb libtvgdb include doc libgdb rhgdb po libtvuti \
	   debobj nodebobj tvpatch librhuti
po_subdirs=libide librhgdb libtvgdb libtvuti tvision rhgdb tvdemo libset

install_bin_files=gpr2mak rhide
install_doc_files=readme.key COPYING COPYING.RH README.IDE RHIDE.BIN\
	LINUX.TXT VCSA.SH

install_data_files_common=rhide_.env
install_data_files_Linux=rhide.terminfo
install_data_files=$(install_data_files_common) $(install_data_files_$(rhide_OS))

include $(RHIDESRC)/common.mak

$(srcdir)/pal.cp: $(srcdir)/pal.c
	gecho "#include <pal.c>\n#undef S\n#define S(x,y,z,name,comment...) __(#name)\n"\
	  "#undef SS\n #define SS(x,y,z,name,group,comment...)  __(#name) __(#group)\ncpIDEColor" |\
	gcc -E -I$(srcdir) - > $@

gpr2mak: gpr2mak.exe
	ln -sf $< $@

programs=gpr2mak.exe idegc.exe

all:: libgdb/libgdb.h $(RHIDESRC)/tvision/include/tv.h $(programs)

$(programs):: $(RHIDESRC)/tvision/include/tv.h

idegc.exe rhgdb/rhgdb.exe:: libgdb/libgdb.a

# these are the targets, which cannot build with RHIDE

.PHONY: needed

needed: libgdb/libgdb.h libgdb/libgdb.a \
	  $(RHIDESRC)/tvision/include/tv.h

libgdb/libgdb.a libgdb/libgdb.h:
	$(MAKE) -C libgdb $(FLAGS_TO_PASS) $(notdir $@)

rhgdb/rhgdb.exe::
	$(MAKE) -C rhgdb $(FLAGS_TO_PASS) $(notdir $@)

gpr2mak.exe:: gpr2mak.mak
	$(MAKE) -f gpr2mak.mak $(FLAGS_TO_PASS) $@

ide.exe:: rhide_.mak
	$(MAKE) -f rhide_.mak $(FLAGS_TO_PASS) $@

rhide$(exe):: idegc.exe
	cp $< $@

tmp.start:
	rm -rf $(copy_dir)

ifeq ($(rhide_OS),DJGPP)
install_prefix_src=contrib/$(PACKAGE)$(DIR_VERSION)
else
install_prefix_src=$(PACKAGE)$(DIR_VERSION)
endif

copy.files:
	@-rm -f $(list_file)
	$(MAKE) prefix=$(prefix) list_file=$(list_file) \
	  list_prefix=$(install_prefix_src)/ copy_src_files

mft.src:
	$(INSTALL_DIR) $(copy_dir)/manifest
	touch $(copy_dir)/manifest/$(rhides).mft $(copy_dir)/manifest/$(rhides).ver
	echo manifest/$(rhides).mft manifest/$(rhides).ver >> $(list_file)
	cp $(list_file) __mft__
	printf "%s\n" @__mft__ | sort > $(copy_dir)/manifest/$(rhides).mft
	rm -f __mft__

_VERSION=$(subst ",,$(subst \",,$(subst -DIDEVERSION=,,$(LOCAL_OPT))))

ver.src:
	$(INSTALL_DIR) $(copy_dir)/manifest
	echo $(rhides) Version $(VERSION) \(sources\) \
	  > $(copy_dir)/manifest/$(rhides).ver

copyrite.exe: copyrite.c
	gcc -o $@ -s -O $<

copyrite_srcdir=$(srcdir)

mark_dirs=$(my_subdirs)
mark_files=$(src_files)
mark_find_files=rhide.env rhide.var makefile makefile.src *.mak

ifeq ($(rhide_OS),DJGPP)
Markfiles=$(addsuffix /.../*,$(addprefix $(copyrite_srcdir)/,$(mark_dirs))) \
	  $(addprefix $(copyrite_srcdir)/,$(mark_files)) \
	  $(addprefix $(copyrite_srcdir)/.../,$(mark_find_files))
else
Markfiles=$(shell find $(addprefix $(copyrite_srcdir)/,$(mark_dirs)) -not -type d -print) \
	  $(addprefix $(copyrite_srcdir)/,$(mark_files)) \
	  $(shell find $(copyrite_srcdir) -name "$(word 1,$(mark_find_files))" \
	   $(foreach name,$(filter-out $(word 1,$(mark_find_files)),$(mark_find_files)),\
	   -o -name "$(name)") -print)
endif

mark.copyright: copyrite.exe
	./copyrite.exe $(Markfiles)

_languages.src: $(addsuffix .po,$(LANGUAGES))

languages.src languages:
	$(MAKE) -C po languages.src

ifeq ($(rhide_OS),DJGPP)
MANIFEST_SRC=mft.src ver.src
LANGUAGES.SRC=
else
MANIFEST_SRC=
LANGUAGES.SRC=
endif

distrib.src_: $(LANGUAGES.SRC) tmp.start copy.files \
	     tv2_diffs $(MANIFEST_SRC)
	rm -f $(rhides).zip
	$(MAKE) mark.copyright copyrite_srcdir=$(prefix)
ifeq ($(rhide_OS),DJGPP)
	cd $(copy_dir);	zip -9r $(obj_dir)/$(rhides).zip \
	  @$(copy_dir)/manifest/$(rhides).mft; \
	  cd /; cd $(obj_dir)
	cat $(srcdir)/zipcomm.src | zip -z $(rhides).zip
else
	tar -C $(copy_dir) -cvf - `ls $(copy_dir) | sed 's,$(copy_dir)/,,g'` \
	  | gzip -9c > $(obj_dir)/$(PACKAGE)$(DIR_VERSION).tar.gz
endif

init.version:
	sed s,@version@,$(VERSION), $(srcdir)/ideversi.in > \
	  $(srcdir)/ideversi.cc
	sed s,@version@,$(VERSION), $(srcdir)/rhgdb/version.in > \
	  $(srcdir)/rhgdb/version.cc

distrib.src: init.version
	rm -f nodebobj/ideversi.o
	$(MAKE) $(FLAGS_TO_PASS) gpr2mak.exe
	touch $(shell find -name "*.gpr" -print)
	$(MAKE) $(FLAGS_TO_PASS) \
	  prefix=$(copy_dir)/$(install_prefix_src) distrib.src_

doc info dvi:
	$(MAKE) -C doc $@

_install.languages:
	$(MAKE) -C po install prefix=$(prefix) logfile=../languages.log \
	  $(FLAGS_TO_PASS)

_install.doc:
	$(MAKE) -C doc install prefix=$(prefix) logfile=../doc.log \
	  $(FLAGS_TO_PASS)

_install.rhgdb:
	$(MAKE) -C rhgdb install prefix=$(prefix) logfile=../rhgdb.log \
	  $(FLAGS_TO_PASS)

ifeq ($(rhide_OS),DJGPP)
INSTALLED_RHIDE=rhide.exe
INSTALLED_GPR2MAK=gpr2mak.exe
else
INSTALLED_RHIDE=rhide
INSTALLED_GPR2MAK=gpr2mak
endif


install.programs: all install.rhgdb
	$(INSTALL_DIR) $(prefix)/$(install_bindir)
	$(INSTALL_PROGRAM) idegc.exe $(prefix)/$(install_bindir)/$(INSTALLED_RHIDE)
	$(INSTALL_PROGRAM) gpr2mak.exe $(prefix)/$(install_bindir)/$(INSTALLED_GPR2MAK)
ifeq ($(use_djp),yes)
	$(DJP) $(addprefix $(prefix)/$(install_bindir)/, \
	  $(INSTALLED_RHIDE) $(INSTALLED_GPR2MAK))
endif
	echo $(install_bindir)/$(INSTALLED_RHIDE) \
	  $(install_bindir)/$(INSTALLED_GPR2MAK) >> $(logfile)

install.keyboard:
	$(MAKE) -C libset/keydefs logfile=../../editor.log \
	  install.cfg_files prefix=$(prefix)

install.start:
	rm -f $(logfile)


_install: install.start install.programs install.languages install.doc install.keyboard
	cat languages.log doc.log rhgdb.log editor.log >> $(logfile)
	rm -f languages.log doc.log rhgdb.log editor.log

manifest.bin:
	$(INSTALL_DIR) $(prefix)/manifest
	printf "%s\n" $(sort $(shell cat $(logfile)) manifest/$(rhideb).mft \
	  manifest/$(rhideb).ver) \
	  > $(prefix)/manifest/$(rhideb).mft
	
ver.bin:
	$(INSTALL_DIR) $(prefix)/manifest
	echo $(rhideb) Version $(VERSION) \(binaries\) \
	  > $(prefix)/manifest/$(rhideb).ver

manifest: manifest.bin ver.bin

ifeq ($(rhide_OS),DJGPP)
MANIFEST=manifest
else
MANIFEST=
endif

bin_src_data_files_DJGPP=
bin_src_data_files_Linux=LINUX.TXT INSTALL.MAK
bin_src_data_files=RHIDE.BIN README.IDE COPYING.RH COPYING rhide_ch.log \
		edit_ch.log \
		$(bin_src_data_files_$(rhide_OS))
bin_src_bin_files_DJGPP=
bin_src_bin_files_Linux=INSTALL.LINUX VCSA.SH
bin_src_bin_files=$(bin_src_bin_files_$(rhide_OS))
bin_src_files=$(bin_src_data_files) $(bin_src_bin_files)
bin_src_prefix_DJGPP=$(install_prefix_src)/
bin_src_prefix_Linux=
bin_src_prefix=$(bin_src_prefix_$(rhide_OS))

install.bin_src_files:
	$(INSTALL_DIR) $(prefix)/$(bin_src_prefix)
	$(INSTALL_DATA) $(addprefix $(srcdir)/,$(bin_src_files)) $(prefix)/$(bin_src_prefix)
ifneq ($(bin_src_bin_files),)
	chmod +x $(addprefix $(prefix)/$(bin_src_prefix),$(bin_src_bin_files))
endif
	echo $(addprefix $(bin_src_prefix),$(bin_src_files)) >> $(logfile)

ifeq ($(rhide_OS),DJGPP)
install_prefix_bin=
DISTRIB_BIN_FLAGS=\
	"LDFLAGS=$(LDFLAGS) -s"
endif

ifeq ($(rhide_OS),Linux)
install_prefix_bin=/$(PACKAGE_DIR)$(DIR_VERSION)
DISTRIB_BIN_FLAGS=\
	"LDFLAGS=$(LDFLAGS) -static"
endif

install::
	$(MAKE) $(FLAGS_TO_PASS) subdir_do SUBDIR_TARGET=install

install.all:
	$(MAKE) $(FLAGS_TO_PASS) install logfile=$(logfile)

distrib.bin_: install.all install.bin_src_files $(MANIFEST)

distrib.bin: init.version
	rm -rf $(copy_dir)
# make at first gpr2mak
	$(MAKE) $(FLAGS_TO_PASS) $(DISTRIB_BIN_FLAGS) gpr2mak.exe
# now touch all .gpr files to force to use the actual gpr2mak
	touch $(shell find -name "*.gpr" -print)
	$(MAKE)  $(FLAGS_TO_PASS) prefix=$(copy_dir)$(install_prefix_bin) \
	  locale_prefix=$(copy_dir)$(install_prefix_bin) distrib.bin_ \
	  logfile=rhide.log $(DISTRIB_BIN_FLAGS)
ifeq ($(rhide_OS),DJGPP)
# On DJGPP the ZIP format is default
	rm -f $(rhideb).zip
	cd $(copy_dir);	zip -9r $(obj_dir)/$(rhideb).zip \
	  @$(copy_dir)/manifest/$(rhideb).mft; \
	  cd /; cd $(obj_dir)
	cat $(srcdir)/zipcomm.bin | zip -z $(rhideb).zip
else
	tar -C $(copy_dir) -cvf - `ls $(copy_dir) | sed 's,$(copy_dir)/,,g'` \
	  | gzip -9c > $(obj_dir)/$(PACKAGE)$(DIR_VERSION).bin.`uname -sr | sed 's/ /-/g'`.tar.gz
endif

tv2_diffs_:
	$(MAKE) -C tvpatch diffs TV2_NEW=$(prefix)/tvision \
	  TV2_DIFF=$(prefix)/tvdiffs

tv2_diffs.clean:
	rm -f $(shell cat __) _ __ ___

tv2_diffs.seds:
	find $(prefix)/tvdiffs -not -type d -fprint _
	sed s,/tvdiffs,/tvision,g _ > __
	sed s,$(prefix)/tvdiffs/,,g _ | \
	  awk '{ print "s,tvision/"$$0",tvdiffs/"$$0"," }' > ___
	sed -f ___ $(list_file) > _
	cp _ $(list_file)

tv2_diffs: tv2_diffs_ tv2_diffs.seds tv2_diffs.clean

update_srcdir:
	$(MAKE) SUBDIR_TARGET=update__srcdir update__srcdir $(FLAGS_TO_PASS)


