# If you're building on Windows, set this to 1.
WINDOWS ?= 0

# This is the location where the plugin will be installed. Modify this if you
# want to install the plugin system-wide.
ifeq ($(WINDOWS),0)

  # *nix Plugin Directory Settings

  PLUGINDIR=$(HOME)/.gaim/plugins

  # Uncomment this to install the plugin for all users.
  #PLUGINDIR=$(shell pkg-config --variable=libdir gaim)/gaim

else

  # Windows Plugin Directory Settings

  PLUGINDIR="$(HOME)/Application Data/.gaim/plugins"

  # Sample system-wide plugin directory:
  #PLUGINDIR="/cygdrive/c/Program Files/Gaim/plugins"

endif

# This only matters on Windows.
GAIM_SOURCE_DIR=../gaim
GTK_TOP=../win32-dev/gtk_2_0

# DO NOT EDIT BELOW THIS LINE

PACKAGE=irchelper
VERSION=0.12

PACKDIR=/tmp/gaim-$(PACKAGE)-$(VERSION)

ifeq ($(WINDOWS),0)
	TARBALL=$(shell pwd)/../gaim-$(PACKAGE)-$(VERSION).tar.bz2
	ARCHIVER=tar cjf
	ARCHIVER_END=--exclude=.cvsignore
else
	TARBALL=$(shell pwd)/../gaim-$(PACKAGE)-$(VERSION).zip
	ARCHIVER=zip -r -9
	ARCHIVER_END=-x gaim-$(PACKAGE)-$(VERSION)/.cvsignore
endif

# Common Compiler Stuff
CC = gcc
DEFINES=-DPACKAGE=\"$(PACKAGE)\" -DVERSION=\"$(VERSION)\"
CFLAGS ?= -g3 -O2 -Wall
override CFLAGS += $(DEFINES)

ifeq ($(WINDOWS),0)

  # *nix Compiler Stuff
  GAIM_CFLAGS=$(shell pkg-config --cflags gaim)
  GAIM_LDFLAGS=$(shell pkg-config --libs gaim)
  override CFLAGS += $(GAIM_CFLAGS) -fPIC
  override LDFLAGS += $(GAIM_LDFLAGS) -fPIC
  SHARED_OBJECT_SUFFIX=.so

else

  # Windows Compiler Stuff

  WIN32_CFLAGS= -I"$(TOPDIR)$(GTK_TOP)/include/glib-2.0" \
                -I"$(TOPDIR)$(GTK_TOP)/lib/glib-2.0/include" \
                -I"$(TOPDIR)$(GAIM_SOURCE_DIR)/src" \
		-mno-cygwin -mms-bitfields \
                $(CFLAGS)

  WIN32_LIBS=   -lgtk-win32-2.0 \
                -lglib-2.0 \
                -lgdk-win32-2.0 \
                -lgobject-2.0 \
                -lgmodule-2.0 \
                -lgdk_pixbuf-2.0 \
                -lpango-1.0 \
                -lintl \
                -lws2_32 \
                -lgaim

  SHARED_OBJECT_SUFFIX=.dll

endif

all: build
build: $(PACKAGE)$(SHARED_OBJECT_SUFFIX)	# Builds all components of the package
rebuild: clean build	# Builds all components of the package from scratch
install: build		# Installs the plugin in $(PLUGINDIR)
	mkdir -p $(PLUGINDIR)
	rm -f $(PLUGINDIR)/$(PACKAGE)$(SHARED_OBJECT_SUFFIX)
	cp $(PACKAGE)$(SHARED_OBJECT_SUFFIX) $(PLUGINDIR)
package:		# Builds the distribution package from this
	test ! -f "$(TARBALL)" || rm -f "$(TARBALL)"
	test ! -d "$(PACKDIR)" || rm -rf "$(PACKDIR)"
	cp -rL . "$(PACKDIR)"
	chmod u+w "$(PACKDIR)"
	cd "$(PACKDIR)" && make distclean
	-test $(WINDOWS) -ne 0 && cd "$(PACKDIR)" && make TOPDIR="$(shell cygpath.exe --windows `pwd`)/" && rm -f $(PACKAGE).dll.o
	cd "$(PACKDIR)/.." && $(ARCHIVER) "$(TARBALL)" "$(shell basename $(PACKDIR))" $(ARCHIVER_END)
	rm -rf "$(PACKDIR)"
	-gpg --armor --detach-sign "$(TARBALL)"
	rm -f ~/src/RPM/RPMS/i386/gaim-$(PACKAGE)-$(VERSION)-0.i386.rpm
	-test $(WINDOWS) -eq 0 && rpmbuild -ta "$(TARBALL)"
	-test $(WINDOWS) -eq 0 && rpm --resign ~/src/RPM/RPMS/i386/gaim-$(PACKAGE)-$(VERSION)-0.i386.rpm
clean:			# Removes all generated files
	rm -f *$(SHARED_OBJECT_SUFFIX) *.o
distclean: clean	# Preparse the directory for distribution
	rm -rf CVS *~ .#* *.bak *.old *.orig *.rej
help:			# Displays usage information
	@sed -ne 's/^\([a-z]*\):.*# /\1\t- /p' Makefile

$(PACKAGE).so: $(PACKAGE).o
	$(LINK.o) --shared $^ $(LOADLIBES) $(LDLIBS) -o $@

$(PACKAGE).dll: $(PACKAGE).c
	$(CC) $(WIN32_CFLAGS) -o $@.o -c $<
	$(CC) -shared $@.o -L"$(TOPDIR)$(GTK_TOP)/lib" -L"$(TOPDIR)$(GAIM_SOURCE_DIR)/src" $(WIN32_LIBS) $(DLL_LD_FLAGS) -o $@

.PHONY: all build rebuild package install clean distclean help
