#! /bin/sh

THEMENAME=Cillop

# Ensure that we can access /opt
sudo chmod 755 /opt

# py2app's modulegraph uses file extensions to determine their nature. So, the fact
# that the advene main script is without extension prevents it from determining it is python code.
# There is a hack in the setup.py file that will use the alternate name advene_gui.py on MacOS X.
# We cannot simply use bin/advene.py, since it would conflict with the advene/ package directory name
# when importing
cp bin/advene bin/advene_gui.py

# Update the version date
./scripts/update_version

Basedir=./dist/Advene.app
Frameworks=${Basedir}/Contents/Frameworks
Resources=${Basedir}/Contents/Resources
MacOS=${Basedir}/Contents/MacOS

rm -rf build dist
/opt/local/bin/python setup.py py2app

# Make sure that all .py files are compiled (esp. advene/plugins/__init__.py)
/opt/local/bin/python -m compileall lib/advene

# Add advene, simpletal and cherrypy, which do not seem to be taken into account automatically. Should investigate this...
(cd lib ; zip -r ../dist/Advene.app/Contents/Resources/lib/python2.6/site-packages.zip  advene  simpletal cherrypy -i \*.pyc )
# Now try to copy the necessary gtk/pango data files (etc, lib)

# Let us put pango and pixbuf loader modules in the same modules directory
mkdir -p ${Frameworks}/modules

echo "Copying all Pango modules ..."
cp -R /opt/local/lib/pango/1.6.0/modules/*.so $Frameworks/modules

echo "Copying GTK modules"
cp -r /opt/local/lib/gtk-2.0 $Resources/lib
# Remove printbackends, there are issues when otool-ing them
rm ${Resources}/lib/gtk-2.0/2.10.0/printbackends/*

# generate new pango.modules file
cat > pangorc <<EOF 
[Pango]
ModulesPath=/opt/local/lib/pango/1.6.0/modules
EOF
env PANGO_RC_FILE=pangorc /opt/local/bin/pango-querymodules | sed "s?/opt/local/lib/pango/1.6.0/modules/?@executable_path/../Frameworks/modules/?" > $Resources/pango.modules.in
rm pangorc

# Generate a pangorc pointing to the relocated directory
cat > $Resources/pangorc.in <<EOF 
[Pango]
ModuleFiles=@userconf/pango.modules
ModulesPath=@executable_path/../Frameworks/modules
EOF

# Copy the fonts.conf file
cp /opt/local/etc/fonts/fonts.conf $Resources/fonts.conf

# Remove any reference to Verdana, since it makes Pango unusable
perl -pi -e 's/.*verdana.*//i' $Resources/fonts.conf

# Copy pixbuf loaders to gtk 2.10 dir
cp -r /opt/local/lib/gdk-pixbuf-2.0/2.10.0/loaders ${Resources}/lib/gtk-2.0/2.10.0

# generate a new GDK pixbufs loaders file
sed "s?/opt/local/lib/gdk-pixbuf-2.0/2.10.0/loaders/?@executable_path/../Resources/lib/gtk-2.0/2.10.0/loaders/?" < /opt/local/etc/gtk-2.0/gdk-pixbuf.loaders > $Resources/gdk-pixbuf.loaders.in

# Copy the theme
[ ! -z "$THEMENAME" ] && cp -r mac/${THEMENAME} ${Resources}/theme

# Some libraries (pixbufloader-gif, pixbufloader-xpm, svg_loader) have too large dependencies. 
# Use a workaround to shorten @executable_path/../Frameworks 
ln -s ../Frameworks ${MacOS}/f

# RSVG hack: include librsvg-2.2.dylib and its dependencies (last time I checked 2007/12/17, there was no need of recursively find dependencies from the dependencies)
cp /opt/local/lib/librsvg-2.2.dylib ${MacOS}/f
otool -L ${MacOS}/f/librsvg-2.2.dylib  | perl -lne 'print $1 if m!/opt/local/lib/(.+?) !' | while read a ; do if [ ! -e ${MacOS}/f/$a ]; then cp /opt/local/lib/$a ${MacOS}/f ; chmod 755 ${MacOS}/f/$a ; fi ; done


# Copy gtk python modules. This will override the py2app way (creating a _gtk.pyc wrapper in the site-packages.zip, which tries to load _gtk.so from lib-dynload, but it crashes)
#cp -r /opt/local/lib/python2.6/site-packages/gtk-2.0/* $Resources/lib/python2.6
# FIXME: now in /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/gtk-2.0/
cp -r  /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/gtk-2.0/* $Resources/lib/python2.6


# Copy gstreamer libs
cp /opt/local/lib/libgst*-0.10.0.dylib ${Frameworks}

# Copy gstreamer plugins and dependencies
( for n in `cat mac/gst-plugins.txt` 
do
   echo "gst plugin $n" >&2
   cp -r /opt/local/lib/gstreamer-0.10/${p} ${Resources}/gst-plugins
   otool -L /opt/local/lib/gstreamer-0.10/${n} | perl -lne 'print $1 if m!/opt/local/lib/(.+?\.dylib)!'
done) | sort -u | ( while read a
do
  echo "Copying $a dep" 
  cp /opt/local/lib/${a} ${Frameworks}
done )

echo "Fixing library names ..."
# fix the libraries we include
for dylib in $Frameworks/*.dylib $Frameworks/modules/*.so $Resources/lib/gtk-2.0/2.10.0/*/*.so $Resources/lib/python2.6/*.so $Resources/lib/python2.6/*/*.so ${Resources}/gst-plugins/*.so
#for dylib in $Frameworks/*.dylib $Frameworks/modules/*.so $Resources/lib/gtk-2.0/2.10.0/*/*.so $Resources/lib/python2.6/*/*.so ${Resources}/gst-plugins/*.so
do
    # skip symlinks
    if test ! -L $dylib ; then
	
	# change all the dependencies

	changes=""
	for lib in `otool -L $dylib | egrep "(/opt/local|/local/|libs/)" | awk '{print $1}'` ; do
	    base=`basename $lib`
	    #changes="$changes -change $lib @executable_path/../Frameworks/$base"
	    changes="$changes -change $lib @executable_path/f/$base"
	done

	if test "x$changes" != x ; then
	    if  install_name_tool $changes $dylib ; then
		:
	    else
                echo "Error for $dylib"
		#exit 1
	    fi
	fi

	# now the change what the library thinks its own name is

	base=`basename $dylib`
	#install_name_tool -id @executable_path/../Frameworks/$base $dylib
	install_name_tool -id @executable_path/f/$base $dylib
    fi
done

# Copy french gtk2.0 locale
cp -r /opt/local/share/locale/fr/LC_MESSAGES/*.mo ${Resources}/locale/fr/LC_MESSAGES

# Copy the x11 vlc output module
# FIXME: to update
##cp mac/libx11_plugin.dylib ${Resources}

# Fix shotdetect permissions
chmod 755 ${Resources}/share/shotdetect

# Use the shell wrapper
mv ${MacOS}/Advene ${MacOS}/Advene.bin
cp mac/Advene.wrapper ${MacOS}/Advene
chmod 755 ${MacOS}/Advene


# Hide the /opt directory, to make sure that we do not reuse something from there inadvertently
sudo chmod 000 /opt

