TOPDIR=./
include $(TOPDIR)Rules.make

SUBDIRS=boot cpiface dev doc devp devw filesel goodies help medialib playay playcda playflac playgmd playtimidity playhvl playit playogg playopl playqoa plugins
ifeq ($(HAVE_MAD),1)
SUBDIRS+=playmp2
endif
ifeq ($(HAVE_LIBGME),1)
SUBDIRS+=playgme
endif
SUBDIRS+=playsid playwav playxm playym stuff
SUBDIRS+=mingw

.PHONY: all clean subdirs $(SUBDIRS)

include boot/Makefile-static
include cpiface/Makefile-static
include devp/Makefile-static
include filesel/Makefile-static
include filesel/cdfs/Makefile-static
include filesel/modland.com/Makefile-static
include stuff/Makefile-static
include dev/Makefile-static
include help/Makefile-static
include medialib/Makefile-static
include mingw/Makefile-static

libocp_start_so=$(LIBOCP_START_OBJECTS)
libocp_so:=$(LIBOCP_OBJECTS)
ifeq ($(STATIC_BUILD),1)
libocp_so+=$(STATIC_OBJECTS)
endif
libocp_end_so=$(LIBOCP_END_OBJECTS)

all: subdirs ocp$(EXE_SUFFIX) ocp.hlp libocp$(LIB_SUFFIX)

install:
	mkdir -p "$(DESTDIR)$(DOCDIR)"
	$(CP) AUTHORS BUGS COPYING CREDITS KEYBOARD_REMAPS "$(DESTDIR)$(DOCDIR)"
	mkdir -p "$(DESTDIR)$(DATADIROCP)/data"
	$(CP) ocp.hlp "$(DESTDIR)$(DATADIROCP)/data"
	mkdir -p "$(DESTDIR)$(DATADIROCP)/etc"
	$(CP) ocp.ini "$(DESTDIR)$(DATADIROCP)/etc"
	mkdir -p "$(DESTDIR)$(LIBDIROCP)"
	$(CP) libocp$(LIB_SUFFIX) "$(DESTDIR)$(LIBDIROCP)"
	mkdir -p "$(DESTDIR)$(LIBDIROCP)/autoload"
	mkdir -p "$(DESTDIR)$(BINDIR)"
	$(CP) ocp$(EXE_SUFFIX) "$(DESTDIR)$(BINDIR)/ocp$(EXE_SUFFIX)"
ifneq ($(WINDOWS),1)
	$(CP) SUID "$(DESTDIR)$(DOCDIR)"
	$(CP) ocp-curses "$(DESTDIR)$(BINDIR)/ocp-curses"
ifeq ($(LINUX),1)
	$(CP) ocp-vcsa "$(DESTDIR)$(BINDIR)/ocp-vcsa"
endif
ifeq ($(HAVE_SDL),1)
	$(CP) ocp-sdl "$(DESTDIR)$(BINDIR)/ocp-sdl"
endif
ifeq ($(HAVE_SDL2),1)
	$(CP) ocp-sdl2 "$(DESTDIR)$(BINDIR)/ocp-sdl2"
endif
ifeq ($(HAVE_X11),1)
	$(CP) ocp-x11 "$(DESTDIR)$(BINDIR)/ocp-x11"
endif
	$(MAKE) -C doc DESTDIR="$(DESTDIR)" install TOPDIR="../$(TOPDIR)"
ifeq (1,$(firstword $(filter 1,$(HAVE_X11) $(HAVE_SDL) $(HAVE_SDL2))))
	mkdir -p "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/16x16/apps/"
	mkdir -p "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/22x22/apps/"
	mkdir -p "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/24x24/apps/"
	mkdir -p "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/32x32/apps/"
	mkdir -p "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/48x48/apps/"
	mkdir -p "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/64x64/apps/"
	mkdir -p "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/128x128/apps/"
	mkdir -p "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/scalable/apps/"
	mkdir -p "$(DESTDIR)$(DATAROOTDIR)/mime/packages"
	$(CP) desktop/opencubicplayer-16x16.png "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/16x16/apps/opencubicplayer.png"
	$(CP) desktop/opencubicplayer-22x22.png "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/22x22/apps/opencubicplayer.png"
	$(CP) desktop/opencubicplayer-24x24.png "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/24x24/apps/opencubicplayer.png"
	$(CP) desktop/opencubicplayer-32x32.png "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/32x32/apps/opencubicplayer.png"
	$(CP) desktop/opencubicplayer-48x48.png "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/48x48/apps/opencubicplayer.png"
	$(CP) desktop/opencubicplayer-64x64.png "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/64x64/apps/opencubicplayer.png"
	$(CP) desktop/opencubicplayer-128x128.png "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/128x128/apps/opencubicplayer.png"
	$(CP) desktop/opencubicplayer.svg "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/scalable/apps/opencubicplayer.svg"
	$(CP) desktop/opencubicplayer.xml "$(DESTDIR)$(DATAROOTDIR)/mime/packages/opencubicplayer.xml"
