PLUGIN_FILE := dumb.so
PACKAGE := audacious-dumb

AUDMINVER := 3.3

CFLAGS ?= -O2 -pipe
CFLAGS += -Wall -fPIC -DPIC -I/usr/local/include

INSTALL_DIR := $(shell pkg-config audacious --variable=input_plugin_dir)
INSTALL_DIR_HOME := ~/.local/share/audacious/Plugins/Input

AUDPREFIX := $(shell pkg-config audacious --variable=exec_prefix)
AUDBIN := $(AUDPREFIX)/bin/audacious

GTKCFLAGS :=  $(shell pkg-config gtk+-3.0 --cflags)

AUDACIOUSCFLAGS := $(shell pkg-config 'audacious >= $(AUDMINVER)' --cflags || echo "old")

ifeq ($(AUDACIOUSCFLAGS),old)
version:
	@echo "Your Audacious version is too old. Version $(AUDMINVER) or later is required."
	@false
else
CFLAGS += $(AUDACIOUSCFLAGS) $(GTKCFLAGS) -DPACKAGE=\"$(PACKAGE)\"
endif

DUMBLIBS := -ldumb -L/usr/local/lib
AUDACIOUSLIBS := $(shell pkg-config audacious --libs) 
GTKLIBS += $(shell pkg-config gtk+-3.0 --libs)

SOURCES := audacious-dumb.c
OBJDIR := obj
OBJECTS := $(SOURCES:%.c=$(OBJDIR)/%.o)

all:	$(PLUGIN_FILE)
	@echo
	@echo "The plug-in has been compiled. Run one of the following:"
	@echo "make install      - for global (root) installation to $(INSTALL_DIR)"
	@echo "make install-home - for installation to home directory $(INSTALL_DIR_HOME)"

$(PLUGIN_FILE): $(OBJECTS)
	$(CC) -shared $(LDFLAGS) -o $@ $^ $(DUMBLIBS) $(AUDACIOUSLIBS) $(GTKLIBS)

$(OBJDIR)/%.o: %.c
	$(CC) -o $@ $(CFLAGS) -c $<

install: all
	install -d $(DESTDIR)$(INSTALL_DIR)
	install -m 0755 $(PLUGIN_FILE) $(DESTDIR)$(INSTALL_DIR)
	@echo
	@echo "The plug-in has been installed."
	@echo "It is recommended you disable the Modplug plug-in"
	@echo "and any others that support the IT, XM, S3M or MOD formats."
	@echo "Enjoy!"

install-home: all
	install -d $(INSTALL_DIR_HOME)
	install -m 0755 $(PLUGIN_FILE) $(INSTALL_DIR_HOME)
	@echo
	@echo "The plug-in has been installed."
	@echo "It is recommended you disable the Modplug plug-in"
	@echo "and any others that support the IT, XM, S3M or MOD formats."
	@echo "Enjoy!"

uninstall:
	rm -f $(DESTDIR)$(INSTALL_DIR)/$(PLUGIN_FILE)
	@echo
	@echo "The plug-in has been uninstalled."

uninstall-home:
	rm -f $(INSTALL_DIR_HOME)/$(PLUGIN_FILE)
	@echo
	@echo "The plug-in has been uninstalled."

clean:
	rm -f $(OBJECTS)

distclean: clean
	rm -f $(PLUGIN_FILE) *~

DEPCC := echo "int main() { return 0; };" | $(CC) -x c -o /dev/null -
depcheck:
	@echo -n "dumb.h ... "
	@$(DEPCC) -include dumb.h && echo "found." || exit 1
	@echo -n "libdumb.so ... "
	@$(DEPCC) $(DUMBLIBS) -lm && echo "found." || exit 1
	@echo -n "Audacious ... "
	@pkg-config audacious --exists || { echo -n "pkg-config audacious not found: " && exit 1; }
	@pkg-config audacious --atleast-version=$(AUDMINVER) || { echo -n "version $(AUDMINVER) or later not found." && exit 1; }
	@$(DEPCC) $(CFLAGS) $(AUDACIOUSLIBS) -include audacious/plugin.h && echo "`pkg-config audacious --modversion` found." || exit 1
	@echo "Everything seems to be ok."

