# Makefile for gkrelltop 
#
# tries to autodetect for gtk+-2.0 to compile for
# gkrellm 2 or if it is lacking it tries to compile
# for gkrellm 1. 
# If it fails to build, then maybe you dont have the 
# gtk+-2.0 or gtk+-1.2 development tools (which you need 
# to have installed) to compile this package.
#
# for linux need the -DLINUX as a CFLAGS parameter

# to compile for FREEBSD the CFLAGS parameter 
# needs to be -DFREEBSD 
# and as David Gardner pointed out, FreeBSD users need to use 
# the GNU make utility (gmake) instead of the BSD make 
# utility (make) in order for it to compile.
# 
# I have attempted to detect the os by the OSFLAG variable 
# NOTE that it will work only for LINUX or FREEBSD 
# 
# if you think it should work for your os than substituting
# your $(OSFLAG) will not quite do the trick because the wmtop code 
# (the three_top.c file) only recognises those two (LINUX and FREEBSD)
# parameters. Let me know if the more recent versions of wmtop 
# support more OS.


OSFLAG = $(shell uname | tr '[:lower:]' '[:upper:]')
SHELL=/bin/sh

GKRELL1FLAG=1
#find out if we have gkrellm 2 or 1 (from the gtk+ version)
GKRELL1FLAG=$(shell bash -c 'pkg-config gtk+-2.0 --cflags &>/dev/null && echo 0')
GKRELLTOP = gkrelltop.so
OBJ = top_three.o gkrelltop.o 
EXTRA = krell_panel1.xpm

INSTALLDIR ?= $(PREFIX)/$(DESTDIR)
ifeq ($(INSTALLDIR),)
	INSTALLDIR=/usr/lib/gkrellm2/plugins
endif


ifeq ($(GKRELL1FLAG),0)
# Parameters for gkrellm version 2.*
CFLAGS2 = -D$(OSFLAG) -DGKRELLM2 -fPIC -Wall `pkg-config gtk+-2.0 --cflags`
LIBS =  
CC=/usr/bin/gcc $(CFLAGS2)

else 
# Parameters for gkrellm version 1.*
CC=/usr/bin/cc
CFLAGS = -D$(OSFLAG) -Wall -fPIC `gtk-config --cflags` `imlib-config --cflags-gdk` 
LIBS=

endif


# Parameters for the gkrelltopd server plugin
ifeq ($(glib12),yes)
WANT_GLIB12 = yes
endif
ifeq ($(glib12),1)
WANT_GLIB12 = yes
endif

ifeq ($(WANT_GLIB12),yes)
CFLAGSD = -I/usr/X11R6/include -D$(OSFLAG) -fPIC -Wall `glib-config --cflags`
LIBSD = `glib-config --libs`
CONFIGURE_ARGS += --with-glib12
else
CFLAGSD = -D$(OSFLAG) -fPIC -Wall `pkg-config glib-2.0 --cflags`
LIBSD = `pkg-config glib-2.0 --libs`
endif
INSTALLDIRD ?= $(PREFIXD)/$(DESTDIR)
ifeq ($(INSTALLDIRD),)
	INSTALLDIRD=$(HOME)/.gkrellm2/plugins-gkrellmd
endif
CCD=/usr/bin/gcc $(CFLAGSD)
OBJD = gkrelltopd.o top_three.o

DUMMY_VAR := $(shell ./configure $(CONFIGURE_ARGS))
HAVE_GKRELLMD = $(shell grep -c HAVE_GKRELLMD configure.h)


ifneq ($(HAVE_GKRELLMD),1)
WARN = $(warning Note: cannot compile server plugin.  See configure.log.)
endif


ifeq ($(HAVE_GKRELLMD),1)
	GKRELLTOPD = gkrelltopd.so
endif

# By default do the compilation for gkrellm 2.*
all: $(GKRELLTOP) $(GKRELLTOPD)


warn: ; $(WARN)

$(GKRELLTOP): $(OBJ) warn
	$(CC)  -shared $(OBJ) -o $(GKRELLTOP) 

# Compile gkrelltopd.so server plugin
server: $(GKRELLTOPD) warn
	@echo ""

$(GKRELLTOPD): $(OBJD) 
	$(CCD)  $(LIBSD) -shared $(OBJD) -o $(GKRELLTOPD)

gkrelltop.o: gkrelltop.c
	$(CC) -c gkrelltop.c -o gkrelltop.o

top_three.o: top_three.c
	$(CC) -D$(OSFLAG) -c top_three.c -o top_three.o

gkrelltopd.o: gkrelltopd.c
	$(CCD) -c gkrelltopd.c -o gkrelltopd.o

install: $(GKRELLTOP)
	install -c -s -m 644 $(GKRELLTOP) $(INSTALLDIR)
	install -c -s -m 644 $(GKRELLTOPD) $(INSTALLDIRD)

install-server: $(GKRELLTOPD)
	install -c -s -m 644 $(GKRELLTOPD) $(INSTALLDIRD)

test_top: top_three.o test_top.o
	$(CC)  -D$(OSFLAG) top_three.o test_top.o -o test_top

view: $(GKRELLTOP)
	gkrellm -p $(GKRELLTOP)

deb:
	fakeroot debian/rules binary

orig:
	echo `pwd` | grep 'orig' || (mkdir `pwd`.orig; cp -p Makefile README config.guess config.sub configure gkrelltop.c gkrelltop.h gkrelltopd.c krell_image.xpm support.h test_top.c top_three.c `pwd`.orig/ )

package:
	@dpkg-buildpackage -rfakeroot -uc -us

clean: 
	@rm -f *.o *.so configure.h configure.log test_top

