# Makefile for wmtheme

# Modify INVOCATIONS before installation to change which window managers
# are supported.
#
# INVOCATIONS=bbtheme etheme sftheme itheme astheme gtktheme xmtheme ortheme gotheme
INVOCATIONS=bbtheme etheme sftheme itheme astheme gtktheme xmtheme

# Fix for compiling as root in Slackware.
# A few more of these and we might as well autoconf the dang thing
GCONF=$(shell PATH=$$PATH:/opt/gnome/bin which gtk-config 2>/dev/null)

CC=cc
CFLAGS=-O2 -Wall
LIBS=

VERSION=$(shell cat VERSION)
DISTDIR=wmtheme-$(VERSION)
TARBALL=wmtheme-$(VERSION).tar.gz
LSM=wmtheme-$(VERSION).lsm

DESTDIR=
prefix=/usr/local

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

INSTALL=install -c
PERL=perl

ALLINVOCATIONS=bbtheme etheme sftheme itheme astheme gtktheme xmtheme \
 ortheme gotheme

SUPPORTFILES=wm_config.pl wm_data.pl wmaker.pl blackbox.pl enlightenment.pl \
   sawfish.pl icewm.pl wm_download.pl wm_update.pl conf.default \
   afterstep.pl 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.1 Makefile \
  stubwm.pl make-wmtheme make-lsm apply-gtk-theme.c wminstallfonts \
  wmtheme.spec.in make-rpm

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

apply-gtk-theme: apply-gtk-theme.c
	@if [ ! $(GCONF) ]; then \
	  echo "NOTE: gtk-config not found -- gtktheme won't be able to activate themes"; \
	else \
	  echo '$(CC) -o apply-gtk-theme apply-gtk-theme.c $(CFLAGS) $(LIBS) `$(GCONF) --libs --cflags`'; \
	  $(CC) -o apply-gtk-theme apply-gtk-theme.c $(CFLAGS) $(LIBS) `$(GCONF) --libs --cflags`; \
	fi

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)
	if [ $(GCONF) ]; then \
	  $(INSTALL) -s -m 0755 apply-gtk-theme $(DESTDIR)$(BINDIR); \
	fi

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-gtk-theme
	rm -f $(DESTDIR)$(MANDIR)/wmtheme.1.gz
# This is removes a symlink created by install-dev
	rm -f $(DESTDIR)$(MANDIR)/wmtheme.1

manpage: wmtheme.1.gz

wmtheme.1.gz: wmtheme.1
	gzip -9 < wmtheme.1 > wmtheme.1.gz

clean:
	rm -f wmtheme.1.gz
	rm -f apply-gtk-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)-i386-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;
	if [ $(GCONF) ]; then \
	  ln -f -s $$PWD/apply-gtk-theme $(BINDIR)/apply-gtk-theme; \
	fi
	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)
	mv wmtheme.tgz wmtheme-$(VERSION)-i386-1.tgz

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
	if [ $(GCONF) ]; then \
	  echo "$(BINDIR)/apply-gtk-theme" >> spec-file-list; \
	fi

