INKSCAPE=$(shell \
   if [ -x /usr/bin/inkscape ]; then                                    \
      echo /usr/bin/inkscape;                                           \
   elif [ -x /opt/gnome/bin/inkscape ]; then                            \
      echo /opt/gnome/bin/inkscape;                                     \
   elif [ -x /bin/flatpak ]                                             \
        && flatpak list | grep org.inkscape.Inkscape > /dev/null; then  \
      echo "setpriv --inh-caps -all flatpak run org.inkscape.Inkscape"; \
   elif [ -x /usr/local/bin/Inkscape-*.AppImage ]; then                 \
      echo /usr/local/bin/Inkscape-*.AppImage;                          \
   fi)

ifeq ($(INKSCAPE),)
inkscape_error:
	@echo "Unable to locate inkscape" >&2
	@false
endif

INKSCAPEVER=$(shell $(INKSCAPE) --version | sed -e '/^Inkscape /!d' -e 's/^Inkscape \([0-9.]*\) .*$$/\1/')

INKSCAPEOPTS=$(shell if [[ "$(INKSCAPEVER)" == 0* ]]; then \
                        echo -en "-e\n"; \
                     else \
                        echo "--export-type=png -o"; \
                     fi)

SVG:=$(wildcard *.svg)
PNG:=$(SVG:.svg=.png) $(SVG:.svg=.icon.png)

default: $(PNG)

details vars:
	@echo "================================================"
	@echo "INKSCAPE     = $(INKSCAPE)"
	@echo "INKSCAPEVER  = $(INKSCAPEVER)"
	@echo "INKSCAPEOPTS = $(INKSCAPEOPTS)"
	@echo "================================================"

%.icon.png: %.svg
	@echo "Generating $@"
	@DISPLAY= $(INKSCAPE) -w 48 $(INKSCAPEOPTS) $@ $< > /dev/null 2>&1

%.png: %.svg
	@echo "Generating $@"
	@DISPLAY= $(INKSCAPE) -w 96 $(INKSCAPEOPTS) $@ $< > /dev/null 2>&1

clean:
	rm -f $(PNG)
