CC = valac
TARGET = icewm-background
SOURCES = icewm-background.vala
DESKTOP_FILE = icewm-background.desktop
SCRIPT_FILE = icewmbg2

# Vala translates the source code into C code first
VALAFLAGS = -C --pkg gtk+-3.0 --pkg gdk-pixbuf-2.0 --pkg gio-2.0

# Manual include directories for the extracted devx system headers
INCLUDES = -I/usr/include/gtk-3.0 \
           -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include \
           -I/usr/include/pango-1.0 \
           -I/usr/include/harfbuzz \
           -I/usr/include/cairo \
           -I/usr/include/gdk-pixbuf-2.0 \
           -I/usr/include/atk-1.0 \
           -I/usr/include/atk-1.0/atk \
           -I/usr/include/pixman-1 \
           -I/usr/include/freetype2 \
           -I/usr/include/libpng16

LIBS = -lgtk-3 -lgdk-3 -lgobject-2.0 -lglib-2.0 -lgdk_pixbuf-2.0 -lgio-2.0 -lpango-1.0 -lcairo -lharfbuzz -latk-1.0

PREFIX = /usr/local

all: $(TARGET)

$(TARGET): $(SOURCES)
	# Step 1: Translate Vala source into C code
	$(CC) $(VALAFLAGS) $(SOURCES)
	# Step 2: Compile C code with manual paths using GCC
	gcc $(SOURCES:.vala=.c) $(INCLUDES) $(LIBS) -o $(TARGET)

install: $(TARGET)
	# Step 3: Copy the compiled binary into the global system path
	install -D -m 755 $(TARGET) $(DESTDIR)$(PREFIX)/bin/$(TARGET)
	
	# Step 4: Check and copy the icewmbg2 script if it does not exist
	if [ ! -f $(DESTDIR)/usr/bin/$(SCRIPT_FILE) ]; then \
		if [ -f $(SCRIPT_FILE) ]; then \
			install -D -m 755 $(SCRIPT_FILE) $(DESTDIR)/usr/bin/$(SCRIPT_FILE); \
			echo "Installed $(SCRIPT_FILE) to /usr/bin/"; \
		else \
			echo "Warning: Local file $(SCRIPT_FILE) not found in this folder. Skipping script install."; \
		fi \
	else \
		echo "$(SCRIPT_FILE) is already present in /usr/bin/. Skipping copy."; \
	fi

	# Step 5: Copy the desktop entry shortcut into the system menu folder
	install -D -m 644 $(DESKTOP_FILE) $(DESTDIR)/usr/share/applications/$(DESKTOP_FILE)
	
	# Step 6: Update the IceWM application menu cache
	-fixmenus

clean:
	rm -f $(TARGET) $(SOURCES:.vala=.c)

.PHONY: all clean install
