SUBDIRS	= controls
TARGETS	= $(OBJDIR)mixer$(EXEEXT)
OBJDIR	=
PREFIX	= /usr/local
DESTDIR	=
BINDIR	= $(PREFIX)/bin
SBINDIR	= $(PREFIX)/sbin
CC	= cc
CPPFLAGSF= -I../include
CPPFLAGS=
CFLAGSF	= `pkg-config --cflags libDesktop`
CFLAGS	= -W -Wall -g -O2 -fPIE -D_FORTIFY_SOURCE=2 -fstack-protector
LDFLAGSF= `pkg-config --libs libDesktop`  -lm
LDFLAGS	= -pie -Wl,-z,relro -Wl,-z,now
EXEEXT	=
RM	= rm -f
LN	= ln -f
MKDIR	= mkdir -m 0755 -p
INSTALL	= install


all: subdirs $(TARGETS)

subdirs:
	@for i in $(SUBDIRS); do (cd "$$i" && \
		if [ -n "$(OBJDIR)" ]; then \
		([ -d "$(OBJDIR)$$i" ] || $(MKDIR) -- "$(OBJDIR)$$i") && \
		$(MAKE) OBJDIR="$(OBJDIR)$$i/"; \
		else $(MAKE); fi) || exit; done

mixer_OBJS = $(OBJDIR)control.o $(OBJDIR)mixer.o $(OBJDIR)window.o $(OBJDIR)main.o
mixer_CFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(CFLAGSF) $(CFLAGS)
mixer_LDFLAGS = $(LDFLAGSF) $(LDFLAGS)

$(OBJDIR)mixer$(EXEEXT): $(mixer_OBJS)
	$(CC) -o $(OBJDIR)mixer$(EXEEXT) $(mixer_OBJS) $(mixer_LDFLAGS)

$(OBJDIR)control.o: control.c ../include/Mixer/control.h common.h control.h ../config.h
	$(CC) $(mixer_CFLAGS) -o $(OBJDIR)control.o -c control.c

$(OBJDIR)mixer.o: mixer.c common.h mixer.h ../config.h
	$(CC) $(mixer_CFLAGS) -o $(OBJDIR)mixer.o -c mixer.c

$(OBJDIR)window.o: window.c mixer.h window.h
	$(CC) $(mixer_CFLAGS) -o $(OBJDIR)window.o -c window.c

$(OBJDIR)main.o: main.c mixer.h window.h common.h ../config.h
	$(CC) $(mixer_CFLAGS) -o $(OBJDIR)main.o -c main.c

clean:
	@for i in $(SUBDIRS); do (cd "$$i" && \
		if [ -n "$(OBJDIR)" ]; then \
		$(MAKE) OBJDIR="$(OBJDIR)$$i/" clean; \
		else $(MAKE) clean; fi) || exit; done
	$(RM) -- $(mixer_OBJS)

distclean:
	@for i in $(SUBDIRS); do (cd "$$i" && \
		if [ -n "$(OBJDIR)" ]; then \
		$(MAKE) OBJDIR="$(OBJDIR)$$i/" distclean; \
		else $(MAKE) distclean; fi) || exit; done
	$(RM) -- $(mixer_OBJS)
	$(RM) -- $(TARGETS)

install: all
	@for i in $(SUBDIRS); do (cd "$$i" && \
		if [ -n "$(OBJDIR)" ]; then \
		$(MAKE) OBJDIR="$(OBJDIR)$$i/" install; \
		else $(MAKE) install; fi) || exit; done
	$(MKDIR) $(DESTDIR)$(BINDIR)
	$(INSTALL) -m 0755 $(OBJDIR)mixer$(EXEEXT) $(DESTDIR)$(BINDIR)/mixer$(EXEEXT)

uninstall:
	@for i in $(SUBDIRS); do (cd "$$i" && \
		if [ -n "$(OBJDIR)" ]; then \
		$(MAKE) OBJDIR="$(OBJDIR)$$i/" uninstall; \
		else $(MAKE) uninstall; fi) || exit; done
	$(RM) -- $(DESTDIR)$(BINDIR)/mixer$(EXEEXT)

.PHONY: all subdirs clean distclean install uninstall
