# -*- mode: makefile; coding: latin-9-unix -*-
#% Time-stamp: <2009-01-22 16:52:07 alain> %
#% $Id: Makefile,v 1.9 2009-01-22 15:52:28 cvsalain Exp $ %
#*******************************************************************************
# Makefile for Tcl/Tk projects
#
# Copyright 2006 by Alain Jaffre <jack.r [at] free.fr>
# Provided under the terms of
# GNU General Public License <http://www.gnu.org/licenses/gpl.html>
#
# It was build for my personal use, but could interest some other people.
# It try to be as generic as possible
#*******************************************************************************

#*******************************************************************************
# Project base name that we want to build
PRJ = netinfo

#*******************************************************************************
# Directory where are stored all used images
IMGDIR =img

#*******************************************************************************
# Directory where are stored all messages files
MSGDIR =msg

#*******************************************************************************
# Directory where we want resulting files
FINALDIR = final



#*******************************************************************************
# NO CHANGE NEEDED BELOW THAT LINE
#*******************************************************************************




#*******************************************************************************
# Tools
TCLKITDIR = /home/alain/dev_perso/tcl/tclkit/
SDX = sdx.kit
TCLKIT = tclkit
TCLKIT_WIN = tclkit-win32.exe 
TCLKIT_LINUX = tclkit-linux

#*******************************************************************************
# Libraries
LIBDIR = /usr/lib
BWIDGETLIB = bwidget1.7.0

#*******************************************************************************
# Build path
ROOTPATH = $(PRJ).vfs
LIBPATH = $(ROOTPATH)/lib
SRCPATH = $(ROOTPATH)/app-$(PRJ)

#*******************************************************************************
# Check what we have to do
ifdef IMGDIR
IMAGES = $(shell ls -l -d $(IMGDIR) | grep d)
endif
ifdef MSGDIR
MESSAGES = $(shell ls -l -d $(MSGDIR) | grep d)
endif
BWIDGET = $(shell cat $(PRJ).tcl | grep BWidget)


.PHONY: help usage kit win32 linux etk clean proper all

help: usage

usage:
	@echo ""
	@echo "Makefile for tcl/tk projects"
	@echo "USAGE: "
	@echo ""
	@echo "help     - That usage information"
	@echo "usage    - That usage information"
	@echo "kit      - Build the starkit file"
	@echo "win32    - Build the starpack for Windows 32 bits"
	@echo "linux    - Build the starpack for linux x86 32 bits"
	@echo "etk      - Build the starpack for linux x86 32 bits"
	@echo "clean    - Remove all intermediate files"
	@echo "proper   - Remove all files except source ones"
	@echo "all      - Build all files (kit, starpack for win 32, starpack for linux)"
	@echo ""

buildcommon: $(PRJ).tcl
	make clean
# source files
	mkdir -p $(SRCPATH)
	cp *.tcl $(SRCPATH)
# images
ifdef IMGDIR
ifeq ($(findstring d,$(IMAGES)),d)
# We really have an images directory so copy it
	mkdir -p $(SRCPATH)/$(IMGDIR)
	cp -f $(IMGDIR)/*.gif $(SRCPATH)/$(IMGDIR)
endif
endif
# messages
ifdef MSGDIR
ifeq ($(findstring d,$(MESSAGES)),d)
# We really have a messages directory so copy it
	mkdir -p $(SRCPATH)/$(MSGDIR)
	cp -f $(MSGDIR)/*.msg $(SRCPATH)/$(MSGDIR)
endif
endif
# libraries
ifeq ($(findstring BWidget,$(BWIDGET)),BWidget)
# we need bwidget library
	cp -r $(LIBDIR)/$(BWIDGETLIB) $(LIBPATH)
endif
	mkdir -p $(SRCPATH)/twapi
	cp -rf twapi/* $(SRCPATH)/twapi
# miscellaneous
	cp getmac.bat $(ROOTPATH)
	cp tclkit.ico $(ROOTPATH)

buildkittree: $(PRJ).tcl
	make buildcommon
# main.tcl
	@echo "package require starkit" > $(ROOTPATH)/main.tcl
	@echo "starkit::startup" >> $(ROOTPATH)/main.tcl
	@echo "source [file join [file dirname [file normalize [info script]]] app-"$(PRJ)" "$(PRJ)".tcl]" >> $(ROOTPATH)/main.tcl
#	@echo "package require app-"$(PRJ) >> $(ROOTPATH)/main.tcl

buildetk: $(PRJ).tcl
	make buildcommon
# main.tcl
	@echo "catch {::etcl::automanage .}" > $(ROOTPATH)/main.tcl
	@echo "source [file join [file dirname [file normalize [info script]]] app-"$(PRJ)" "$(PRJ)".tcl]" >> $(ROOTPATH)/main.tcl
	cd $(ROOTPATH) && zip -r $(PRJ).etk *
	mv $(ROOTPATH)/$(PRJ).etk .

clean:
	rm -rf $(ROOTPATH)

proper:
	make clean
	rm -f $(PRJ).kit
	rm -f $(PRJ).exe
	rm -f $(PRJ)
	rm -f $(PRJ).etk
	rm -rf $(FINALDIR)

kit: $(PRJ).tcl
	make buildkittree
	$(TCLKITDIR)$(TCLKIT) $(TCLKITDIR)$(SDX) wrap $(PRJ).kit
	make clean

win32: $(PRJ).tcl
	make buildkittree
	$(TCLKITDIR)$(TCLKIT) $(TCLKITDIR)$(SDX) wrap $(PRJ).exe -runtime $(TCLKITDIR)$(TCLKIT_WIN)
	make clean

linux: $(PRJ).tcl
	make buildkittree
	$(TCLKITDIR)$(TCLKIT) $(TCLKITDIR)$(SDX) wrap $(PRJ) -runtime $(TCLKITDIR)$(TCLKIT_LINUX)
	make clean

etk: $(PRJ).tcl
	make buildetk
	make clean

all: $(PRJ).tcl
	make buildkittree
# kit
	$(TCLKITDIR)$(TCLKIT) $(TCLKITDIR)$(SDX) wrap $(PRJ).kit
# win32
	$(TCLKITDIR)$(TCLKIT) $(TCLKITDIR)$(SDX) wrap $(PRJ).exe -runtime $(TCLKITDIR)$(TCLKIT_WIN)
# linux
	$(TCLKITDIR)$(TCLKIT) $(TCLKITDIR)$(SDX) wrap $(PRJ) -runtime $(TCLKITDIR)$(TCLKIT_LINUX)
# etk
	make buildetk
# final files
	rm -rf $(FINALDIR)
	mkdir $(FINALDIR)
	mv $(PRJ).kit $(FINALDIR)/
	mv $(PRJ).exe $(FINALDIR)/
	mv $(PRJ) $(FINALDIR)/
	mv $(PRJ).etk $(FINALDIR)/
	make clean




