prefix  	:= @prefix@
exec_prefix	:= @exec_prefix@
srcdir		:= @srcdir@
datarootdir	:= @datarootdir@
dstdir  	:= @datadir@
slnkdir 	:= @bindir@
VPATH		:= $(srcdir)

STATIC		:= @STATIC@

doc_files	:= docs INSTALL CREDITS FAQ GNUGPL HELP README TODO AGREEMENT remote.help

FILES_TO_INSTALL := *.tcl hotmlog.htm amsn amsn-remote amsn-remote-CLI amsn.desktop langlist
FILES_TO_INSTALL += $(doc_files)
FILES_TO_INSTALL += desktop-icons lang skins utils/des utils/base64 utils/drawboard utils/framec utils/http
FILES_TO_INSTALL += utils/pixmapscroll utils/pixmapmenu utils/contentmanager
FILES_TO_INSTALL += utils/scalable-bg utils/sha1 utils/snit utils/BWidget-1.9.0 utils/dpbrowser
FILES_TO_INSTALL += plugins utils/sexytile
FILES_TO_INSTALL += utils/log utils/uri utils/combobox utils/voipcontrols
FILES_TO_INSTALL += utils/md4 utils/sasl ca-certs

FILES_TO_EXCLUDE := lang/genpage.c lang/addkey.tcl lang/lang1.tmpl lang/lang2.tmpl lang/lang3.tmpl lang/langchk.sh lang/complete.pl lang/genlangfiles.c

capture_dir := $(srcdir)/utils/linux/capture
webcamsn_dir := $(srcdir)/utils/webcamsn
tcl_siren_dir := $(srcdir)/utils/tcl_siren
tkcximage_dir := $(srcdir)/utils/TkCximage
tray_dir := $(srcdir)/utils/linux/traydock
linflash_dir := $(srcdir)/utils/linux/linflash
tclISF_dir := $(srcdir)/utils/tclISF
async_dir := $(srcdir)/utils/asyncresolver
macosx_dir := $(srcdir)/utils/macosx
tcl_farsight_dir := $(srcdir)/utils/farsight
gupnp_dir := $(srcdir)/utils/gupnp

# programs
CC		:= @CC@
CXX		:= @CXX@

# misc
PACKAGE		:= @APPNAME@
BASEVERSION	:= @APPVERSION@
ifeq (@APPSTABLE@,yes)
RELEASEVERSION	:= @APPRELEASE@
else
RELEASEVERSION  := svn`which svnversion > /dev/null && svnversion ||  true`
endif
VERSION		:= $(BASEVERSION)-$(RELEASEVERSION)
TCL_VERSION     := @TCL_VERSION@
TK_VERSION      := @TK_VERSION@

# for CFLAGS
#WARN_FLAGS	:= -Wall -Wmissing-prototypes -Wstrict-prototypes -Wpointer-arith
WARN_FLAGS	:= -w
LFS_FLAGS	:= -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
LIB_FLAGS	:= -I. -I$(capture_dir) -I$(capture_dir)/libng -I$(capture_dir)/structs
LIB_FLAGS	+= -I$(webcamsn_dir)/src -I$(tcl_siren_dir)/src
LIB_FLAGS	+= -I$(tclISF_dir)/src/libISF
LIBDIR		:= @LIBDIR@


# various libraries
THREAD_LIBS	:= @LIBPTHREAD@


# Directories in which the Tcl core can be found
TCL_INC_SPEC 	:= @TCL_INC_SPEC@
TCL_STUB_LIB_SPEC := @TCL_STUB_LIB_SPEC@

# Directories in which the Tk core can be found
TK_INC_SPEC 	:= @TK_INC_SPEC@
TK_STUB_LIB_SPEC := @TK_STUB_LIB_SPEC@

# C Flags for X
X_CFLAGS := @X_CFLAGS@

# stuff configure has found
FOUND_X11	:= @FOUND_X11@
FOUND_OS	:= @FOUND_OS@
USE_MMX		:= @USE_MMX@

# build final cflags
CFLAGS   := @CFLAGS@
ifeq (@DEBUG@,no)
CFLAGS   += -O2
else
CFLAGS   += -g3 -DDEBUG
endif

CFLAGS   += $(WARN_FLAGS)
CFLAGS   += $(LFS_FLAGS)
CFLAGS	 += ${TCL_INC_SPEC}
CFLAGS	 += ${TK_INC_SPEC}
CFLAGS   += @LIBV4L_CFLAGS@
ifeq (@HAVE_LIBV4L@,yes)
  CFLAGS += -DHAVE_LIBV4L
endif
ifeq (@HAVE_GUPNP@,yes)
  CFLAGS += @GUPNP_CFLAGS@
