#!/bin/sh
##################################################
#             aMule.app bundle creator.          #
##################################################

## This file is part of the aMule Project
##
## Copyright (c) 2004      Angel Vidal (Kry) ( kry@amule.org )
## Copyright (c) 2003-2004 aMule Project     ( http://www.amule-project.net )
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either
## version 2 of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA


echo ""
echo -n "Step 1: Cleaning bundle... "
rm aMule.app/Contents/Frameworks/* aMule.app/Contents/MacOS/* 1> /dev/null 2> /dev/null
rm -r aMule.app/Contents/Resources/webserver 1> /dev/null 2> /dev/null
rm -r aMule.app/Contents/SharedSupport 1> /dev/null 2> /dev/null
echo "Done"
echo ""
echo -n "Step 2: Copying aMule to app bundle... " 
cp src/amule aMule.app/Contents/MacOS/
cp src/webserver/src/amuleweb aMule.app/Contents/MacOS/
cp src/ed2k aMule.app/Contents/MacOS/
cp src/amulecmd aMule.app/Contents/MacOS/
cp -R src/webserver aMule.app/Contents/Resources
find aMule.app/Contents/Resources/webserver \( -name .svn -o -name "Makefile*" -o -name src \) -print0 | xargs -0 rm -rf
echo "Done"
echo ""
echo -n "Step 3: Installing translations to app bundle... "
orig_dir=`pwd`
cd po
make install datadir=$orig_dir/aMule.app/Contents/SharedSupport 1> /dev/null 2> /dev/null
cd $orig_dir
echo "Done"
echo ""
echo "Step 4: Copying libs to Framework"
echo "    wxWidgets..."
# wxWidgets libs to frameworks
for i in $( otool -L	aMule.app/Contents/MacOS/amule \
						aMule.app/Contents/MacOS/amuleweb \
						aMule.app/Contents/MacOS/ed2k \
						aMule.app/Contents/MacOS/amulecmd \
			| sort -u | grep libwx_ | cut -d " " -f 1 ); do 
	cp $i aMule.app/Contents/Frameworks; 
done
echo "Libs copy done."
echo ""
echo "Step 5: Update libs info"
#then install_name_tool on them to fix the path on the shared lib
cd aMule.app/Contents/
for i in $( ls Frameworks | grep -v CVS); do
	echo "    Updating $i"
	#update library id
	install_name_tool -id @executable_path/../Frameworks/$i Frameworks/$i
	#update library links
	for j in $( otool -L Frameworks/$i | grep libwx_ | cut -d " " -f 1 ); do
	        install_name_tool -change \
                $j @executable_path/../Frameworks/`echo $j | rev | cut -d "/" -f 1 | rev` \
		Frameworks/$i 1> /dev/null 2> /dev/null

	done
	echo "    Updating aMule lib info for $i"
	#update amule executable
	install_name_tool -change \
		`otool -L MacOS/amule | grep $i | cut -d " " -f 1` \
		@executable_path/../Frameworks/$i MacOS/amule 1> /dev/null 2> /dev/null
	install_name_tool -change \
		`otool -L MacOS/amuleweb | grep $i | cut -d " " -f 1` \
		@executable_path/../Frameworks/$i MacOS/amuleweb 1> /dev/null 2> /dev/null
	install_name_tool -change \
		`otool -L MacOS/ed2k | grep $i | cut -d " " -f 1` \
		@executable_path/../Frameworks/$i MacOS/ed2k 1> /dev/null 2> /dev/null
	install_name_tool -change \
		`otool -L MacOS/amulecmd | grep $i | cut -d " " -f 1` \
		@executable_path/../Frameworks/$i MacOS/amulecmd 1> /dev/null 2> /dev/null
done
echo "Libs info updated, aMule.app is ready to package."
echo ""
cd ../..
echo -n "Creating aMule.zip... "
zip -9 -r aMule.zip aMule.app/ -j docs/README.Mac.txt > /dev/null
echo "Done"
echo ""
