##
## G-Todo GKrellm Plugin Makefile
## Dimitar Haralanov (voidtrance at comcast dot net)
##
##############################################

## User specific valiables
## You should change these to match your
## environment
GLOBAL_INSTALL_PATH ?= /usr/lib/gkrellm2/plugins
USER_INSTALL_PATH ?= $(HOME)/.gkrellm2/plugins

## executables
CC = gcc
RM = rm -f
INSTALL ?= install -m 755

## Compiler and Linker flags
## Theoretically, you should not need to change these
## but if the make does not work look into it
CFLAGS = -fPIC -Wall -I. $(shell pkg-config gtk+-2.0 --cflags)
LIBFLAGS = -shared $(shell pkg-config gtk+-2.0 --libs)

## Set all the locale converstion stuff
## if necessary
LOCALEDIR ?= /usr/share/locale
PACKAGE ?= gkrellm-gtodo
ifeq ($(enable_nls),1)
    CFLAGS += -DENABLE_NLS -DLOCALEDIR=\"$(LOCALEDIR)\"
    export enable_nls
endif
CFLAGS += -DPACKAGE="\"$(PACKAGE)\""
export PACKAGE LOCALEDIR

## File name and target definitions
## DO NOT CHANGE THESE or the make will break
PROG = $(PACKAGE)
EXTRAS := gtodo-timer.xpm warn.xpm
SOURCES = $(wildcard *.c)
OBJECTS = $(SOURCES:.c=.o)
TARGET = $(PROG).so

## Make targets
all: $(TARGET) convert trans

$(PROG): $(TARGET)

strict: clean
	$(MAKE) CFLAGS="-Werror $(CFLAGS)"

$(TARGET): $(OBJECTS)
	@echo "Linking $@..."
	@$(CC) $(LIBFLAGS) $(OBJECTS) -o $(TARGET)

trans:
ifeq ($(enable_nls),1)
	@echo "Building translations..."
	@$(MAKE) -C po --no-print-directory all
endif

gtodo.o: gtodo.c gtodo.h main.h debug.h $(EXTRAS)
	@echo "Compiling gtodo.c..."
	@$(CC) $(CFLAGS) -c $< -o $@

%.o: %.c %.h main.h debug.h
	@echo "Compiling $<..."
	@$(CC) $(CFLAGS) -c $< -o $@

convert: convert.in
	@echo "Generating conversion script..."
	@sed -e 's%@PERL@%'$(shell which perl)'%' $< > $@ ; \
	chmod 755 $@

install: $(TARGET) install_trans
	@strip -s $(TARGET)
	@if [ -d $(GLOBAL_INSTALL_PATH) -a -w $(GLOBAL_INSTALL_PATH) ]; then \
		echo "Installing to global location: $(GLOBAL_INSTALL_PATH)"; \
		install -m 644 $(TARGET) $(GLOBAL_INSTALL_PATH)/$(TARGET); \
	else \
		echo "Installing to user location: $(USER_INSTALL_PATH)"; \
		install -D -m 644 $(TARGET) $(USER_INSTALL_PATH)/$(TARGET); \
	fi

install_trans:
ifeq ($(enable_nls),1)
	@echo "Installing translations..."
	@$(MAKE) -C po --no-print-directory install
endif

uninstall:
	@if [ -w $(GLOBAL_INSTALL_PATH)/$(TARGET) ]; then \
		echo "Uninstalling from $(GLOBAL_INSTALL_PATH)"; \
		$(RM) $(GLOBAL_INSTALL_PATH)/$(TARGET); \
	fi
	@if [ -w $(USER_INSTALL_PATH)/$(TARGET) ]; then \
		echo "Uninstalling from $(USER_INSTALL_PATH)"; \
		$(RM) $(USER_INSTALL_PATH)/$(TARGET); \
	fi

clean:
	@$(MAKE) -C po --no-print-directory clean
	$(RM) $(OBJECTS) $(TARGET) *.so	convert *~ #*

package: clean
	@$(RM) ../gkrell-gtodo-*.tar.gz
	@if [ "$$VERSION" == "" ]; then \
	    VERSION=$(shell cat main.h | grep GTODO_VERSION | sed -e 's%#[a-z]*\ [A-Z_]*\ *\"\(.*\)\"%\1%g'); \
	fi; \
	cd .. ; echo "Packaging code to version $$VERSION..."; \
	tar --exclude CVS --exclude core* -zcvf gkrellm-gtodo-$$VERSION.tar.gz gkrellm-gtodo

### This is an internal target
### I sometime forget to update the date of the release in the
### ChangeLog so this will help me do it.
### The user should not be interested in this target...
release:
	@[ -x $(HOME)/bin/clean ] && $(HOME)/bin/clean >/dev/null 2>&1 ; \
	version=$(shell cat main.h | grep GTODO_VERSION | sed -e 's%#[a-z]*\ [A-Z_]*\ *\"\(.*\)\"%\1%g') ; \
	echo "Making release $$version..." ; \
	sed -e 's%\('$$version'\)$$%\1\ \ \ ('$(shell date +"%m/%d/%Y")')%' ChangeLog > .tmp ; \
	mv .tmp ChangeLog ; \
	cvs ci -m "Updated the release date for version $$version" ChangeLog ; \
	cvs diff >/dev/null 2>&1 ; \
	if [ $$? -ne 0 ]; then echo "Uncommited changes found! Commit them first!"; exit 1; fi ; \
	rver=`echo $$version | sed -e 's%\.%_%g'` ; \
	echo "Tagging version $$version as 'release-$$rver'..." ; \
	cvs -q tag release-$$rver
	@$(MAKE) --no-print-directory package VERSION=$$version
pot:
	@echo "Generating PO Template file..."
	@xgettext -k_ -kN_ *.c -o po/$(PACKAGE).pot
###

### DEBUGGING TARGETS
### You should do 'make clean' before 'make debug'
### just in case some of the objects are up-to-date
debug:
	$(MAKE) CFLAGS="$(CFLAGS) -g -DDEBUG"

debuginstall: debug install
### END DEBUGGING TARGETS
