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

# Just a stupid "mod" of original script...

THE_BASE_NAME=aMuleAdunanzA

## This file is part of the aMule Project
##
## Copyright (c) 2004-2009 Angel Vidal (Kry) ( kry@amule.org )
## Copyright (c) 2003-2009 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


/bin/echo ""
/bin/echo -n "Step 1: Cleaning bundle... "

# fixed in SVN but not in 2.2.x
if [ -z "${VERSION#2.2.[12345]}" ]; then
	ln -sn ../Resources/webserver $THE_BASE_NAME.app/Contents/SharedSupport/webserver 2> /dev/null
fi
# if you have MacOS dir in your PATH, man automatically search here (AUTOPATH)
# at least on Tiger
ln -sn SharedSupport/man $THE_BASE_NAME.app/Contents/man 2> /dev/null
rm $THE_BASE_NAME.app/Contents/Frameworks/libwx_* $THE_BASE_NAME.app/Contents/MacOS/* 1> /dev/null 2> /dev/null
rm -r $THE_BASE_NAME.app/Contents/Resources/webserver 1> /dev/null 2> /dev/null
rm -r $THE_BASE_NAME.app/Contents/SharedSupport/man 1> /dev/null 2> /dev/null
rm -r $THE_BASE_NAME.app/Contents/SharedSupport 1> /dev/null 2> /dev/null
/bin/echo "Done"
/bin/echo ""
/bin/echo -n "Step 2: Copying $THE_BASE_NAME to app bundle... " 
if [ -f src/amule ]; then
	cp src/amule $THE_BASE_NAME.app/Contents/MacOS/
fi

# per skin, da vedere meglio
mkdir -p $THE_BASE_NAME.app/Contents/SharedSupport/skins
cp src/skins/*.zip $THE_BASE_NAME.app/Contents/SharedSupport/skins/.

if [ -f src/webserver/src/amuleweb ]; then
	cp src/webserver/src/amuleweb $THE_BASE_NAME.app/Contents/MacOS/
fi

if [ -f src/ed2k ]; then
	cp src/ed2k $THE_BASE_NAME.app/Contents/MacOS/
fi

if [ -f src/amuled ]; then
	cp src/amulecmd $THE_BASE_NAME.app/Contents/MacOS/
fi

cp -R src/webserver $THE_BASE_NAME.app/Contents/Resources
orig_dir="`pwd`"
(cd docs/man && make install prefix="$orig_dir/$THE_BASE_NAME.app/Contents/SharedSupport") 1> /dev/null 2> /dev/null
find $THE_BASE_NAME.app/Contents/Resources/webserver \( -name .svn -o -name "Makefile*" -o -name src \) -print0 | xargs -0 rm -rf
/bin/echo "Done"
/bin/echo ""
/bin/echo -n "Step 3: Installing translations to app bundle... "
orig_dir=`pwd`
cd po
make install datadir=$orig_dir/$THE_BASE_NAME.app/Contents/SharedSupport 1> /dev/null 2> /dev/null
cd $orig_dir
/bin/echo "Done"
/bin/echo ""
/bin/echo "Step 4: Copying libs to Framework"
/bin/echo "    wxWidgets..."
# wxWidgets libs to frameworks
APPSTOCHECK="$THE_BASE_NAME.app/Contents/MacOS/amule"
if [ -f $THE_BASE_NAME.app/Contents/MacOS/amuleweb ]; then
	APPSTOCHECK=$APPSTOCHECK + " $THE_BASE_NAME.app/Contents/MacOS/amuleweb"
fi
if [ -f $THE_BASE_NAME.app/Contents/MacOS/ed2k ]; then
	APPSTOCHECK="$APPSTOCHECK $THE_BASE_NAME.app/Contents/MacOS/ed2k"
fi
if [ -f  $THE_BASE_NAME.app/Contents/MacOS/amulecmd ]; then
	APPSTOCHECK="$APPSTOCHECK $THE_BASE_NAME.app/Contents/MacOS/amulecmd"
fi   
# for i in $( otool -L	$THE_BASE_NAME.app/Contents/MacOS/amule \
# 						$THE_BASE_NAME.app/Contents/MacOS/amuleweb \
# 						$THE_BASE_NAME.app/Contents/MacOS/ed2k \
# 						$THE_BASE_NAME.app/Contents/MacOS/amulecmd \
# 			| sort -u | grep -E 'libwx_|/opt/local|/usr/local' | cut -d " " -f 1 ); do 
# 	cp $i $THE_BASE_NAME.app/Contents/Frameworks; 
# done
for i in $( otool -L	$APPSTOCHECK \
			| sort -u | grep -E 'libwx_|/opt/local|/usr/local|libGeoIP' | cut -d " " -f 1 ); do 
	cp $i $THE_BASE_NAME.app/Contents/Frameworks; 
done
# Some MacPorts libraries have not user write permission
chmod 644 $THE_BASE_NAME.app/Contents/Frameworks/*
/bin/echo "Libs copy done."
/bin/echo ""
/bin/echo "Step 5: Update libs info"
#then install_name_tool on them to fix the path on the shared lib
cd $THE_BASE_NAME.app/Contents/
for i in $( ls Frameworks | grep -v CVS); do
	/bin/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 -E 'libwx_|/opt/local|/usr/local|libGeoIP' | 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
	/bin/echo "    Updating $THE_BASE_NAME 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
	if [ -f MacOS/amuleweb ]; then
		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
	fi
	if [ -f MacOS/ed2k ]; then
		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
	fi
	if [ -f MacOS/amulecmd ]; then
		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
	fi
done

/bin/echo "Libs info updated, $THE_BASE_NAME.app is ready to package."
/bin/echo ""
cd ../..

NAME_OF_ZIP="${THE_BASE_NAME}-2010.1"
if [ -d .svn ]; then
	THE_SVNVERSION=`svnversion . | sed -e 's/[0-9][0-9]*:\(.*\)/\1/' -e 's/\([0-9][0-9]*\).*/\1/'`
	NAME_OF_ZIP+="-rev${THE_SVNVERSION}"
fi
NAME_OF_ZIP+="-univbin"

# Backup previous zip, if any
if [ -f ${NAME_OF_ZIP}.zip ]; then
	THE_TIMESTAMP=`/bin/date "+%Y%m%d_%H%M%S"`
	# THE_TIMESTAMP=`/bin/date`
	BACKUPNAME=${NAME_OF_ZIP}_${THE_TIMESTAMP}
	/bin/echo -n "Archiving previous zip as ${BACKUPNAME}.zip... "
	mv -f "${NAME_OF_ZIP}.zip" "${BACKUPNAME}.zip"
	/bin/echo "Done"
fi

/bin/echo -n "Creating ${NAME_OF_ZIP}.zip... "
zip -9 -r ${NAME_OF_ZIP}.zip $THE_BASE_NAME.app/ Changelog.AdunanzA LEGGIMI.AdunanzA docs/README.Mac.txt docs/COPYING > /dev/null
/bin/echo "Done"
/bin/echo ""