endif
CFLAGS	 += ${X_CFLAGS}
CFLAGS   += $(LIB_FLAGS)
CFLAGS   += -DPACKAGE='"$(PACKAGE)"'
CFLAGS   += -DVERSION='"$(VERSION)"'
CFLAGS   += -DLIBDIR='"$(LIBDIR)"'

CXXFLAGS := @CXXFLAGS@
ifeq (@DEBUG@,no)
CXXFLAGS   += -O2
else
CXXFLAGS   += -g3 -DDEBUG
endif

CXXFLAGS   += $(WARN_FLAGS)
CXXFLAGS   += $(LFS_FLAGS)
CXXFLAGS   += ${TCL_INC_SPEC}
CXXFLAGS   += ${TK_INC_SPEC}
CXXFLAGS   += -I$(tkcximage_dir)/src -I$(tkcximage_dir)/src/CxImage
CXXFLAGS   += -DPACKAGE='"$(PACKAGE)"'
CXXFLAGS   += -DVERSION='"$(VERSION)"'
CXXFLAGS   += -DLIBDIR='"$(LIBDIR)"'

ifeq ($(STATIC),yes)
CXXFLAGS += -static-libgcc -L.
endif


GST_LIBS        := @GST_LIBS@ @GST_INTERFACES_LIBS@
GST_CFLAGS      := @GST_CFLAGS@ @GST_INTERFACES_CFLAGS@
FARSIGHT2_LIBS  := @FARSIGHT2_LIBS@
FARSIGHT2_CFLAGS := @FARSIGHT2_CFLAGS@


# for gcc3
#CFLAGS   += -std-gnu99

# shared objects need -fPIC
CFLAGS   += -fPIC
CXXFLAGS += -fPIC

# libraries
ifeq (@FOUND_OS@,mac)
  LDLIBS  := @LDLIBS@ -lz -lm
else
  LDLIBS  := @LDLIBS@ -lpng -lz -lm -ljpeg
endif
LDFLAGS := @LDFLAGS@

ifeq (@FOUND_OS@,mac)
	SHLIB_EXTENSION := dylib
else
	SHLIB_EXTENSION := so
endif

ifeq (@DEBUG@,no)
	ifneq (@FOUND_OS@,mac)
LDFLAGS += -s
	endif
endif
X_LIBS  := @X_LIBS@
CXX_LIB := @CXX_LIB@

LDLIBS	 += ${TCL_STUB_LIB_SPEC}
LDLIBS	 += ${TK_STUB_LIB_SPEC}
LDLIBS	 += @LIBV4L_LIBS@

ifeq (@HAVE_GUPNP@,yes)
  LDLIBS += @GUPNP_LIBS@
  FILES_TO_INSTALL += utils/gupnp/gupnp.$(SHLIB_EXTENSION) utils/gupnp/pkgIndex.tcl
endif

ifeq (@HAVE_FARSIGHT@,yes)
  FILES_TO_INSTALL += utils/farsight/tcl_farsight.$(SHLIB_EXTENSION) utils/farsight/pkgIndex.tcl
endif

#AppMain.tcl is needed by MacOSX : do not try to simplify by moving it outside the ifeq/else.
ifeq ($(FOUND_OS),linux)
FILES_TO_INSTALL += utils/webcamsn/webcamsn.so utils/webcamsn/pkgIndex.tcl utils/TkCximage/TkCximage.so utils/TkCximage/pkgIndex.tcl utils/linux/capture/capture.so utils/linux/capture/pkgIndex.tcl utils/linux/linflash/flash.so utils/linux/linflash/pkgIndex.tcl utils/linux/traydock/libtray.so  utils/linux/traydock/pkgIndex.tcl utils/linux/capture/libng/plugins/drv0-v4l2.so utils/linux/capture/libng/plugins/drv1-v4l.so utils/linux/capture/libng/plugins/conv-mjpeg.so utils/linux/capture/libng/plugins/sn9c10x.so utils/tcl_siren/tcl_siren.so utils/tcl_siren/pkgIndex.tcl utils/tclISF/tclISF.so utils/tclISF/pkgIndex.tcl utils/asyncresolver/asyncresolver.tcl utils/asyncresolver/libasyncresolver.so utils/asyncresolver/pkgIndex.tcl

FILES_TO_EXCLUDE += AppMain.tcl
else 
	ifeq ($(FOUND_OS),bsd)
