# Makefile for wmtheme

# This makefile does not, by default, install symlinks for all possible
# invocations.  INVOCATIONS is the list of symlinks that will be created.
#
# Here is a list of all possible invocations:
# bbtheme etheme sftheme itheme gtktheme xmtheme ortheme gotheme

INVOCATIONS=bbtheme etheme sftheme itheme gtktheme xmtheme

CC=cc
CFLAGS=-O2 -Wall
LIBS=

PROGRAM=wmtheme

VERSION=$(shell cat VERSION)
DISTDIR=$(PROGRAM)-$(VERSION)
TARBALL=$(PROGRAM)-$(VERSION).tar.gz
LSM=$(PROGRAM)-$(VERSION).lsm
PODFILE=$(PROGRAM).pod
MANPOD2HTML=$(shell which manpod2html 2>/dev/null)
POD2HTML=$(shell which pod2html 2>/dev/null)

DESTDIR=
prefix=/usr/local

BINDIR=$(prefix)/bin
LIBDIR=$(prefix)/lib/$(PROGRAM)
MANDIR=$(prefix)/man/man1
DOCDIR=$(prefix)/doc/$(PROGRAM)-$(VERSION)

INSTALL=install -c
PERL=perl

ALLINVOCATIONS=bbtheme etheme sftheme itheme gtktheme xmtheme \
 ortheme gotheme

SUPPORTFILES=wm_config.pl wm_data.pl wmaker.pl blackbox.pl enlightenment.pl \
   sawfish.pl icewm.pl conf.default gtk.pl xmms.pl wm_fonts.pl \
   oroborus.pl golem.pl

DOCFILES=CHANGES INSTALL README TODO COPYING CODING CREDITS README.fonts

DISTFILES=wmtheme $(SUPPORTFILES) $(DOCFILES) VERSION wmtheme.pod wmtheme.1 \
  Makefile stubwm.pl make-wmtheme apply-gtk2-theme.c wminstallfonts \
  wmtheme.spec.in make-rpm lsm.in

all: apply-gtk2-theme manpage
	PREFIX=$(prefix) $(PERL) ./make-wmtheme

apply-gtk2-theme: apply-gtk2-theme.c
	$(CC) -o apply-gtk2-theme apply-gtk2-theme.c $(CFLAGS) $(LIBS) `pkg-config --libs --cflags gtk+-2.0`; \

install: all install-dirs
	$(INSTALL) -m 755 wmtheme $(DESTDIR)$(BINDIR)
	for SUPT in $(SUPPORTFILES) ; do \
	  $(INSTALL) -m 644 $$SUPT $(DESTDIR)$(LIBDIR); \
	done;
	$(INSTALL) -m 644 wmtheme.1.gz $(DESTDIR)$(MANDIR)
	for INVOCATION in $(INVOCATIONS); do \
	  if [ ! -e $(DESTDIR)$(BINDIR)/$$INVOCATION ] ; then \
	    ln -s wmtheme $(DESTDIR)$(BINDIR)/$$INVOCATION; \
	  fi; \
	  if [ ! -e $(DESTDIR)$(MANDIR)/$$INVOCATION.1.gz ] ; then \
	    ln -s wmtheme.1.gz $(DESTDIR)$(MANDIR)/$$INVOCATION.1.gz; \
	  fi; \
	done;
	$(INSTALL) -m 755 wminstallfonts $(DESTDIR)$(BINDIR)
	$(INSTALL) -s -m 0755 apply-gtk2-theme $(DESTDIR)$(BINDIR); \

install-doc:
	$(INSTALL) -d $(DESTDIR)$(DOCDIR)
	for doc in $(DOCFILES); do \
	  $(INSTALL) -m 644 $$doc $(DESTDIR)$(DOCDIR); \
	done;

uninstall:
	rm -f $(DESTDIR)$(BINDIR)/wmtheme
	rm -fr $(DESTDIR)$(LIBDIR)
	for INVOCATION in $(INVOCATIONS); do \
	  if [ -h $(DESTDIR)$(BINDIR)/$$INVOCATION ]; \
	    then rm $(DESTDIR)$(BINDIR)/$$INVOCATION; \
	  fi; \
	  if [ -h $(DESTDIR)$(MANDIR)/$$INVOCATION.1.gz ]; \
	    then rm $(DESTDIR)$(MANDIR)/$$INVOCATION.1.gz; \
	  fi; \
	done;
	rm -f $(DESTDIR)$(BINDIR)/wminstallfonts
	rm -f $(DESTDIR)$(BINDIR)/apply-gtk2-theme
	rm -f $(DESTDIR)$(MANDIR)/wmtheme.1.gz
