# 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)
ifeq ($(USE_X),1)
TFONTS = $(FONTS:$(REALTOPDIR)/etc/%.bdf=$(top_builddir)/etc/Xfonts/%.pcf.gz)
endif

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/dev/sb16/softmpu base/sound

#
# This is defined when the CPU emulator is required. You should
# re-configure, rather than just change this.
#
ifeq ($(X86_EMULATOR),1)
XCPUEMU = base/emu-i386/simx86
ifeq ($(USE_SOFTFLOAT),1)
XCPUEMU += base/emu-i386/simx86/softfloat
endif
endif

ifeq ($(MCONTEXT),1)
LIBMC = base/lib/mcontext
endif

DPMI = dosext/dpmi dosext/dpmi/doslib dosext/dpmi/dnative

_LIBSUBDIRS=base/video base/dev/vga base/core base/kbd_unicode \
	arch/linux/async base/lib/mapping $(LIBMC) \
	base/misc base/lib/misc base/lib/libpcl base/lib/timer \
	base/lib/fslib base/lib/libbsd \
	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)/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

ifeq ($(USE_DL_PLUGINS),0)
include $(CONFFILES)
endif

ifeq ($(SOLIB),1)
$(BINPATH)/bin/$(DOSLIB): $(LIBS_)
ifeq ($(OS),Darwin)
	$(LD) $(ALL_LDFLAGS) -o $@ \
	   -Wl,-all_load $(LIBS_) $(LIBS) \
	   -Wl,-map,$(BINPATH)/bin/dosemu.map \
	   -dynamiclib -install_name $(libdir)/$(DOSLIB) \
	   -compatibility_version $(SOVERSION) -current_version $(SOVERSION)
else
	$(LD) $(ALL_LDFLAGS) -o $@ \
	   -Wl,--whole-archive $(LIBS_) -Wl,--no-whole-archive $(LIBS) \
	   -Wl,-Map=$(BINPATH)/bin/dosemu.map \
	   -shared -Wl,-Bsymbolic -Wl,-soname=$(DOSLIB) \
	   $(SO_LDFLAGS)
endif

DOSLIBLINKNAME=$(subst .$(SOVERSION),,$(DOSLIB))
$(BINPATH)/bin/$(DOSLIBLINKNAME): $(BINPATH)/bin/$(DOSLIB)
	$(LN_S) -f $(DOSLIB) $@

$(BINPATH)/bin/$(DOSBIN): base/core/main.o $(BINPATH)/bin/$(DOSLIBLINKNAME)
	$(LD) $(DOSBIN_LDFLAGS) $(LDFLAGS) -o $@ $< -L $(BINPATH)/bin -ldosemu2
else
$(BINPATH)/bin/$(DOSBIN): base/core/main.o $(LIBS_)
	$(LD) $(DOSBIN_LDFLAGS) $(LDFLAGS) -o $@ $< \
	   -Wl,--whole-archive $(LIBS_) -Wl,--no-whole-archive $(LIBS) \
	   -Wl,-Map=$(BINPATH)/bin/dosemu.map
endif

$(BINPATH)/bin/dosemu: $(SRCPATH)/dosemu.systemwide $(SRCPATH)/dosemu \
  $(top_builddir)/Makefile.conf
	cp $< $@
	echo >> $@
	echo "LOCAL_BUILD_PATH=$(abs_top_builddir)" >> $@
	echo "LOCAL_BIN_DIR=$(THISVERSION)/bin" >> $@
	echo "prefix=$(prefix)" >> $@
	echo "libexecdir=$(libexecdir)" >> $@
	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) .WAIT $(top_builddir)/commands
	@($(MAKE) -C $@)

doslib: dosemu

installnew: doslib
	$(MAKE) install

install:
ifneq ($(USERADD),)
	$(USERADD) --system $(username) --shell /sbin/nologin \
	  --home-dir /nonexistent || true