FILES_TO_INSTALL += utils/webcamsn/webcamsn.so utils/webcamsn/pkgIndex.tcl utils/TkCximage/TkCximage.so utils/TkCximage/pkgIndex.tcl utils/linux/linflash/flash.so utils/linux/linflash/pkgIndex.tcl utils/linux/traydock/libtray.so  utils/linux/traydock/pkgIndex.tcl utils/tcl_siren/tcl_siren.so utils/tcl_siren/pkgIndex.tcl utils/tclISF/tclISF.so utils/tclISF/pkgIndex.tcl utils/asyncresolver/asyncresolver.tcl utils/asyncresolver/libasyncresolver.so utils/asyncresolver/pkgIndex.tcl

FILES_TO_EXCLUDE += AppMain.tcl
	else 
		ifeq ($(FOUND_OS),solaris)
FILES_TO_INSTALL += utils/webcamsn/webcamsn.so utils/webcamsn/pkgIndex.tcl utils/TkCximage/TkCximage.so utils/TkCximage/pkgIndex.tcl utils/linux/capture/capture.so utils/linux/capture/pkgIndex.tcl utils/linux/linflash/flash.so utils/linux/linflash/pkgIndex.tcl utils/linux/traydock/libtray.so  utils/linux/traydock/pkgIndex.tcl utils/linux/capture/libng/plugins/drv0-v4l2.so utils/linux/capture/libng/plugins/conv-mjpeg.so utils/tcl_siren/tcl_siren.so utils/tcl_siren/pkgIndex.tcl utils/tclISF/tclISF.so utils/tclISF/pkgIndex.tcl utils/asyncresolver/asyncresolver.tcl utils/asyncresolver/libasyncresolver.so utils/asyncresolver/pkgIndex.tcl

FILES_TO_EXCLUDE += AppMain.tcl
		endif
	endif
endif

CONFIGURE_ARGS := @CONFIGURE_ARGS@

#########################################################
# targets

build: all

Makefile: $(srcdir)/Makefile.in $(srcdir)/configure
	$(srcdir)/configure $(CONFIGURE_ARGS)

$(srcdir)/configure: $(srcdir)/configure.ac
	(cd $(srcdir); autoreconf -i)

clean::
	find . -name \*~ -print | xargs rm -f
	find . -name \*.o -print | xargs rm -f
	find . -name \*.a -print | xargs rm -f
	find . -name \*.dep -print | xargs rm -f


distclean:: clean
	-rm -f Makefile Make.config
	-rm -f config.cache config.h config.log config.status


install:: $(FILES_TO_INSTALL)
	rm -Rf $(DESTDIR)$(dstdir)/$(PACKAGE)

	mkdir -p $(DESTDIR)$(dstdir)/$(PACKAGE)
	mkdir -p $(DESTDIR)$(slnkdir)
	mkdir -p $(DESTDIR)$(dstdir)/applications/
	mkdir -p $(DESTDIR)$(dstdir)/pixmaps/

	@for p in $?; do \
		mkdir -p `dirname $(DESTDIR)$(dstdir)/$(PACKAGE)/$$p`;\
		cp -Rf $(srcdir)/$$p $(DESTDIR)$(dstdir)/$(PACKAGE)/$$p; \
	done
	find $(DESTDIR)$(dstdir)/$(PACKAGE) -name '.svn' -print | xargs rm -Rf
	@for p in $(FILES_TO_EXCLUDE); do \
		rm -Rf $(DESTDIR)$(dstdir)/$(PACKAGE)/$$p; \
	done

	ln -sf $(dstdir)/$(PACKAGE)/amsn $(DESTDIR)$(slnkdir)/amsn
	ln -sf $(dstdir)/$(PACKAGE)/amsn-remote $(DESTDIR)$(slnkdir)/amsn-remote
	ln -sf $(dstdir)/$(PACKAGE)/amsn-remote-CLI $(DESTDIR)$(slnkdir)/amsn-remote-CLI

	cp $(srcdir)/amsn.desktop $(DESTDIR)$(dstdir)/applications/

	ln -sf $(dstdir)/$(PACKAGE)/desktop-icons/48x48/apps/amsn.png $(DESTDIR)$(dstdir)/pixmaps/

uninstall::
	rm -Rf $(dstdir)/$(PACKAGE)
	rm -f $(slnkdir)/amsn
	rm -f $(slnkdir)/amsn-remote
	rm -f $(slnkdir)/amsn-remote-CLI
	rm -f $(dstdir)/applications/amsn.desktop
	rm -f $(dstdir)/pixmaps/amsn.png

