## process this file with automake to produce Makefile.in

ACLOCAL_AMFLAGS=-I ac-macros

BUNDLEPATH=dist/macosx/Subcommander
QTPATH=@QT_LIBPATH@

DISTDIR=subcommander-@PACKAGE_VERSION@
DISTARC=Subcommander-@PACKAGE_VERSION@.tar.gz

## html doc install dir
## todo replace with configure option
DOCDIR_HTML=doc/docbook/html-install


all:
SUBDIRS = util svn sublib submerge subcommander


guide:
	-rm doc/docbook/*.pdf
	-rm doc/docbook/guide/*.fo
	(export XML_CATALOG_FILES=`pwd`/doc/docbook/catalog.xml; \
    cd doc/docbook/guide; \
    xsltproc -o guide.fo --xinclude ../stylesheets/subcommander-fo.xsl guide.xml; \
    xep -fo guide.fo -out ../SubcommanderGuide.pdf; )


guide-html:
	-rm -rf doc/docbook/html
	(export XML_CATALOG_FILES=`pwd`/doc/docbook/catalog.xml; \
    cd doc/docbook/guide; \
    xsltproc -param chunk.first.sections 1 -stringparam base.dir ../html/ \
      --xinclude ../stylesheets/subcommander-html.xsl guide.xml;\
    cp -R images ../html; )

install-html: guide-html
	cp -R doc/docbook/html $(DOCDIR_HTML)


sdist:
	echo "** NOTE **"
	echo "did you create a proper sublib/version.out.h ?"
	echo "if not run gen_version.rb in the versioned(!) source directory."
	-rm -rf ../$(DISTDIR)
	-rm ../$(DISTARC)
	svn export . ../$(DISTDIR)
	cp sublib/version.out.h ../$(DISTDIR)/sublib
	cp dist/macosx/Info.plist.sc ../$(DISTDIR)/dist/macosx
	cp dist/macosx/Info.plist.sm ../$(DISTDIR)/dist/macosx
	cp doc/docbook/guide/version.out.xml ../$(DISTDIR)/doc/docbook/guide
	(cd ../$(DISTDIR); ./gen_ac.sh)
	(cd ..; rm -rf $(DISTDIR)/www)
	(cd ..; tar czf $(DISTARC) $(DISTDIR))  



bundle:
	-rm -rf $(BUNDLEPATH)
	mkdir -p $(BUNDLEPATH)/doc
	cp README $(BUNDLEPATH)
	cp CHANGES $(BUNDLEPATH)
	-cp doc/docbook/SubcommanderGuide.pdf $(BUNDLEPATH)
	cp doc/COPYING $(BUNDLEPATH)/doc
	cp doc/GNU-GPL.txt $(BUNDLEPATH)/doc

	mkdir -p $(BUNDLEPATH)/subcommander.app/Contents/MacOS
	mkdir -p $(BUNDLEPATH)/subcommander.app/Contents/Frameworks
	mkdir -p $(BUNDLEPATH)/subcommander.app/Contents/Resources/icons/default

	cp dist/macosx/Info.plist.sc $(BUNDLEPATH)/subcommander.app/Contents/Info.plist
	cp subcommander/subcommander $(BUNDLEPATH)/subcommander.app/Contents/MacOS
	cp pics/icons/sc.icns $(BUNDLEPATH)/subcommander.app/Contents/Resources
	cp pics/icons/default/*.png $(BUNDLEPATH)/subcommander.app/Contents/Resources/icons/default
	cp pics/icons/default/README $(BUNDLEPATH)/subcommander.app/Contents/Resources/icons/default
	cp pics/icons/default/LICENSE $(BUNDLEPATH)/subcommander.app/Contents/Resources/icons/default

	mkdir -p $(BUNDLEPATH)/submerge.app/Contents/MacOS
	mkdir -p $(BUNDLEPATH)/submerge.app/Contents/Frameworks
	mkdir -p $(BUNDLEPATH)/submerge.app/Contents/Resources/icons/default

	cp dist/macosx/Info.plist.sm $(BUNDLEPATH)/submerge.app/Contents/Info.plist
	cp submerge/submerge $(BUNDLEPATH)/submerge.app/Contents/MacOS
	cp pics/icons/sm.icns $(BUNDLEPATH)/submerge.app/Contents/Resources
	cp pics/icons/default/*.png $(BUNDLEPATH)/submerge.app/Contents/Resources/icons/default
	cp pics/icons/default/README $(BUNDLEPATH)/submerge.app/Contents/Resources/icons/default
	cp pics/icons/default/LICENSE $(BUNDLEPATH)/submerge.app/Contents/Resources/icons/default

## copy shared qt to the bundle and set its library path to the bundle, this needs special
## handling because qt is always linked without full path. No idea how to link with full path.

#	qtlibpath=`otool -L dist/macosx/Subcommander/subcommander.app/Contents/MacOS/subcommander \
  | grep qt-mt | sed "s@[^/]@@" | sed "s@[ ].*@@"`; \
    qtlib=`echo $$qtlibpath | sed "s@.*/@@"`; \
    echo qtlibpath: $$qtlibpath qtlib: $$qtlib; \
    cp $(QTPATH)/$$qtlib $(BUNDLEPATH)/subcommander.app/Contents/Frameworks; \
    install_name_tool -change $$qtlib @executable_path/../Frameworks/$$qtlib \
    $(BUNDLEPATH)/subcommander.app/Contents/MacOS/subcommander; \
    cp $(QTPATH)/$$qtlib $(BUNDLEPATH)/submerge.app/Contents/Frameworks; \
    install_name_tool -change $$qtlib @executable_path/../Frameworks/$$qtlib \
    $(BUNDLEPATH)/submerge.app/Contents/MacOS/submerge;