endif
	$(INSTALL) -d $(DESTDIR)$(dosemudir)
	$(RM) -r $(DESTDIR)$(dosemudir)/commands
	$(RM) -r $(DESTDIR)$(dosemudir)/fdboot
	$(RM) -r $(DESTDIR)$(dosemudir)/freedos
	$(RM) -r $(cmddir)
	$(RM) -r $(sysdir)
	$(INSTALL) -d $(DESTDIR)$(etcdir) || true

	$(INSTALL) -d $(sysdir)
	for i in `find $(SRCPATH)/bindist -maxdepth 1 -type f`; do \
	  $(INSTALL) -m 0644 $$i $(sysdir); \
	done
	$(INSTALL) -d $(sysdir)/c
	$(INSTALL) -d $(cmddir)
	$(INSTALL) -d $(cmddir)/dosrc.d
	$(INSTALL) -m 0644 -D $(SRCPATH)/bindist/c/* $(sysdir)/c
	$(INSTALL) -m 0644 -D $(SRCPATH)/bindist/bat/*.bat $(cmddir)
	$(INSTALL) -m 0644 -D $(SRCPATH)/bindist/bat/dosrc.d/* $(cmddir)/dosrc.d
	cp -f -d $(SYS)/* $(cmddir)
	$(LN_S) -f $(cmdsuff) $(DESTDIR)$(dosemudir)/commands
	if [ -d $(DESTDIR)$(etcdir) -a ! -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)$(libexecdir)/dosemu2
	$(INSTALL) -m 0755 $(top_builddir)/bin/$(DOSBIN) $(DESTDIR)$(libexecdir)/dosemu2
	$(INSTALL) -d $(DESTDIR)$(libdir)
ifeq ($(SOLIB),1)
	$(INSTALL) -m 0755 $(top_builddir)/bin/$(DOSLIB) $(DESTDIR)$(libdir)
	$(LN_S) -f $(DOSLIB) $(DESTDIR)$(libdir)/$(DOSLIBLINKNAME)
endif
	$(INSTALL) -d $(DESTDIR)$(includedir)/$(PACKAGE_NAME)
	$(INSTALL) -m 0644 -D $(SRCPATH)/include/$(PACKAGE_NAME)/* $(DESTDIR)$(includedir)/$(PACKAGE_NAME)
	$(INSTALL) -d $(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.md THANKS BUGS; do \
	    $(INSTALL) -m 0644 $(REALTOPDIR)/$$i $(DESTDIR)$(docdir); \
	done
	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..."
ifeq ($(USE_X),1)
	$(RM) -r $(DESTDIR)$(x11fontdir)
	$(INSTALL) -d $(DESTDIR)$(x11fontdir)
	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) $(DESTDIR)$(sysconfdir)/X11/fontpath.d/$(PACKAGE_NAME):unscaled:pri=20; \
	    $(LN_S) -f $(x11fontdir) $(DESTDIR)$(sysconfdir)/X11/fontpath.d/$(PACKAGE_NAME):unscaled:pri=20; \
	fi
endif
	$(RM) -r $(DESTDIR)$(ttffontdir)
	$(INSTALL) -d $(DESTDIR)$(ttffontdir)
	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) -r $(DESTDIR)$(dosemuxdir)/freedos; \
	  tar -C $(DESTDIR)$(dosemuxdir) --strip-components=1 --no-same-owner -xpzvf $(fdtarball); \
	  $(RM) $(DESTDIR)$(dosemuxdir)/FDchange.log; \
	  $(RM) -r $(DESTDIR)$(dosemuxdir)/freedos/tmp; \
	fi
	if [ -d $(DESTDIR)$(dosemuxdir)/freedos ]; then \
	  $(RM) -r $(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 may need to run 'sudo ldconfig' before running dosemu2."

uninstall:
	$(RM) -r $(DESTDIR)$(dosemudir)
	$(RM) -r $(DESTDIR)$(libexecdir)/$(PACKAGE_NAME)
	$(RM) -r $(DESTDIR)$(includedir)/$(PACKAGE_NAME)
ifeq ($(SOLIB),1)
	$(RM) $(DESTDIR)$(libdir)/$(DOSLIBLINKNAME)
	$(RM) $(DESTDIR)$(libdir)/$(DOSLIB)
endif
	$(RM) $(DESTDIR)$(bindir)/dosemu
	$(RM) $(DESTDIR)$(bindir)/mkfatimage16
	$(RM) $(DESTDIR)$(bindir)/mkhdimage
	$(RM) $(DESTDIR)$(bindir)/dosdebug
	$(RM) -r $(DESTDIR)$(plugindir)
	$(RM) -r $(DESTDIR)$(docdir)
	ls $(DOCS)/*.1 | xargs --max-args=1 basename | xargs -I {} --max-args=1 $(RM) $(DESTDIR)$(mandir)/man1/{}
	ls $(DOCS)/ru/*.1 | xargs --max-args=1 basename | xargs -I {} --max-args=1 $(RM) $(DESTDIR)$(mandir)/ru/man1/{}

local_clean:
	$(RM) -r $(top_builddir)/bin $(top_builddir)/commands $(top_builddir)/lib

clean:: local_clean

realclean:: local_clean
	$(RM) -r $(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) -r $(top_builddir)/lib
	-$(RM) -r $(BINPATH) $(top_builddir)/bin $(top_builddir)/commands $(top_builddir)/etc/Xfonts