rpm: all
	mkdir -p ./distrib

	rm -f THIS.TK.SPEC
	echo "%define _builddir `pwd`" > THIS.AMSN.SPEC
	echo "%define _rpmdir /tmp/RPMS" >> THIS.AMSN.SPEC
	echo "%define _prefix $(prefix)" >> THIS.AMSN.SPEC
	echo "%define _tmppath /tmp" >> THIS.AMSN.SPEC
	echo "%define _datadir $(dstdir)/$(PACKAGE)" >> THIS.AMSN.SPEC
	echo "%define _bindir $(slnkdir)" >> THIS.AMSN.SPEC
	echo "%define _doc_files $(doc_files)" >> THIS.AMSN.SPEC
	echo "%define _platform `uname -m`" >> THIS.AMSN.SPEC
	echo "%define _version $(BASEVERSION)" >> THIS.AMSN.SPEC
	echo "%define __release $(RELEASEVERSION)" >> THIS.AMSN.SPEC

	cat amsn.spec >> THIS.AMSN.SPEC
	mkdir -p /tmp/RPMS
	rpmbuild -bb THIS.AMSN.SPEC
	rm -rf ./distrib/RPMS
	mv /tmp/RPMS ./distrib
	rm -rf /tmp/RPMS THIS.AMSN.SPEC

deb: all
	mkdir -p ./distrib/DEB

	sed "s/#VERSION#/${VERSION}/" debian/changelog.in > debian/changelog
	sed "s/#TCL_VERSION#/${TCL_VERSION}/" debian/control.in > debian/control.tmp
	sed "s/#TK_VERSION#/${TK_VERSION}/" debian/control.tmp > debian/control
	rm debian/control.tmp
	fakeroot debian/rules clean
	fakeroot debian/rules binary VERSION=${VERSION}

	rm -f debian/changelog
	rm -f debian/control
	rm -rf debian/amsn

ifeq ($(FOUND_OS),mac)
libstdc++.a :
	ln -s `g++ -print-file-name=libstdc++-static.a` libstdc++.a
else
libstdc++.a :
	ln -s `g++ -print-file-name=libstdc++.a`
endif


#########################################################
# include stuff

# subdirs

include $(tkcximage_dir)/Rules.mk
include $(tkcximage_dir)/src/Rules.mk
include $(tkcximage_dir)/src/CxImage/Rules.mk
include $(tkcximage_dir)/src/Rules.mk

include $(webcamsn_dir)/Rules.mk
include $(webcamsn_dir)/src/Rules.mk

include $(tcl_siren_dir)/Rules.mk
include $(tcl_siren_dir)/src/Rules.mk

include $(tclISF_dir)/Rules.mk
include $(tclISF_dir)/src/Rules.mk
include $(tclISF_dir)/src/libISF/Rules.mk
include $(tclISF_dir)/src/Rules.mk

include $(async_dir)/Rules.mk
include $(async_dir)/src/Rules.mk

ifeq (@HAVE_FARSIGHT@,yes)
  include $(tcl_farsight_dir)/Rules.mk
  include $(tcl_farsight_dir)/src/Rules.mk
endif

ifeq (@HAVE_GUPNP@,yes)
  include $(gupnp_dir)/Rules.mk
endif

ifeq ($(FOUND_OS),linux)
  include $(capture_dir)/Rules.mk
  include $(capture_dir)/libng/Rules.mk
  include $(capture_dir)/libng/plugins/Rules.mk

  ifeq ($(FOUND_X11),yes)
      ADDLIBS	 += ${X_LIBS}
      include $(tray_dir)/Rules.mk
      include $(linflash_dir)/Rules.mk
  endif
else
  ifeq ($(FOUND_OS),mac)
	include $(macosx_dir)/sndplay-src/Rules.mk
	include $(macosx_dir)/growl1.0/Rules.mk
	include $(macosx_dir)/growl1.0/src/Rules.mk
	include $(macosx_dir)/tclCarbon/Rules.mk
	include $(macosx_dir)/tclCarbon/src/Rules.mk
	include $(macosx_dir)/macDock/Rules.mk
	include $(macosx_dir)/macDock/src/Rules.mk
	include $(macosx_dir)/statusicon/Rules.mk

.PHONY: finishbuild
finishbuild:
	./utils/macosx/finishbuild.sh
all:: finishbuild

  endif
  ifeq ($(FOUND_OS),bsd)
    ifeq ($(FOUND_X11),yes)
       ADDLIBS    += ${X_LIBS} 
      include $(tray_dir)/Rules.mk
      include $(linflash_dir)/Rules.mk
    endif
  else
    ifeq ($(FOUND_OS),solaris)
      include $(capture_dir)/Rules.mk
      include $(capture_dir)/libng/Rules.mk
      include $(capture_dir)/libng/plugins/Rules.mk

      ifeq ($(FOUND_X11),yes)
        ADDLIBS	 += ${X_LIBS}
        include $(tray_dir)/Rules.mk
        include $(linflash_dir)/Rules.mk
      endif
    endif
  endif
endif

#########################################################
# some rules ...

include $(srcdir)/Compile.mk
