#!/bin/sh

set -e

#
# Creates Mac OS X bundle file. Must be run from $(builddir)/src
#
# Usage: make-bundle <bundleName.app> <poedit_binary> <top_builddir> <top_srcdir>
# 
# $Id$
#

bundle="$1"
poedit_binary="$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_binary $bindir/Poedit

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

    # copy gettext binaries over:
    for f in $gettext_apps ; do
        cp -pPf $GETTEXT_PREFIX/bin/$f $bindir
    done
else
    echo "WARNING: not copying gettext binaries, point GETTEXT_PREFIX" >&2
    echo "         environment variable to a directory that contains them" >&2
fi

# metadata:
cp $top_builddir/macosx/Info.plist $bundle/Contents
cp $top_srcdir/icons/osx/*.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/*/*/wx*.mo) | \
        (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 [ "x$SPARKLE_FRAMEWORK" != "xno" ] ; 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/icons/appicon/128x128/apps/poedit.png $iconsdir
cp $top_srcdir/icons/ui/*.png $iconsdir
cp $top_srcdir/icons/ui/toolbar-32/*.png $iconsdir
# now overwrite with Mac-specific ones:
cp $top_srcdir/icons/ui/macosx/*.png $iconsdir
