#
# NAME
#   makefile -- Plain makefile that does not need autoconf
# SYNOPSIS
#   * make
#	* make clean
#   * make install
#   * make uninstall
#   * make reinstall
#   * make doc
#	* make astyle
#   * make pkgIndex.tcl
# PURPOSE
#   Build libraries and manage Gnocl project files
# NOTES
# 	There are two labels smalldist:
# DEPENDENCIES
#	robodoc
#	astyle
# HISTORY
#   Added libglade
#	Last worked on by WJG 15/01/09


# $Id: Makefile,v 1.51 2005/08/16 20:57:45 baum Exp $

export VERSION := 0.9.94
export SUFFIX :=

# It is tried to guess the TCL version for stubs support. If the guess does
# not work, please use for example for Tcl 8.3: 'make TCL_VERSION=8.3'
#
# for installation of the resulting libs in a special directory
# use 'make DESTDIR=/dir/to/use'

# comment the next lines if you don't want canvas, gconf and/or gnome support
# or just do a "make gnocl" and then
# "make gconf", "make gnome", make "vfs" or "make canvas" just as required
#USE_CANVAS := 1make
#USE_GCONF := 1
#USE_VFS := 1
#USE_VTE := 1
#USE_GNOME := 1
#USE_SV := 1

# for releases use -Os, this makes the lib 15% smaller
# amd64 need -fPIC, x86 works with and without
CFLAGS += -Wall -pedantic -fPIC
GTK_24 := $(shell pkg-config --atleast-version=2.4 'gtk+-2.0' && echo yes)
GTK_26 := $(shell pkg-config --atleast-version=2.6 'gtk+-2.0' && echo yes)

# WJG ADDED
GTK_28  := $(shell pkg-config --atleast-version=2.8 'gtk+-2.0' && echo yes)
GTK_210 := $(shell pkg-config --atleast-version=2.10 'gtk+-2.0' && echo yes)
GTK_212 := $(shell pkg-config --atleast-version=2.12 'gtk+-2.0' && echo yes)

# get tclsh to tell us which version of Tcl is installed on the system
TCL_VERSION := $(shell echo 'puts $$tcl_version' | tclsh)

# this breaks build on GTK 2.4
# -DGTK_DISABLE_DEPRECATED -DGNOME_DISABLE_DEPRECATED
export ADDCFLAGS := -DVERSION=\"$(VERSION)\" \
	-DUSE_TCL_STUBS $(shell pkg-config \
	--cflags gtk+-2.0) \
	-I/usr/include/tcl$(TCL_VERSION) \
	-I/usr/local/include/gtksourceview-2.0 \
	-I/usr/include/libglade-2.0/  \
	-I/usr/local/include/gtkhtml /

# don't forget to add libs for new widgets!!
export LIBS := $(shell pkg-config \
	--libs gtk+-2.0 libglade-2.0 vte) \
	-ltclstub$(TCL_VERSION)

############################################################
# There should be no need to edit something below this line
############################################################

TGZDIR:=gnocl-$(VERSION)$(SUFFIX)
TGZFILE:=gnocl-$(VERSION)$(SUFFIX).tar.bz2

# add new objects to build a the bottom of the list
GTK_OBJ := \
   arrowButton.o \
   box.o \
   button.o \
   callback.o \
   checkButton.o \
   colorSelection.o \
   commands.o \
   debug.o \
   dialog.o \
   fontSelection.o \
   entry.o \
   eventBox.o \
   gnocl.o \
   parseOptions.o \
   helperFuncs.o \
   image.o \
   label.o \
   menu.o \
   menuBar.o \
   menuCheckItem.o \
   menuRadioItem.o \
   menuSeparator.o \
   menuItem.o \
   notebook.o \
   paned.o \
   plug.o \
   progressBar.o \
   radioButton.o \
   scale.o \
   scaleButton.o \
   scrolledWindow.o \
   separator.o \
   socket.o \
   spinButton.o \
   statusBar.o \
   table.o \
   text.o \
   treeList.o \
   toolBar.o \
   window.o \
   deprecated/combo.o \
   deprecated/fileSelection.o \
   deprecated/optionMenu.o \
   recentChooserDialog.o \
   print.o \
   pixbuf.o \
   statusicon.o

#####
#
#####

ifeq ($(GTK_24),yes)
   GTK_OBJ += action.o \
            colorButton.o \
            comboBox.o \
            expander.o \
            fileChooser.o \
            fontButton.o
endif

ifeq ($(GTK_26),yes)
   GTK_OBJ += aboutDialog.o \
   			toggleButton.o \
   			volumeButton.o \
   			linkButton.o \
   			drawingArea.o \
   			fileChooserButton.o \
   			folderChooserButton.o \
   			handleBox.o
endif
#   			assistant.o

# some new simple megawidgets
# GTK_OBJ += labelEntry.o


# setr paths to sub-dirs containing module code
SUBDIRS :=
ifdef USE_CANVAS
   SUBDIRS += canvas
endif
ifdef USE_GCONF
   SUBDIRS += gconf
endif
ifdef USE_VFS
   SUBDIRS += vfs
endif
ifdef USE_GNOME
   SUBDIRS += gnome
endif
ifdef USE_VTE
   SUBDIRS += vte
endif
ifdef USE_SV
   SUBDIRS += sourceview
endif
ifdef USE_HTML
   SUBDIRS += html
endif