ifneq ($(UPDATE_MIME_DATABASE),)
	mkdir -p "$(DESTDIR)$(DATAROOTDIR)/applications"
	$(UPDATE_MIME_DATABASE) "$(DESTDIR)$(DATAROOTDIR)/mime"
endif
ifneq ($(DESKTOP_FILE_INSTALL),)
	mkdir -p "$(DESTDIR)$(DATAROOTDIR)/applications"
	$(DESKTOP_FILE_INSTALL) --vendor="cubic.org" --dir="$(DESTDIR)$(DATAROOTDIR)/applications" desktop/opencubicplayer.desktop
endif
ifneq ($(UPDATE_DESKTOP_DATABASE),)
	mkdir -p "$(DESTDIR)$(DATAROOTDIR)/applications"
	$(UPDATE_DESKTOP_DATABASE)
endif
endif
endif
	for i in $(SUBDIRS) ; do \
	       $(MAKE) -C $$i TOPDIR=../$(TOPDIR) install || exit 1 ;\
	done

uninstall:
	for i in $(SUBDIRS) ; do \
	       $(MAKE) -C $$i TOPDIR=../$(TOPDIR) uninstall || exit 1 ;\
	done
	rm -Rf "$(DESTDIR)$(DOCDIR)" "$(DESTDIR)$(DATADIROCP)" "$(DESTDIR)$(LIBDIROCP)" "$(DESTDIR)$(BINDIR)/ocp$(EXE_SUFFIX)" "$(DESTDIR)$(BINDIR)/ocp-curses" "$(DESTDIR)$(BINDIR)/ocp-sdl" "$(DESTDIR)$(BINDIR)/ocp-sdl2" "$(DESTDIR)$(BINDIR)/ocp-vcsa" "$(DESTDIR)$(BINDIR)/ocp-x11"
	rm -f "$(DESTDIR)$(DATAROOTDIR)/applications/opencubicplayer.desktop"
	rm -f "$(DESTDIR)$(DATAROOTDIR)/applications/cubic.org-opencubicplayer.desktop"
	rm -f "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/16x16/apps/opencubicplayer.png"
	rm -f "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/22x22/apps/opencubicplayer.png"
	rm -f "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/24x24/apps/opencubicplayer.png"
	rm -f "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/32x32/apps/opencubicplayer.png"
	rm -f "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/48x48/apps/opencubicplayer.png"
	rm -f "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/64x64/apps/opencubicplayer.png"
	rm -f "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/128x128/apps/opencubicplayer.png"
	rm -f "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/scalable/apps/opencubicplayer.svg"
	rm -f "$(DESTDIR)$(DATAROOTDIR)/mime/packages/opencubicplayer.xml"
ifneq ($(UPDATE_MIME_DATABASE),)
	$(UPDATE_MIME_DATABASE) "$(DESTDIR)$(DATAROOTDIR)/mime" || true
endif
ifneq ($(UPDATE_DESKTOP_DATABASE),)
	$(UPDATE_DESKTOP_DATABASE) || true
endif

clean:
	for i in $(SUBDIRS) ; do \
	       $(MAKE) -C $$i TOPDIR=../$(TOPDIR) clean || exit 1 ;\
	done
	rm -f ocp$(EXE_SUFFIX) libocp$(LIB_SUFFIX)

subdirs: $(SUBDIRS)

$(SUBDIRS):
	$(MAKE) -C $@ TOPDIR=../$(TOPDIR)

OCP_OBJECTS+=boot/kickload.o
ifeq ($(WINDOWS),1)
OCP_OBJECTS+=boot/utf-8.o boot/utf-16.o
endif
ocp$(EXE_SUFFIX): boot
	$(CC) $(LDFLAGS) -o $@ $(OCP_OBJECTS) @DL_LIBS@ @EFENCE_LIBS@ @DUMA_LIBS@
ifeq ($(HAIKU),1)
	rc -o haiku/ocp.rsrc haiku/ocp.rdef
	xres -o $@ haiku/ocp.rsrc
	mimeset $@
endif

ocp.hlp: doc/opencp.dox goodies
	goodies/helpc/ocphhc doc/opencp.dox $@

distclean: clean
	rm -Rf \
		autom4te.cache \
		config.log \
		config.status \
		config.h \
		playsid/sidplayfp-config/config.h \
		Rules.make \
		Makefile \
		goodies/helpc/Makefile \
		goodies/pack/Makefile \
		ocp.ini \
		ocp.hlp \
		desktop/opencubicplayer.desktop \
		doc/ocp.1 \
		doc/opencp.dox \
		doc/texi/faq.texi \
		doc/texi/ocp.texi \
		haiku/ocp.rdef \
		haiku/ocp.rsrc \
		playsid/libsidplayfp-git/src/builders/residfp-builder/residfp/siddefs-fp.h \
		playsid/libsidplayfp-git/src/builders/residfpII-builder/residfpII/siddefs-fp.h \
		mingw/ocp.rc

libocp$(LIB_SUFFIX): subdirs
	$(CXX) $(SHARED_FLAGS) $(LDFLAGS) -o $@ $(libocp_start_so) $(libocp_so) $(libocp_end_so) $(STATIC_LIBS)
