################################################################################
# GTK-NoCSD                                                                    #
# Copyright (C) 2025 MorsMortium                                               #
#                                                                              #
# This program is free software: you can redistribute it and/or modify         #
# it under the terms of the GNU General Public License as published by         #
# the Free Software Foundation, either version 3 of the License, or            #
# (at your option) any later version.                                          #
#                                                                              #
# This program is distributed in the hope that it will be useful,              #
# but WITHOUT ANY WARRANTY; without even the implied warranty of               #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
# GNU General Public License for more details.                                 #
#                                                                              #
# You should have received a copy of the GNU General Public License            #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.        #
################################################################################

###########################
#      * ARGUMENTS *      #
###########################

# Paths
PREFIX ?= /usr/local
LICENSEDIR ?= $(PREFIX)/share/licenses/gtk-nocsd
LIBDIR ?= $(PREFIX)/lib64
OPTDIR ?= /opt/gtk-nocsd
DOCDIR ?= $(PREFIX)/share/doc/gtk-nocsd

# Compilation
CC ?= cc
CFLAGS += `pkg-config --cflags libadwaita-1 gobject-2.0 gio-2.0` -Wall -Wextra \
	-Wconversion -Wstrict-prototypes
ifndef NOLDFLAGS
	LDFLAGS += `pkg-config --libs gobject-2.0 gio-2.0`
else
	LDFLAGS =
endif

###########################
#       * TARGETS *       #
###########################
build: libgtk-nocsd.so.0

libgtk-nocsd.so.0: ./Source/GTK-NoCSD.c
	$(CC) -fPIC -shared ./Source/GTK-NoCSD.c -o libgtk-nocsd.so.0 \
		-Wl,-soname,libgtk-nocsd.so.0 \
		-Wl,-e,GTKNoCSDMain $(CFLAGS) $(LDFLAGS)

clean:
	rm -f ./libgtk-nocsd.so.0

install: build
	@for dir in "$(LICENSEDIR)" "$(LIBDIR)"; do\
		mkdir -p "$(DESTDIR)$$dir";\
	done
	install -m 644 LICENSE $(DESTDIR)$(LICENSEDIR)/LICENSE
	install -Dm 755 libgtk-nocsd.so.0 $(DESTDIR)$(LIBDIR)/libgtk-nocsd.so.0
	ln -s libgtk-nocsd.so.0 $(DESTDIR)$(LIBDIR)/libgtk-nocsd.so

ifndef NOOPT
	mkdir -p "$(DESTDIR)$(OPTDIR)"
	ln $(DESTDIR)$(LIBDIR)/libgtk-nocsd.so.0 \
		$(DESTDIR)$(OPTDIR)/libgtk-nocsd.so
endif

ifndef NODOC
	mkdir -p "$(DESTDIR)$(DOCDIR)/examples/profile.d"
	install -m 644 Source/gtk-nocsd.sh \
		$(DESTDIR)$(DOCDIR)/examples/profile.d/gtk-nocsd.sh
	install -m 644 Source/gtk-nocsd.csh \
		$(DESTDIR)$(DOCDIR)/examples/profile.d/gtk-nocsd.csh
endif

uninstall:
	-rm -r $(DESTDIR)$(LICENSEDIR)
	-rm -r $(DESTDIR)$(OPTDIR)
	-rm -r $(DESTDIR)$(DOCDIR)
	-rm $(DESTDIR)$(LIBDIR)/libgtk-nocsd.so.0
	-rm $(DESTDIR)$(LIBDIR)/libgtk-nocsd.so

format:
	uncrustify -c ./Uncrustify.cfg --replace ./Source/*.c

.PHONY: build uninstall format clean install
