# Makefile for Linux DOSEMU
#
# You should do a "make" to compile and a "make install" as root to
# install DOSEMU.
SYS = $(top_builddir)/$(THISVERSION)/commands
dosemudir = $(datadir)/dosemu
dosemuxdir = $(datadir)/dosemu2-extras
sysdir = $(DESTDIR)$(dosemudir)/$(cmdsuff)
cmddir = $(sysdir)/dosemu
etcdir = $(sysconfdir)/$(confdir)
FONTS = $(wildcard $(REALTOPDIR)/etc/*.bdf)
TFONTS = $(FONTS:$(REALTOPDIR)/etc/%.bdf=$(top_builddir)/etc/Xfonts/%.pcf.gz)

NET=dosext/net

#
# This is defined when the SB Emulation is required. You should re-configure,
# rather than just change this.
#
SBEMU=base/dev/sb16 base/sound

#
# This is defined when the CPU emulator is required. You should
# re-configure, rather than just change this.
#
ifdef X86_EMULATOR
XCPUEMU=base/emu-i386/simx86
endif

# No longer need midid
#REQUIRED += $(MIDID)

DPMI = dosext/dpmi dosext/dpmi/msdos

_LIBSUBDIRS=base/video base/dev/vga base/core base/kbd_unicode \
	arch/linux/async arch/linux/mapping base/lib/mcontext \
	base/misc base/lib/misc base/lib/libpcl base/dev/misc \
	base/emu-i386 $(XCPUEMU) base/speaker \
	base/dev/pic base/dev/ne2k \
	dosext/mfs dosext/misc dosext/builtins \
	base/init base/serial base/mouse \
	base/dev/dma base/lib/translate \
	$(NET) $(IPX) $(SBEMU) dosext/drivers base/bios base/bios/x86 \
	$(DPMI)

LIBSUBDIRS=$(_LIBSUBDIRS) $(PLUGINSUBDIRS)
ST_LIBSUBDIRS=$(_LIBSUBDIRS) $(ST_PLUGINSUBDIRS)

# call all libraries the name of the directory
LIBS_ := ${addsuffix .a,${addprefix $(top_builddir)/lib/lib,$(subst /,_,$(ST_LIBSUBDIRS))}}

DOCS= $(top_builddir)/man

###################################################################

default: $(top_builddir)/src/include/version.hh doslib \
	$(TFONTS)
	@echo ""
	@echo "---------------------------------DONE compiling-------------------------------"
	@echo ""
	@echo " Now you must install DOSEMU2 with the following command:"
	@echo " sudo make install"
	@echo ""
	@echo ""

$(top_builddir)/etc/Xfonts:
	mkdir $@

$(top_builddir)/etc/Xfonts/%.pcf.gz: $(REALTOPDIR)/etc/%.bdf | $(top_builddir)/etc/Xfonts
	bdftopcf $< | gzip -c -9 -n > $@

$(LIBS_): | $(top_builddir)/lib

$(BINPATH)/bin/$(DOSBIN): $(LIBS_)
	$(LD) $(ALL_LDFLAGS) $(DOSBIN_LDFLAGS) -o $@ \
	   -Wl,--whole-archive $(LIBS_) -Wl,--no-whole-archive $(LIBS) \
	   -Wl,-Map=$(BINPATH)/bin/dosemu.map

$(BINPATH)/bin/dosemu: $(SRCPATH)/dosemu.systemwide $(SRCPATH)/dosemu
	cp $< $@
	echo >> $@
	echo "LOCAL_BUILD_PATH=$(abs_top_builddir)" >> $@
	echo "LOCAL_BIN_DIR=$(THISVERSION)/bin" >> $@
	echo "prefix=$(prefix)" >> $@
	echo >> $@
	cat $(SRCPATH)/dosemu >> $@
	chmod +x $@

bin: $(top_builddir)/bin $(BINPATH)/bin/$(DOSBIN) $(BINPATH)/bin/dosemu

# This recursive call seems to be necessary to deal with parallel makes.
# Otherwise the rule for $(DOSBIN) would depend on a phony target, and
# this does not seem to be supported by GNU Make.
dosemu: $(LIBSUBDIRS)
	@$(MAKE) bin

DIRLIST = $(LIBSUBDIRS) $(OPTIONALSUBDIRS)
.PHONY:	dossubdirs optionalsubdirs dosemu bin
.PHONY: $(DIRLIST)

optionalsubdirs:	$(OPTIONALSUBDIRS)

$(DIRLIST): $(GENERATED) $(top_builddir)/commands
	@($(MAKE) SUBDIR:=$@ -C $@)

doslib: dosemu

installnew: doslib
	$(MAKE) install

install:
	$(INSTALL) -d $(DESTDIR)$(dosemudir)
	rm -rf $(DESTDIR)$(dosemudir)/commands
	rm -rf $(DESTDIR)$(dosemudir)/fdboot
	rm -rf $(DESTDIR)$(dosemudir)/freedos
	rm -rf $(cmddir)
	rm -rf $(sysdir)
	$(INSTALL) -d $(DESTDIR)$(etcdir)

	$(INSTALL) -d $(sysdir)
	for i in `find $(SRCPATH)/bindist -maxdepth 1 -type f`; do \
	  $(INSTALL) -m 0644 $$i $(sysdir); \
	done
	$(INSTALL) -m 0644 -D $(SRCPATH)/bindist/c/* -t $(sysdir)/c
	$(INSTALL) -m 0644 -D $(SRCPATH)/bindist/bat/*.bat -t $(sysdir)/dosemu
	$(INSTALL) -m 0644 -D $(SRCPATH)/bindist/bat/dosrc.d/* -t $(sysdir)/dosemu/dosrc.d
	$(INSTALL) -d $(cmddir)
	for i in `find -L $(SYS) -xtype f`; do \
	  $(INSTALL) -m 0644 $$i $(cmddir); \
	done
	cd $(SYS); for i in `find * -type l`; do \
	  ln -sf generic.com $(cmddir)/$$i; \
	done
	ln -sf $(cmdsuff) $(DESTDIR)$(dosemudir)/commands
	if [ ! -f $(DESTDIR)$(etcdir)/dosemu.conf ]; then \
	  $(INSTALL) -m 0644 $(REALTOPDIR)/etc/dosemu.conf $(DESTDIR)$(etcdir); \
	fi
	$(INSTALL) -d $(DESTDIR)$(dosemudir)/keymap
	$(INSTALL) -m 0644 $(REALTOPDIR)/etc/keymap/* $(DESTDIR)$(dosemudir)/keymap
	$(INSTALL) -d $(DESTDIR)$(dosemudir)/cpi
	$(INSTALL) -m 0644 $(REALTOPDIR)/etc/cpi/* $(DESTDIR)$(dosemudir)/cpi
	$(INSTALL) -d $(DESTDIR)$(dosemudir)/icons
	$(INSTALL) -m 0644 $(REALTOPDIR)/etc/*.xpm $(DESTDIR)$(dosemudir)/icons
	$(INSTALL) -d $(DESTDIR)$(datadir)/applications
	$(INSTALL) -m 0644 $(top_builddir)/etc/*.desktop $(DESTDIR)$(datadir)/applications
	$(INSTALL) -m 0644 $(REALTOPDIR)/etc/locales.conf $(DESTDIR)$(dosemudir)
	$(INSTALL) -d $(DESTDIR)$(bindir)
	$(INSTALL) -m 0755 $(top_builddir)/bin/$(DOSBIN) $(DESTDIR)$(bindir)
	$(INSTALL) -m 0755 $(top_builddir)/bin/dosemu $(DESTDIR)$(bindir)
	[ ! -f $(top_builddir)/bin/mkfatimage16 ] || $(INSTALL) -m 0755 $(top_builddir)/bin/mkfatimage16 $(DESTDIR)$(bindir)
	[ ! -f $(top_builddir)/bin/dosdebug ] || $(INSTALL) -m 0755 $(top_builddir)/bin/dosdebug $(DESTDIR)$(bindir)
	$(INSTALL) -d $(DESTDIR)$(plugindir)
	for i in $(top_builddir)/bin/*.so; do \
	  if [ -f $$i ]; then \
	    $(INSTALL) -m 0755 $$i $(DESTDIR)$(plugindir); \
	  fi; \
	done
	$(INSTALL) -d $(DESTDIR)$(docdir)
	for i in NEWS THANKS BUGS; do \
	    $(INSTALL) -m 0644 $(REALTOPDIR)/$$i $(DESTDIR)$(docdir); \
	done
	$(INSTALL) -m 0644 $(top_builddir)/changelog $(DESTDIR)$(docdir)
	for i in README EMUfailure tweaks; do \
	  $(INSTALL) -m 0644 $(REALTOPDIR)/doc/$$i.html $(DESTDIR)$(docdir); \
	done
	$(INSTALL) -m 0644 $(REALTOPDIR)/doc/NOVELL-HOWTO.txt $(DESTDIR)$(docdir)
	$(INSTALL) -m 0644 $(REALTOPDIR)/doc/README.gdb $(DESTDIR)$(docdir)
	echo "-> Installing fonts..."
	rm -rf $(DESTDIR)$(ttffontdir)
	rm -rf $(DESTDIR)$(x11fontdir)
	$(INSTALL) -d $(DESTDIR)$(x11fontdir)
	$(INSTALL) -d $(DESTDIR)$(ttffontdir)
	install -m 0644 -D $(top_builddir)/etc/Xfonts/* $(DESTDIR)$(x11fontdir)
	$(INSTALL) -m 0644 $(REALTOPDIR)/etc/$(PACKAGE_NAME).alias $(DESTDIR)$(x11fontdir)/fonts.alias
	! which mkfontdir >/dev/null || mkfontdir $(DESTDIR)$(x11fontdir)
	if [ -d "$(DESTDIR)$(sysconfdir)/X11/fontpath.d" ]; then \
	    rm -f $(DESTDIR)$(sysconfdir)/X11/fontpath.d/$(PACKAGE_NAME):unscaled:pri=20; \
	    ln -sf $(x11fontdir) $(DESTDIR)$(sysconfdir)/X11/fontpath.d/$(PACKAGE_NAME):unscaled:pri=20; \
	fi

	install -m 0644 -D $(REALTOPDIR)/etc/ttf/* $(DESTDIR)$(ttffontdir)

	echo "-> Installing man pages..."
	$(MAKE) -C $(top_builddir)/man install

	if [ -n "$(fdtarball)" -a -f "$(fdtarball)" ]; then \
	  echo "-> Installing FreeDOS..."; \
	  $(INSTALL) -d $(DESTDIR)$(dosemuxdir); \
	  rm -rf $(DESTDIR)$(dosemuxdir)/freedos; \
	  tar -C $(DESTDIR)$(dosemuxdir) --strip-components=1 --no-same-owner -xpzvf $(fdtarball); \
	  rm -f $(DESTDIR)$(dosemuxdir)/FDchange.log; \
	  rm -rf $(DESTDIR)$(dosemuxdir)/freedos/tmp; \
	fi
	if [ -d $(DESTDIR)$(dosemuxdir)/freedos ]; then \
	  rm -rf $(DESTDIR)$(dosemuxdir)/fdboot; \
	  $(INSTALL) -d $(DESTDIR)$(dosemuxdir)/fdboot; \
	  cd $(DESTDIR)$(dosemuxdir)/fdboot; \
	  ln -s ../../dosemu/commands/c/fdconfig.sys fdconfig.sys; \
	  ln -s ../freedos/kernel.sys kernel.sys; \
	  ln -s ../freedos/command.com command.com; \
	fi

	@if test $(etcdir) != /etc; then \
	  if [ -f $(DESTDIR)/etc/dosemu.users ]; then \
	    echo ; \
	    echo /etc/dosemu.users exists but you did not set etcdir=/etc; \
	    echo Deprecated: moving to /etc/dosemu/dosemu.users; \
	    echo mv /etc/dosemu.users /etc/dosemu/dosemu.users; \
	    mv /etc/dosemu.users /etc/dosemu/dosemu.users; \
	  fi; \
	  if [ -f $(DESTDIR)/etc/dosemu.conf ]; then \
	    echo ; \
	    echo /etc/dosemu.conf exists but you did not set etcdir=/etc; \
	    echo Deprecated: moving to /etc/dosemu/dosemu.conf; \
	    echo mv /etc/dosemu.conf /etc/dosemu/dosemu.conf; \
	    mv /etc/dosemu.conf /etc/dosemu/dosemu.conf; \
	  fi \
	fi
	@echo ""; \
	 echo "---------------------------------DONE Installing-------------------------------"; \
	 echo ""
	@echo "  - You can type 'dosemu' to run DOSEMU2."

uninstall:
	rm -rf $(DESTDIR)$(dosemudir)
	rm -f $(DESTDIR)$(bindir)/$(DOSBIN)
	rm -f $(DESTDIR)$(bindir)/dosemu
	rm -f $(DESTDIR)$(bindir)/mkfatimage16
	rm -f $(DESTDIR)$(bindir)/mkhdimage
	rm -f $(DESTDIR)$(bindir)/dosdebug
	rm -rf $(DESTDIR)$(plugindir)
	rm -rf $(DESTDIR)$(docdir)
	ls $(DOCS)/*.1 | xargs --max-args=1 basename | xargs -I {} --max-args=1 rm -f $(DESTDIR)$(mandir)/man1/{}
	ls $(DOCS)/ru/*.1 | xargs --max-args=1 basename | xargs -I {} --max-args=1 rm -f $(DESTDIR)$(mandir)/ru/man1/{}

local_clean:
	rm -rf $(top_builddir)/bin $(top_builddir)/commands $(top_builddir)/lib

clean:: local_clean

realclean:: local_clean
	rm -rf $(BINPATH)

CLEANDIRS=$(addsuffix .clean, $(DIRLIST))
REALCLEANDIRS=$(addsuffix .realclean, $(DIRLIST))

clean:: $(CLEANDIRS)

realclean:: $(REALCLEANDIRS)

.PHONY: $(CLEANDIRS)
$(CLEANDIRS):
	-@$(MAKE) -C $(subst .clean,,$@) clean CLEANING=true

.PHONY: $(REALCLEANDIRS)
$(REALCLEANDIRS):
	-@$(MAKE) -C $(subst .realclean,,$@) realclean CLEANING=true

pristine:	realclean
	-rm -rf $(top_builddir)/lib
	-rm -rf $(BINPATH) $(top_builddir)/bin $(top_builddir)/commands $(top_builddir)/etc/Xfonts