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
ifeq ($(HAVE_MAD),1)
SUBDIRS+=playmp2
endif
SUBDIRS+=playsid playwav playxm playym stuff

.PHONY: all clean subdirs $(SUBDIRS)

include boot/Makefile-static
include cpiface/Makefile-static
include filesel/Makefile-static
include filesel/cdfs/Makefile-static
include stuff/Makefile-static
include dev/Makefile-static
include help/Makefile-static
include medialib/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 ocp.hlp libocp$(LIB_SUFFIX)

install: all
	mkdir -p "$(DESTDIR)$(DOCDIR)"
	$(CP) AUTHORS BUGS COPYING CREDITS KEYBOARD_REMAPS SUID "$(DESTDIR)$(DOCDIR)"
	mkdir -p "$(DESTDIR)$(DATADIR)/ocp$(DIR_SUFFIX)/data"
	$(CP) ocp.hlp "$(DESTDIR)$(DATADIR)/ocp$(DIR_SUFFIX)/data"
	mkdir -p "$(DESTDIR)$(DATADIR)/ocp$(DIR_SUFFIX)/etc"
	$(CP) ocp.ini "$(DESTDIR)$(DATADIR)/ocp$(DIR_SUFFIX)/etc"
	mkdir -p "$(DESTDIR)$(LIBDIR)"
	$(CP) libocp$(LIB_SUFFIX) "$(DESTDIR)$(LIBDIR)"
	mkdir -p "$(DESTDIR)$(LIBDIR)/autoload"
	mkdir -p "$(DESTDIR)$(BINDIR)"
	$(CP) ocp "$(DESTDIR)$(BINDIR)/ocp$(DIR_SUFFIX)"
	if ! test -z $(DIR_SUFFIX); then ln -sf "ocp$(DIR_SUFFIX)" "$(DESTDIR)$(BINDIR)/ocp"; fi
ifeq ($(DIR_SUFFIX),)
	$(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
endif
	$(MAKE) -C doc DESTDIR="$(DESTDIR)" install TOPDIR="../$(TOPDIR)"

ifeq (1,$(firstword $(filter 1,$(HAVE_X11) $(HAVE_SDL) $(HAVE_SDL2))))
	mkdir -p "$(DESTDIR)$(DATAROOTDIR)/applications"
	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/"
	$(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"
endif

ifneq ($(DESKTOP_FILE_INSTALL),)
	mkdir -p "$(DESTDIR)$(DATAROOTDIR)/applications"
	$(DESKTOP_FILE_INSTALL) --vendor="cubic.org" --dir="$(DESTDIR)$(DATAROOTDIR)/applications" desktop/opencubicplayer.desktop
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)$(DATADIR)/ocp$(DIR_SUFFIX)" "$(DESTDIR)$(LIBDIR)" "$(DESTDIR)$(BINDIR)/ocp$(DIR_SUFFIX)" "$(DESTDIR)$(BINDIR)/ocp" "$(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" "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/16x16/apps/opencubicplayer.xpm" "$(DESTDIR)$(DATAROOTDIR)/icons/hicolor/48x48/apps/opencubicplayer.xpm"

clean:
	for i in $(SUBDIRS) ; do \
	       $(MAKE) -C $$i TOPDIR=../$(TOPDIR) clean || exit 1 ;\
	done

subdirs: $(SUBDIRS)

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

ocp: boot/kickload.o
	$(CC) $(LDFLAGS) -o $@ $^ @DL_LIBS@ @EFENCE_LIBS@ @DUMA_LIBS@
ifeq ($(HAIKU),1)
	rc -o ocp.rsrc ocp.rdef
	xres -o $@ ocp.rsrc
	mimeset $@
endif

ocp.hlp: doc/opencp.dox goodies/helpc/ocphhc
	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 \
		desktop/opencubicplayer.desktop \
		doc/ocp.1 \
		doc/opencp.dox \
		doc/texi/faq.texi \
		doc/texi/ocp.texi \
		playsid/libsidplayfp-git/src/builders/resid-builder/resid/siddefs.h \
		playsid/libsidplayfp-git/src/builders/residfp-builder/residfp/siddefs-fp.h

libocp$(LIB_SUFFIX): $(libocp_start_so) $(libocp_so) $(libocp_end_so)
	$(CC) $(SHARED_FLAGS) $(LDFLAGS) -o $@ $^ $(STATIC_LIBS)
