#!/bin/sh

#
# Creates Mac OS X bundle file. Must be run from $(builddir)/src
#
# Usage: make-bundle <bundleName.app> <rez> <top_builddir> <top_srcdir>
# 
# $Id: make-bundle 1201 2007-11-18 11:59:50Z vaclavslavik $
#

bundle="$1"
rez="$2"
top_builddir="$3"
top_srcdir="$4"

rm -rf $bundle
mkdir -p $bundle/Contents/MacOS
mkdir -p $bundle/Contents/Resources

# executables:
cp poedit $bundle/Contents/MacOS/Poedit
$rez $bundle/Contents/MacOS/Poedit

if [ -n "$GETTEXT_BINARIES" ] ; then
    for f in msgfmt msgmerge msgunfmt msgcat xgettext ; do
        cp -pPf $GETTEXT_BINARIES/$f $bundle/Contents/MacOS
    done
else
    echo "WARNING: not copying gettext binaries, point GETTEXT_BINARIES" >&2
    echo "         environment variable to a directory that contains them" >&2
fi

# extract (DWARF) debug information into separate dSYM files and pack them into
# an archive:
if [ -f /usr/bin/dsymutil ] ; then
    for f in $bundle/Contents/MacOS/* ; do
        dsymutil $f
    done
    tar -cjf $bundle.dSYM.tar.bz2 $bundle/Contents/MacOS/*.dSYM
    rm -rf $bundle/Contents/MacOS/*.dSYM
else
    echo "WARNING: Xcode 2.4+ is needed to extract debug information" >&2
fi

# we can strip the binaries now:
strip $bundle/Contents/MacOS/*

# metadata:
cp $top_builddir/macosx/Info.plist $bundle/Contents
cp $top_srcdir/macosx/*.icns $bundle/Contents/Resources

# gettext catalogs:
for i in $top_srcdir/locales/*.mo ; do
    lang=`basename $i .mo`
    mkdir -p $bundle/Contents/Resources/locale/$lang/LC_MESSAGES
    cp $i $bundle/Contents/Resources/locale/$lang/LC_MESSAGES/poedit.mo
done

if [ -n "$WX_ROOT" ] ; then
    (cd $WX_ROOT/share ; tar -c locale) | \
        (cd $bundle/Contents/Resources ; tar -x)
else
    echo "WARNING: not copying wxWidgets message catalogs, point WX_ROOT" >&2
    echo "         environment variable to a directory with installed wx" >&2
fi

# 3rd party frameworks:
if [ -n "$SPARKLE_FRAMEWORK" ] ; then
    mkdir -p $bundle/Contents/Frameworks
    cp -R $SPARKLE_FRAMEWORK $bundle/Contents/Frameworks
else
    echo "WARNING: not copying Sparkle framework, point SPARKLE_FRAMEWORK" >&2
    echo "         environment variable to it" >&2
fi

echo "Installed message catalogs:"
find $bundle/Contents/Resources/locale -type f

# icons:
iconsdir="$bundle/Contents/Resources/icons"
mkdir -p $iconsdir
cp $top_srcdir/src/icons/*.png $iconsdir
