# $Id: moodss.mk,v 1.11 2004/01/20 21:00:42 jfontain Exp $


VERSION = 17.17

# note: TCLSH and WISH must include path as they are used
# as shell interpreters inside scripts (using the #! syntax)
TCLSH = /usr/bin/tclsh
WISH = /usr/bin/wish
CC = gcc
# dynamic libraries compilation options (strip and optimize):
CFLAGS = -s -O2
TCLLIBDIR = /usr/lib
# eventually add Tcl includes path (use -Idirectory syntax):
INCLUDES =

# only use as the temporary installation directory, such as from a rpm
# specification file, otherwise change the directories below instead:
ROOTDIR =

# you may change the directories below:
BASEDIR =
BINDIR = $(BASEDIR)/usr/bin
LIBDIR = $(BASEDIR)/usr/lib
MANDIR = $(BASEDIR)/usr/share/man
DOCDIR = $(BASEDIR)/usr/share/doc
MOODSSLIBDIR = $(LIBDIR)/moodss
# where the modules will be finally installed (useful in rpm specification):
MODULESDIR = $(MOODSSLIBDIR)
MOODSSDOCDIR = $(DOCDIR)/moodss-$(VERSION)
# where the HTML documentation will be finally installed (useful in rpm specification):
MOODSSHTMLDIR = $(MOODSSDOCDIR)

# uncomment the MODULES entries for your platform below,
# while obviously commenting those entries for all the other platforms:
# Linux:
MODULES = apache apachex arp cpustats disks diskstats hddtemp interrupts kernmods log memstats minimal mounts myerrorlog myhealth myprocs myquery myreplication mystatus myvars netdev odbcquery partitions pci ping ps random route sensors smithy snmp snmptrap system trace usb Minimal Random minipy randpy
# Solaris:
#MODULES = apache apachex log minimal mounts myerrorlog myhealth myprocs myquery myreplication mystatus myvars odbcquery ping random smithy snmp snmptrap trace Minimal Random minipy randpy


### You should not need to change anything below this line ###
###    (let me know at jfontain@free.fr if you have to)    ###

all: htmldata.tcl moodss packlibs/libfilesystem.so.1.1 packlibs/libnetwork.so.1.20 packlibs/liblogging.so.1.0

