# Copyright (C) 1996-2003 Robert H”hne, see COPYING.RH for details 
# This file is part of RHIDE. 
#

ifeq ($(strip $(RHIDESRC)),)
export RHIDESRC:=$(shell pwd)
endif

srcdir=$(RHIDESRC)
VPATH=$(srcdir)
projects=gpr2mak rhide_ rhide gprexp rh tolf tocrlf copyrite rh_comm
project=
cfg_files=
txt_conv_DJGPP=tocrlf.exe
txt_conv_Linux=tolf.exe

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 openedit.cc \
	ideuser.cc idecomp.cc idespec.cc idesupp.cc idebug.cc idesynta.cc \
	rhassert.c ideenv.cc fstrcmp.c ideapp.cc idedial.cc \
	idercs.cc commands.cc idecheck.cc gprexp.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 todo rhide.env \
	LINUX.TXT rhide.var VCSA.SH common.var configure \
	ideversi.in ideversi.cc readme.key rhide_.env iconv.c \
	moveifch gprexp.gpr gprexp.mak rh.gpr rh.mak conio.o \
	tolf.cc tolf.gpr tolf.mak tocrlf.gpr tocrlf.mak \
	copyrite.gpr copyrite.mak rhidex rhgdbx eterm-rhide \
	rhide.spec aclocal.m4 config.guess config.in config.sub \
	configure.in configure.sub install.sh rh_comm.gpr rh_comm.mak

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

additional_bin_files=
ifeq ($(rhide_OS),Linux)
additional_bin_files=rhidex rhgdbx
endif
install_bin_files=gpr2mak$(exe) gprexp$(exe) rhide$(exe) $(additional_bin_files)
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=eterm-rhide
install_data_files=$(install_data_files_common) $(install_data_files_$(rhide_OS))

main_executables=gpr2mak.exe gprexp.exe idegc.exe rhgdb/rhgdb.exe

include $(RHIDESRC)/common.mak

programs=gpr2mak.exe idegc.exe rhgdb/rhgdb.exe gprexp.exe

all:: libgdb/libgdbrh.h init.version $(programs)

# these are the targets, which cannot build with RHIDE

.PHONY: needed touch_gprs

needed: libgdb/libgdbrh.h libgdb/libgdbrh.a init.version

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

rhgdb/rhgdb.exe:: rhgdb/version.cc
	$(MAKE) -C rhgdb $(FLAGS_TO_PASS)

gprexp.exe:: gprexp.mak force
	$(MAKE) -f gprexp.mak $(FLAGS_TO_PASS)

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

ide.exe:: rhide_.mak force
	$(MAKE) -f rhide_.mak $(FLAGS_TO_PASS)
	
idegc.exe rhgdb/rhgdb.exe:: libgdb/libgdbrh.a

idegc.exe ide.exe gpr2mak.exe gprexp.exe:: ideversi.cc

idegc.exe:: rhide.mak force
	$(MAKE) -f rhide.mak $(FLAGS_TO_PASS)

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

rhide: rhide.exe
	$(LN_SF) $< $@

tolf.exe:: tolf.mak force
	$(MAKE) -f tolf.mak $(FLAGS_TO_PASS)

tocrlf.exe:: tocrlf.mak force
	$(MAKE) -f tocrlf.mak $(FLAGS_TO_PASS)

clean::
	rm -f rhide

gpr2mak: gpr2mak.exe
	$(LN_SF) $< $@

clean::
	rm -f gpr2mak

gprexp: gprexp.exe
	$(LN_SF) $< $@

clean::
	rm -f gprexp

force::

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

touch_gprs:
	@touch $(shell find -name "*.gpr" -print)

check.files.internal: init.version gpr2mak.exe gprexp.exe touch_gprs
	@echo Checking in $(obj_dir)
	@$(MAKE) --no-print-directory check_src_files \
	  update_src_file="cp -p" \
	  update_gpr_file=$(top_obj_dir)/gprexp.exe

check.files:
	@$(MAKE) check.files.internal LFN=y FNCASE=y

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

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_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_: $(notdir $(txt_conv)) $(LANGUAGES.SRC) tmp.start copy.files \
	     $(MANIFEST_SRC)
	rm -f $(rhides).zip
	$(MAKE) mark.copyright copyrite_srcdir=$(prefix)
ifneq ($(txt_conv),)
	find $(copy_dir) -not -type d -a -not -name "*.gpr" \
	  -a -not -name "*.o" -exec $(txt_conv) {} \;
endif
ifeq ($(rhide_OS),DJGPP)
	cd $(copy_dir);	zip -9r $(obj_dir)/$(rhides).zip \
	  @$(copy_dir)/manifest/$(rhides).mft; \
	  cd /; cd $(obj_dir)