# is this still necessary? who uses man files?
DOCFILES := manpages.txt gnocl.xml concepts.xml nonwidgetcmds.xml \
           appletfactory.xml file.xml mime.xml sed_cmds txt2xml.tcl

EXCLFILES := -name "CVS" -o -name ".??*" -o -name "qqq*"

.PHONY: all gnocl canvas gconf vfs vte gnome sourceview html clean dist install installAndTest uninstall

%.o : %.c; $(CC) -c $(CPPFLAGS) $(CFLAGS) $(ADDCFLAGS) -o $*.o $<

all: pkgIndex.tcl gnocl.so $(SUBDIRS) ;

gnocl: gnocl.so ;

gnocl.so: $(GTK_OBJ)
	$(CC) -shared -o $@ $(GTK_OBJ) $(LIBS)

gnome canvas gconf vfs vte:
	$(MAKE) -C $@

gnoclCanvas.so:
	$(MAKE) -C canvas

gnoclGconf.so:
	$(MAKE) -C gconf

gnoclVFS.so:
	$(MAKE) -C vfs

gnoclGnome.so:
	$(MAKE) -C gnome

# Added by WJG 20/07/08
gnoclVTE.so:
    # rm -f $(GTK_OBJ) gnoclVTE.so
	$(MAKE) -C vte

# Added by WJG 10/10/08
gnoclSourceView.so:
	$(MAKE) -C sourceview

# Added by WJG 10/02/09
gnoclHTML.so:
	$(MAKE) -C html


#****
# NAME
#   smalldist --
# NOTE
#
# SOURCE
clean:
	rm -f $(GTK_OBJ) gnocl.so
	$(MAKE) -C canvas clean
	$(MAKE) -C gconf clean
	$(MAKE) -C gnome clean
	$(MAKE) -C vfs clean
	$(MAKE) -C vte clean
	$(MAKE) -C sv clean

#****
# NOTE
#	Run external Tcl script to install available packages.
# SOURCE
#
install:
	sudo ./install.tcl $(VERSION) install


#****
# NOTE
#	Run external Tcl script to install available packages.
# SOURCE
#
installAndTest:
	sudo ./install.tcl $(VERSION) install
	sudo ./install.tcl $(VERSION) test

#****
# NAME
#   smalldist --
# NOTE
#
# SOURCE
#
uninstall:
	sudo ./install.tcl $(VERSION) uninstall

#****
# NAME
#   reinstall --
# NOTE
#   Reinstall gnocl from recently compiled files
# SOURCE
#
reinstall:
	sudo ./install.tcl $(VERSION) uninstall
	sudo ./install.tcl $(VERSION) install

#****
# NAME
#   pkgIndex.tcl --
# NOTE
#
# SOURCE
#
pkgIndex.tcl: Makefile
	echo -e ',s/ [0-9]*\.[0-9]*\.[0-9]* / $(VERSION) /g\nwq' | ed $@

#****
# NAME
#   smalldist --
# NOTE
#
# SOURCE
#
smalldist: EXCLFILES += -o -path "*/doc/pics/*.png" -o -path "*/doc/*.gif"

smalldist: dist


#****
# NAME
#   dist --
# NOTE
#
# SOURCE
#
dist:
	(cd ..; \
	mkdir $(TGZDIR); mkdir $(TGZDIR)/doc ; mkdir $(TGZDIR)/doc/xml ; \
        for el in src src/canvas src/gconf src/gnome src/vfs src/deprecated; do \
           mkdir $(TGZDIR)/$$el; \
           cp -r $$el/{Makefile,*.[ch]} $(TGZDIR)/$$el; \
        done; \
        cp src/pkgIndex.tcl src/install.tcl $(TGZDIR)/src; \
	cp -r demos NEWS README license.terms gpg_key.txt $(TGZDIR); \
        (cd doc/xml; cp $(DOCFILES) ../../$(TGZDIR)/doc/xml); \
        cp -r doc/pics $(TGZDIR)/doc ; \
        cp -r doc/gnocl $(TGZDIR)/doc ; \
        find $(TGZDIR) \( $(EXCLFILES) \) -exec rm -r {} \; ; \
	tar -cjf $(TGZFILE) $(TGZDIR);  \
	rm -rf $(TGZDIR) )

#****
# NAME
#   doc --
# NOTE
#
# SOURCE
#
doc:
	doxygen Doxyfile

#****
# NAME
#   test-bz --
# NOTE
# SOURCE
#
test-bz:
	tar -xvjf $(TGZFILE)
	cd $(TGZDIR) && make && .demos/test.tcl
	rm -rf $(TGZDIR)

#****
# NAME
#   astyle --
# NOTE
#   Reformats sourcecode to coherent style.
#	http://astyle.sourceforge.net/
# SOURCE
#
astyle:
	astyle \
		--style=ansi \
		--pad=oper \
		--one-line=keep-statements \
		--force-indent=tab=4 \
		--suffix=none \
		--recursive "./*.c" "./*.h" \
		--indent-switches \
		--indent-cases \
		--mode=c \
		--brackets=break \
		--pad=paren \
		--break-blocks=all

#****
# NOTE
#   Re-set access rights to files.
# SOURCE
#
chmod:
	chmod -Rv a+rwx *

#****
# NAME
#   reinstall --
# NOTE
#   Reinstall gnocl from recently compiled files
# SOURCE
#
installApplet:
	echo -e 'install applet' > tst

#****
# NAME
#   reinstall --
# NOTE
#   Reinstall gnocl from recently compiled files
# SOURCE
#
removeApplet:
	echo -e 'remove applet'
