#!/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$
#

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


# this function returns *non-system* shared libs a binary depends on
get_private_shared_deps()
{
    # ignore any shared libs in /usr/lib* or /System
    otool -X -L $1 | cut -d" " -f1 | cut -c2- | grep -v '^\(/usr/lib\|/System\)'
}

prepend_path()
{
    path=$1
    shift
    for f in $* ; do echo $path/$f; done
}


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

bindir="$bundle/Contents/MacOS"

# executables:
cp poedit $bindir/Poedit
$rez $bindir/Poedit

if [ -n "$GETTEXT_PREFIX" ] ; then
    gettext_apps="msgfmt msgmerge msgunfmt msgcat xgettext"

    all_dylibs=""

    # copy gettext binaries over:
    for f in $gettext_apps ; do
        cp -pPf $GETTEXT_PREFIX/bin/$f $bindir

        # now copy any gettext shared libs they need and update their install paths
        # so that they are usable from the bundle:
        for dylib_full in `get_private_shared_deps $bindir/$f` ; do
            all_dylibs="$all_dylibs $dylib_full"
            dylib=`basename $dylib_full`
            if [ ! -f $bindir/$dylib -a -f $GETTEXT_PREFIX/lib/$dylib ] ; then
                cp -pPf $GETTEXT_PREFIX/lib/$dylib $bindir
                install_name_tool -id $dylib $bindir/$dylib
            fi
        done
    done
    
    # now update shared libs references to point to bundle's location so that they
    # are correctly resolved at runtime:
    for dylib in $all_dylibs ; do
        for f in `prepend_path $bindir $gettext_apps` $bindir/*.dylib ; do
            install_name_tool -change $dylib "@executable_path/`basename $dylib`" $f
        done
    done
else
    echo "WARNING: not copying gettext binaries, point GETTEXT_PREFIX" >&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
    dsymutil $bindir/Poedit
    tar -cjf $bundle.dSYM.tar.bz2 $bundle/Contents/MacOS/Poedit.dSYM
    rm -rf $bundle/Contents/MacOS/Poedit.dSYM
    strip -u -r $bindir/Poedit
else
    echo "WARNING: Xcode 2.4+ is needed to extract debug information" >&2
fi

# metadata:
cp $top_builddir/macosx/Info.plist $bundle/Contents
cp $top_srcdir/macosx/*.icns $bundle/Contents/Resources
cp $top_srcdir/macosx/dsa_pub.pem $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