# This is removes a symlink created by install-dev
	rm -f $(DESTDIR)$(MANDIR)/wmtheme.1

manpage: $(PROGRAM).1.gz $(PROGRAM)-man.html

$(PROGRAM).1.gz: $(PROGRAM).1
	gzip -9 < $(PROGRAM).1 > $(PROGRAM).1.gz

$(PROGRAM).1: $(PODFILE)
	pod2man --section=1 --center="User Manuals" -r "" $(PODFILE) > $(PROGRAM).1

$(PROGRAM)-man.html: $(PODFILE)
	if [ $(MANPOD2HTML) ]; then \
	   $(MANPOD2HTML) -H "wmtheme,1,User Manuals" $(PODFILE) > $(PROGRAM)-man.html; \
	elif [ $(POD2HTML) ]; then \
	   $(POD2HTML) --noindex $(PODFILE) > $(PROGRAM)-man.html; \
	fi


clean:
	rm -f wmtheme.1.gz
	rm -f wmtheme.1
	rm -f wmtheme-man.html
	rm -f apply-gtk2-theme
	rm -f $(LSM)
	rm -f $(TARBALL)
	rm -fr $(DISTDIR)
	rm -f spec-file-list
	rm -f *.rpm
	rm -f wmtheme.tgz
	rm -f wmtheme-$(VERSION)-i?86-1.tgz

# The INSTALL-DEV target
#
# First, move all files to the desired development directory,
# such as ~/projects/wmtheme.  From that directory, invoke
# "make install-dev".
#
# It will create symlinks from the normal install locations
# to the files in the current directory.
# "make uninstall" will undo both install and install-dev.

install-dev: uninstall all install-dirs
	ln -s $$PWD/wmtheme $(BINDIR)/wmtheme
	ln -s $$PWD/wmtheme.1 $(MANDIR)/wmtheme.1
	for INVOCATION in $(ALLINVOCATIONS); do \
	  ln -f -s $$PWD/wmtheme $(BINDIR)/$$INVOCATION; \
	done;
	for SUPT in $(SUPPORTFILES) ; do \
	  ln -s $$PWD/$$SUPT $(LIBDIR)/$$SUPT; \
	done;
	ln -f -s $$PWD/apply-gtk2-theme $(BINDIR)/apply-gtk2-theme;
	ln -f -s $$PWD/wminstallfonts $(BINDIR)/wminstallfonts

install-dirs:
	$(INSTALL) -d $(DESTDIR)$(BINDIR)
	$(INSTALL) -d $(DESTDIR)$(LIBDIR)
	$(INSTALL) -d $(DESTDIR)$(MANDIR)

dist: $(TARBALL)

lsm: $(LSM)

$(LSM):
	make-lsm

$(TARBALL): $(DISTFILES) $(LSM)
	rm -fr $(DISTDIR)
	mkdir $(DISTDIR)
	cp $(DISTFILES) $(LSM) $(DISTDIR)
	for INVOCATION in $(ALLINVOCATIONS); do \
	  ln -s wmtheme $(DISTDIR)/$$INVOCATION; \
	done;
	tar -c $(DISTDIR) | gzip > $(TARBALL)
	make-lsm
	cp $(LSM) $(DISTDIR)
	tar -c $(DISTDIR) | gzip > $(TARBALL)

tgz: $(TARBALL)
	autopkg $(TARBALL)

rpm: $(TARBALL)
	@if [ `id -u` != 0 ]; then \
	  echo You must be root to make the RPM.; \
	  exit 1; \
	fi
	./make-rpm

speclist:
	rm -f spec-file-list
	echo "%doc $(DOCFILES) $(LSM)" > spec-file-list
	echo $(LIBDIR) >> spec-file-list
	for invocation in $(INVOCATIONS); do \
	  echo "$(BINDIR)/$$invocation" >> spec-file-list; \
	  echo "$(MANDIR)/$$invocation.1.gz" >> spec-file-list; \
	done
	echo "$(MANDIR)/wmtheme.1.gz" >> spec-file-list
	echo "$(BINDIR)/wmtheme" >> spec-file-list
	echo "$(BINDIR)/wminstallfonts" >> spec-file-list
	echo "$(BINDIR)/apply-gtk2-theme" >> spec-file-list;