## subcommander: copy non system shared libraries to bundle.

	for dylib in intl; do \
    libpath=`otool -L dist/macosx/Subcommander/subcommander.app/Contents/MacOS/subcommander \
    | grep $$dylib | sed "s@[^/]@@" | sed "s@[ ].*@@"`; \
    lib=`echo $$libpath | sed "s@.*/@@"`; \
    echo dylib: $$dylib libpath: $$libpath lib: $$lib; \
    cp $$libpath $(BUNDLEPATH)/subcommander.app/Contents/Frameworks; \
    install_name_tool -change $$libpath @executable_path/../Frameworks/$$lib \
    $(BUNDLEPATH)/subcommander.app/Contents/MacOS/subcommander; \
  done

## submerge: copy non system shared libraries to bundle.

	for dylib in intl; do \
    libpath=`otool -L dist/macosx/Subcommander/submerge.app/Contents/MacOS/submerge \
    | grep $$dylib | sed "s@[^/]@@" | sed "s@[ ].*@@"`; \
    lib=`echo $$libpath | sed "s@.*/@@"`; \
    echo dylib: $$dylib libpath: $$libpath lib: $$lib; \
    cp -n $$libpath $(BUNDLEPATH)/submerge.app/Contents/Frameworks; \
    install_name_tool -change $$libpath @executable_path/../Frameworks/$$lib \
    $(BUNDLEPATH)/submerge.app/Contents/MacOS/submerge; \
  done


if BUNDLE_NLS
	for mo in `ls @SVN_PATH@/share/locale`; do \
    mkdir -p $(BUNDLEPATH)/subcommander.app/Contents/Resources/locale/$$mo/LC_MESSAGES; \
    cp @SVN_PATH@/share/locale/$$mo/LC_MESSAGES/subversion.mo \
      $(BUNDLEPATH)/subcommander.app/Contents/Resources/locale/$$mo/LC_MESSAGES; \
      \
    mkdir -p $(BUNDLEPATH)/submerge.app/Contents/Resources/locale/$$mo/LC_MESSAGES; \
    cp @SVN_PATH@/share/locale/$$mo/LC_MESSAGES/subversion.mo \
      $(BUNDLEPATH)/submerge.app/Contents/Resources/locale/$$mo/LC_MESSAGES; \
  done
endif

if ENABLE_NLS
	for pomo in `ls po/*.mo`; do \
    mo=`echo $$pomo | sed s@po/@@ | sed s@[.].*@@`; \
    mkdir -p $(BUNDLEPATH)/subcommander.app/Contents/Resources/locale/$$mo/LC_MESSAGES; \
    cp $$pomo $(BUNDLEPATH)/subcommander.app/Contents/Resources/locale/$$mo/LC_MESSAGES/Subcommander.mo; \
    \
    mkdir -p $(BUNDLEPATH)/submerge.app/Contents/Resources/locale/$$mo/LC_MESSAGES; \
    cp $$pomo $(BUNDLEPATH)/submerge.app/Contents/Resources/locale/$$mo/LC_MESSAGES/Subcommander.mo; \
  done
endif


image:
	strip $(BUNDLEPATH)/subcommander.app/Contents/MacOS/subcommander
	strip $(BUNDLEPATH)/submerge.app/Contents/MacOS/submerge
	-rm dist/macosx/Subcommander-@PACKAGE_VERSION@.dmg
	hdiutil create -srcfolder $(BUNDLEPATH) dist/macosx/Subcommander-@PACKAGE_VERSION@.dmg