else
	tar -C $(copy_dir) -cvf - `ls $(copy_dir) | sed 's,$(copy_dir)/,,g'` \
	  | gzip -9c > $(obj_dir)/$(PACKAGE)$(DIR_VERSION).tar.gz
endif
	rm -rf $(copy_dir)

init.version: ideversi.cc rhgdb/version.cc

ideversi.cc: ideversi.in common.mak
	@sed -e s,@version@,$(VERSION),g \
	    -e "s/WARN/This file is generated automatically, do not edit!/g" \
          $(srcdir)/ideversi.in > _vers_
	@$(move-if-change) _vers_ $@
	@-rm -f _vers_

clean::
	rm -f ideversi.cc

rhgdb/version.cc: rhgdb/version.in common.mak
	@sed -e s,@version@,$(VERSION),g \
	    -e "s/WARN/This file is generated automatically, do not edit!/g" \
          $(srcdir)/rhgdb/version.in > _vers_
	@$(move-if-change) _vers_ $@
	-rm -f _vers_

distrib.src: check_perms init.version touch_gprs
	$(MAKE) $(FLAGS_TO_PASS) gpr2mak.exe
	$(MAKE) $(FLAGS_TO_PASS) \
	  prefix=$(copy_dir)/$(install_prefix_src) \
	  LFN=y FNCASE=y txt_conv="$(top_obj_dir)/$(txt_conv_$(rhide_OS))" \
	  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=../$(logfile) \
	  $(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_upx),yes)
	$(UPX) $(addprefix $(prefix)/$(install_bindir)/, \
	  $(INSTALLED_RHIDE) $(INSTALLED_GPR2MAK))
endif
	echo $(install_bindir)/$(INSTALLED_RHIDE) \
	  $(install_bindir)/$(INSTALLED_GPR2MAK) >> $(logfile)

install.start:
	rm -f $(logfile)


_install: install.start install.programs install.languages install.doc
	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
bin_src_data_files=RHIDE.BIN README.IDE COPYING.RH COPYING rhide_ch.log \
		$(bin_src_data_files_$(rhide_OS))
bin_src_bin_files_DJGPP=
bin_src_bin_files_Linux=VCSA.SH configure
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)
ifneq ($(txt_conv),)
	$(txt_conv) $(addprefix $(prefix)/$(bin_src_prefix),$(bin_src_files))
endif

ifneq ($(link_type),dynamic)
DISTRIB_BIN_FLAGS=\
	"LDFLAGS=$(LDFLAGS) -s -static" \
	LFN=Y \
	FNCASE=Y
else
DISTRIB_BIN_FLAGS=\
	"LDFLAGS=$(LDFLAGS) -s -dynamic" \
	LFN=Y \
	FNCASE=Y
endif

ifeq ($(rhide_OS),DJGPP)
install_prefix_bin=
else
install_prefix_bin=/$(PACKAGE_DIR)$(DIR_VERSION)
endif

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

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

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

distrib.dynamic:
	$(MAKE) link_type=dynamic archive_id=.dynamic distrib.bin 

distrib.build.bin: distrib.src
	rm -rf $(build_dir)
	mkdir -p $(build_dir)
	-unzip -d $(build_dir) $(rhides).zip
	cd $(build_dir)/$(install_prefix_src); $(build_dir)/$(install_prefix_src)/configure \
	  --with-dummy-iconv
	make -C $(build_dir)/$(install_prefix_src) \
	  top_obj_dir=$(build_dir)/$(install_prefix_src) \
	  distrib.bin
	cp $(build_dir)/$(install_prefix_src)/$(rhideb).zip $(top_obj_dir)
	
distrib.bin: check_perms init.version touch_gprs
	rm -rf $(copy_dir)
	rm -f $(main_executables)
# make at first gpr2mak
	$(MAKE) $(FLAGS_TO_PASS) $(DISTRIB_BIN_FLAGS) gpr2mak.exe
	$(MAKE)  $(FLAGS_TO_PASS) prefix=$(copy_dir)$(install_prefix_bin) \
	  logfile=rhide.log $(DISTRIB_BIN_FLAGS) \
	  txt_conv="$(top_obj_dir)/$(txt_conv_$(rhide_OS))" \
	  distrib.bin_ 
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)
else
	tar -C $(copy_dir) -cvf - `ls $(copy_dir) | sed 's,$(copy_dir)/,,g'` \
	  | gzip -9c > $(obj_dir)/$(PACKAGE)$(DIR_VERSION).bin$(archive_id).`uname -s | sed 's/ /-/g'`.tar.gz
endif

