# dwm - dynamic window manager
# See LICENSE file for copyright and license details.
.PHONY: all clean install uninstall

include config.mk

SRC = drw.c dwm.c util.c
OBJ = ${SRC:.c=.o}

all: dwm

*.c: buildroot
*.h: buildroot
.c.o:
	${CC} -c ${CFLAGS} $<

dwm.o: buildroot config.h
config.mk:
	cp config/config.mk config.mk
config.h:
	cp config/config.h config.h

${OBJ}: config.h config.mk

dwm: ${OBJ} buildroot
	${CC} -o $@ ${OBJ} ${LDFLAGS}

clean:
	find . -maxdepth 1 -type f  | grep -Pv "^\./\.|Makefile$$" | xargs -r rm
	rm -r tmp patch 2>/dev/null || true
	git -C dwm-flexipatch reset --hard HEAD
	git -C dwm-flexipatch clean -fdx

buildroot:
	cp config/patches.h dwm-flexipatch/patches.h
	flexipatch-finalizer/flexipatch-finalizer.sh -r -d dwm-flexipatch -o tmp
	rm -r tmp/Makefile tmp/config.mk patch 2>/dev/null || true
	mv tmp/* ./
	for patch in config/patches/*.diff; do patch <"$$patch"; done

install: all
	mkdir -p ${DESTDIR}${PREFIX}/bin
	cp -f dwm ${DESTDIR}${PREFIX}/bin
	chmod 755 ${DESTDIR}${PREFIX}/bin/dwm
	mkdir -p ${DESTDIR}${MANPREFIX}/man1
	sed "s/VERSION/${VERSION}/g" < dwm.1 > ${DESTDIR}${MANPREFIX}/man1/dwm.1
	chmod 644 ${DESTDIR}${MANPREFIX}/man1/dwm.1
	mkdir -p ${DESTDIR}${PREFIX}/share/xsessions
	test -f ${DESTDIR}${PREFIX}/share/xsessions/dwm.desktop || cp -n dwm.desktop ${DESTDIR}${PREFIX}/share/xsessions
	chmod 644 ${DESTDIR}${PREFIX}/share/xsessions/dwm.desktop

uninstall:
	rm -f ${DESTDIR}${PREFIX}/bin/dwm\
		${DESTDIR}${MANPREFIX}/man1/dwm.1\
		${DESTDIR}${PREFIX}/share/xsessions/dwm.desktop