install: all
	mkdir -p $(ROOTDIR)$(BINDIR)
	mkdir -p $(ROOTDIR)$(LIBDIR)
	mkdir -p $(ROOTDIR)$(MANDIR)/man1
	mkdir -p $(ROOTDIR)$(MOODSSDOCDIR)
	# append final modules directory to Tcl automatic package path
	sed -e 's,$$::tcl_library/moodss,$(MODULESDIR),;' moodss > $(ROOTDIR)$(BINDIR)/moodss
	chmod 755 $(ROOTDIR)$(BINDIR)/moodss
	# use final module directory for images in HTML help:
	for module in $(MODULES); do\
		directory=$(MOODSSLIBDIR)/$$module;\
		mkdir -p $(ROOTDIR)$$directory;\
		cp -f $$module/pkgIndex.tcl $(ROOTDIR)$$directory/;\
		if [ -r $$module/$$module.htm ]; then\
			sed -e "s,<img src=\",<img src=\"$(MODULESDIR)/$$module/,g;" $$module/$$module.htm >\
				$(ROOTDIR)$$directory/$$module.htm;\
		chmod 644 $(ROOTDIR)$$directory/$$module.htm;\
		fi;\
		for extension in gif pm py tcl; do\
			name=$$module/$$module.$$extension;\
			if [ -r $$name ]; then\
				cp -f $$name $(ROOTDIR)$$directory/;\
			fi;\
		done;\
	done
	cp -f apache/apacheu.tcl $(ROOTDIR)$(MOODSSLIBDIR)/apache/
	cp -f arp/vendor.txt $(ROOTDIR)$(MOODSSLIBDIR)/arp/
	for file in INSTALL hosts; do\
		cp -f ping/$$file $(ROOTDIR)$(MOODSSLIBDIR)/ping/;\
	done
	cp -f mystatus/mystatus.dat $(ROOTDIR)$(MOODSSLIBDIR)/mystatus/
	cp -f myvars/myvars.dat $(ROOTDIR)$(MOODSSLIBDIR)/myvars/
	cp -f myerrorlog/*.txt $(ROOTDIR)$(MOODSSLIBDIR)/myerrorlog/
	mkdir -p $(ROOTDIR)$(MOODSSLIBDIR)/packlibs
	for file in pkgIndex.tcl htmldata.tcl misc.tcl network.tcl threads.tcl linetask.tcl libfilesystem.so.1.1 libnetwork.so.1.20 liblogging.so.1.0; do\
		cp -f packlibs/$$file $(ROOTDIR)$(MOODSSLIBDIR)/packlibs/;\
	done
	mkdir -p $(ROOTDIR)$(MOODSSLIBDIR)/stooop
	for file in pkgIndex.tcl stooop.tcl switched.tcl; do\
		cp -f stooop/$$file $(ROOTDIR)$(MOODSSLIBDIR)/stooop/;\
	done
	mkdir -p $(ROOTDIR)$(MOODSSLIBDIR)/msgs
	for file in pkgIndex.tcl fr.msg; do\
		cp -f msgs/$$file $(ROOTDIR)$(MOODSSLIBDIR)/msgs/;\
	done
	mkdir -p $(ROOTDIR)$(MOODSSLIBDIR)/instance
	for file in pkgIndex.tcl instance.tcl; do\
		cp -f instance/$$file $(ROOTDIR)$(MOODSSLIBDIR)/instance/;\
	done
	# SQLite package, eventually install library (http://www.hwaci.com/sw/sqlite/):
	mkdir -p $(ROOTDIR)$(MOODSSLIBDIR)/sqlite
	echo 'package ifneeded sqlite 2 [list load [file join $$dir tclsqlite.so]]' > $(ROOTDIR)$(MOODSSLIBDIR)/sqlite/pkgIndex.tcl
	if [ -f sqlite/tclsqlite.so ]; then\
		cp -f sqlite/tclsqlite.so $(ROOTDIR)$(MOODSSLIBDIR)/sqlite/;\
	fi
	cp -f documentation/moodss.1 $(ROOTDIR)$(MANDIR)/man1/
	# in HTML documentation, convert relative URLs for modules to absolute ones:
	sed -e 's,<a href="../,<a href="$(MODULESDIR)/,;' documentation/modules.htm > $(ROOTDIR)$(MOODSSDOCDIR)/modules.htm
	cd documentation;\
		cp -f README CHANGES INSTALL DEVELOPMENT COPYRIGHT BUGS TODO moodss.htm develop.htm database.htm *.gif $(ROOTDIR)$(MOODSSDOCDIR)/;\
	cd ..


clean:
	rm -f moodss htmldata.tcl packlibs/htmldata.tcl packlibs/libfilesystem.so.1.1 packlibs/libnetwork.so.1.20 packlibs/liblogging.so.1.0

# combine all source into a single Tcl code file:
moodss:
	rm -f $@
	echo '#!$(WISH)' > $@
	$(TCLSH) source.tcl moodss.tcl | sed\
		-e 's/if {\[catch {package require stooop.*/if {1} {/;' -e 's/if {\[catch {package require switched.*/if {1} {/;'\
		-e 's/if {\[catch {package require scwoop.*/if {1} {/;' -e 's/if {\[catch {package require tkpiechart.*/if {1} {/;'\
		-e 's/if {\[catch {package require BWidget.*/if {1} {/;' >> $@
	chmod 755 $@

htmldata.tcl: documentation/moodss.htm
	# fix local image links according to final installation directory
	$(TCLSH) genhtml.tcl - $? | sed -e 's,<img src=",<img src="$(MOODSSHTMLDIR)/,g;' > $@
	cd packlibs/ && ln -sf ../$@ $@ && cd ..

libtclstub.a::
	rm -f $@; ln -s $(TCLLIBDIR)/libtclstub`echo 'puts $$tcl_version' | $(TCLSH)`.a $@ || ln -s $(TCLLIBDIR)/$@ $@

packlibs/libfilesystem.so.1.1: packlibs/filesystem.c libtclstub.a
	$(CC) -shared -fPIC -DUSE_TCL_STUBS $(CFLAGS) $(INCLUDES) -o $@ $?

packlibs/libnetwork.so.1.20: packlibs/network.c libtclstub.a
	$(CC) -shared -fPIC -DUSE_TCL_STUBS $(CFLAGS) $(INCLUDES) -o $@ $? -lresolv

packlibs/liblogging.so.1.0: packlibs/logging.c libtclstub.a
	$(CC) -shared -fPIC -DUSE_TCL_STUBS $(CFLAGS) $(INCLUDES) -o $@ $?