RAW_TV_SRC=$(sort $(subst .o,.cc,$(shell ar t $(TVOBJ)/librhtv.a)))
TV_SRC=\
	$(wildcard $(addprefix $(REAL_TVSRC)/classes/,$(RAW_TV_SRC)))\
	$(wildcard $(addprefix $(REAL_TVSRC)/linux/,$(RAW_TV_SRC)))\
	$(wildcard $(addprefix $(REAL_TVSRC)/djgpp/,$(RAW_TV_SRC)))\
	$(wildcard $(addprefix $(REAL_TVSRC)/classes/linux/,$(RAW_TV_SRC)))\
	$(wildcard $(addprefix $(REAL_TVSRC)/classes/djgpp/,$(RAW_TV_SRC)))

RAW_SET_SRC_BASE=$(subst .o,.cc,$(shell ar t $(SETOBJ)/libset.a))
ifneq ($(wildcard $(SETOBJ)/easydiag.a),)
RAW_SET_SRC_EASY=$(subst .o,.cc,$(shell ar t $(SETOBJ)/easydiag.a))
endif
RAW_SET_SRC=$(sort $(RAW_SET_SRC_BASE) $(RAW_SET_SRC_EASY))
SET_SRC=\
	$(wildcard $(addprefix $(REAL_SETSRC)/calcu/,$(RAW_SET_SRC)))\
	$(wildcard $(addprefix $(REAL_SETSRC)/doc/,$(RAW_SET_SRC)))\
	$(wildcard $(addprefix $(REAL_SETSRC)/easydiag/,$(RAW_SET_SRC)))\
	$(wildcard $(addprefix $(REAL_SETSRC)/infview/,$(RAW_SET_SRC)))\
	$(wildcard $(addprefix $(REAL_SETSRC)/mainsrc/,$(RAW_SET_SRC)))\
	$(wildcard $(addprefix $(REAL_SETSRC)/setedit/,$(RAW_SET_SRC)))\
	$(wildcard $(addprefix $(REAL_SETSRC)/settvuti/,$(RAW_SET_SRC)))

additional_po_files=$(TV_SRC) $(SET_SRC)

_additional_po_files:=$(wildcard $(REAL_TVSRC)/*.cc)\
	$(wildcard $(REAL_TVSRC)/classes/*.cc)\
	$(wildcard $(REAL_TVSRC)/linux/*.cc)\
	$(wildcard $(REAL_TVSRC)/djgpp/*.cc)\
	$(wildcard $(REAL_TVSRC)/classes/linux/*.cc)\
	$(wildcard $(REAL_TVSRC)/classes/djgpp/*.cc)\
	\
	$(wildcard $(REAL_SETSRC)/calcu/*.cc)\
	$(wildcard $(REAL_SETSRC)/doc/*.c)\
	$(wildcard $(REAL_SETSRC)/easydiag/*.cc)\
	$(wildcard $(REAL_SETSRC)/infview/*.cc)\
	$(wildcard $(REAL_SETSRC)/mainsrc/*.cc)\
	$(wildcard $(REAL_SETSRC)/setedit/*.cc)\
	$(wildcard $(REAL_SETSRC)/settvuti/*.cc)

check_perms:
ifneq ($(rhide_OS),DJGPP)
	-find $(srcdir) -not -type d -exec chmod 644 {} \;
	-find $(srcdir) -type d -o -name "*.exe" -exec chmod 755 {} \;
	-chmod 755 $(srcdir)/configure $(srcdir)/rhidex $(srcdir)/rhgdbx \
	$(srcdir)/config.guess $(srcdir)/config.sub \
	$(srcdir)/configure.sub $(srcdir)/install.sh
endif

distrib.deb: distrib.src
ifneq ($(rhide_OS),DJGPP)
	rm -rf $(copy_dir)
	mkdir -p $(copy_dir)
	tar -C $(copy_dir) -xzvf $(obj_dir)/$(PACKAGE)$(DIR_VERSION).tar.gz
	cd $(copy_dir)/$(PACKAGE)$(DIR_VERSION); echo s | deb-make
	cd $(copy_dir)/$(PACKAGE)$(DIR_VERSION); fakeroot debian/rules binary
endif

distrib.rpm: distrib.src
ifneq ($(rhide_OS),DJGPP)
	ln -sf $(obj_dir)/$(PACKAGE)$(DIR_VERSION).tar.gz /usr/src/packages/SOURCES/$(PACKAGE)$(DIR_VERSION).tar.gz
	rpm -ba --clean $(srcdir)/rhide.spec
	rm -f /usr/src/packages/SOURCES/$(PACKAGE)$(DIR_VERSION).tar.gz
endif

