1#!/bin/bash
2##################################################
3#             aMule.app bundle creator.          #
4##################################################
5
6## This file is part of the aMule Project
7##
8## Copyright (c) 2011 Angel Vidal ( kry@amule.org )
9##
10## This program is free software; you can redistribute it and/or
11## modify it under the terms of the GNU General Public License
12## as published by the Free Software Foundation; either
13## version 2 of the License, or (at your option) any later version.
14##
15## This program is distributed in the hope that it will be useful,
16## but WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with this program; if not, write to the Free Software
22## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
23
24SCRIPTDIR=`dirname "$0"`
25SCRIPTNAME=`basename "$0"`
26
27## Get full path
28SCRIPTDIR=`cd $SCRIPTDIR; pwd`
29
30PATH="$SCRIPTDIR:$PATH"
31
32. defs-global.sh $1
33. defs-wx.sh
34. defs-functions.sh
35
36echo "Start" > $STDOUT_FILE
37echo "Start" > $ERROR_FILE
38
39REPEATSCRIPT=${ROOT_FOLDER}/repeat.sh
40if [ -f $REPEATSCRIPT ]; then
41	chmod 600 $REPEATSCRIPT
42fi
43echo "Save configuration commandline to ${REPEATSCRIPT} - execute that script to repeat this compilation with the same options."
44echo "#!/bin/bash" > $REPEATSCRIPT
45echo "echo \"Starting repeat, moving away current repeat.sh\"" >> $REPEATSCRIPT
46echo "rm -rf \${0}.old 2>/dev/null; mv \$0 \${0}.old" >> $REPEATSCRIPT
47if [ x"$SDKNUMBER" == x"" ]; then
48	SDKSTRING="default"
49else
50	SDKSTRING=$SDKNUMBER
51fi
52
53echo "BUILDARCHS=\"${BUILDARCHS}\" SDKNUMBER=\"${SDKSTRING}\" UNIVERSAL=\"${UNIVERSAL}\" WXVERSION=\"${WXVERSION}\" WXPORT=\"${WXPORT}\" SLIMWX=\"${SLIMWX}\" BUILD_FOLDER=\"${BUILD_FOLDER}\" $0" >> $REPEATSCRIPT
54echo "echo \"Repeat finished\"" >> $REPEATSCRIPT
55chmod 500 $REPEATSCRIPT
56
57echo "Starting build..."
58
59echo -e "\tGetting aMule sources..."
60
61#Get aMule first, because it may contain patches
62if [ -d $AMULE_FOLDER/ ]; then
63	if [ -d ${AMULE_FOLDER}/.svn/ ]; then
64		echo -e "\t\tSources already exist, updating."
65	        pushd $AMULE_FOLDER/ >> $STDOUT_FILE
66	        svn up >> $STDOUT_FILE
67	        popd >> $STDOUT_FILE
68	else
69		echo -e "\t\taMule sources at \"" $AMULE_FOLDER "\" are not from SVN checkout, so not updating."
70	fi
71else
72	echo -e "\tFirst checkout."
73	if [ "$SVN_REPOSITORY" == "public" ]; then
74		SVN_REPOSITORY=http://amule.googlecode.com/svn/trunk/
75		echo -e "\tUsing public SVN repository at ${SVN_REPOSITORY}."
76	else
77		echo -e "\tUsing provided SVN repository at ${SVN_REPOSITORY}."
78	fi
79
80	svn co $SVN_REPOSITORY $AMULE_FOLDER >> $STDOUT_FILE
81	if [ ! -d $AMULE_FOLDER/ ]; then
82		echo "ERROR: aMule sources could not be retrieved. Review your settings."
83		exit
84	fi
85fi
86
87pushd $ROOT_FOLDER >> $STDOUT_FILE
88
89echo -e "\tDone"
90
91echo -e "\tGetting wxWidgets sources..."
92
93if [ -d ${WXFOLDER} ]; then
94	pushd ${WXFOLDER} >> $STDOUT_FILE
95	svn up >> $STDOUT_FILE 2>> $ERROR_FILE
96	popd >> $STDOUT_FILE
97else
98	echo -e "\tFirst checkout."
99	svn checkout http://svn.wxwidgets.org/svn/wx/wxWidgets/${WXSVNROOT} ${WXFOLDER} >> $STDOUT_FILE 2>> $ERROR_FILE
100	pushd ${WXFOLDER} >> $STDOUT_FILE
101	echo -e "\tApplying patches."
102	for i in $AMULE_FOLDER/src/utils/patches/wxWidgets/*.patch; do 
103		echo -e "\t\tAppying \"$i\""
104		patch -p0 < $i >> $STDOUT_FILE 2>> $ERROR_FILE
105	done
106	popd >> $STDOUT_FILE
107fi	
108echo -e "\tDone"
109
110echo -e "\tConfiguring wxWidgets..."
111
112pushd ${WXFOLDER} > $STDOUT_FILE
113
114if [ "$SDKNUMBER" == "" ]; then
115	WX_SDK_FLAGS=""	
116else
117	WX_SDK_FLAGS="--with-macosx-sdk=/Developer/SDKs/MacOSX${SDKRELEASE}.sdk \
118	--with-macosx-version-min=$SDKNUMBER"
119fi
120
121if [ -e amulewxcompilation ]; then
122	echo -e "\t\twxWidgets is already configured"
123else
124	make clean >> $STDOUT_FILE 2>/dev/null
125	./configure CC=gcc$CCVERSION CXX=g++$CCVERSION CPP=cpp$CCVERSION LD=g++$CCVERSION \
126	CFLAGS="$CFLAGS $ARCHCPPFLAGS" CXXFLAGS="$CXXFLAGS $ARCHCPPFLAGS" CPPFLAGS="$CPPFLAGS $ARCHCPPFLAGS" \
127	LDFLAGS="$LDFLAGS $ARCHCPPFLAGS" \
128	OBJCFLAGS="$OBJCFLAGS $ARCHCPPFLAGS" OBJCXXFLAGS="$OBJCXXFLAGS $ARCHCPPFLAGS" \
129	--enable-debug --disable-shared \
130	$EXTRA_WXFLAGS \
131	$ARCHCONFIGFLAGS \
132	$WX_SDK_FLAGS >> $STDOUT_FILE 2>> $ERROR_FILE
133	touch amulewxcompilation >> $STDOUT_FILE
134	echo -e "\t\tConfigured."
135fi
136
137echo -e "\t\tDone"
138
139echo -e "\tCompiling wxWidgets..."
140
141make >> $STDOUT_FILE 2>> $ERROR_FILE
142
143echo -e "\tDone"
144
145popd >> $STDOUT_FILE
146
147CRYPTOPP_FOLDER="cryptopp-source"
148CRYPTOPP_FOLDER_INST="cryptopp"
149CRYPTOPP_URL=`curl -sS http://www.cryptopp.com/ | grep -oE "http://.*/cryptopp/cryptopp[0-9]+\.zip" | sort -r | head -1`
150
151echo -e "\tGetting cryptopp sources..."
152
153if [ -d $CRYPTOPP_FOLDER_INST ]; then
154	echo -e "\t\t$CRYPTOPP_FOLDER_INST already exists, skipping (delete and rerun script to get new sources)"	
155else
156	mkdir $CRYPTOPP_FOLDER
157	mkdir $CRYPTOPP_FOLDER_INST
158	curl -L -o cryptopp.zip $CRYPTOPP_URL >> $STDOUT_FILE 2>> $ERROR_FILE
159	unzip cryptopp.zip -d $CRYPTOPP_FOLDER >> $STDOUT_FILE 2>> $ERROR_FILE
160	pushd $CRYPTOPP_FOLDER >> $STDOUT_FILE 
161	#./configure 
162	for i in $AMULE_FOLDER/src/utils/patches/cryptopp/*.patch; do 
163		echo -e "\t\tAppying \"$i\"" 
164		patch -p0 < $i >> $STDOUT_FILE 2>> $ERROR_FILE
165	done
166	#cp ../GNUMakefile .
167	echo -e "\t\tCompiling cryptopp..."
168	CC=gcc$CCVERSION CXX=g++$CCVERSION CPP=cpp$CCVERSION LD=g++$CCVERSION \
169		CXXFLAGS="-pthread $ARCHCPPFLAGS $SDK" CFLAGS="-pthread $ARCHCPPFLAGS $SDK" LDFLAGS="-pthread $SDK" make > $STDOUT_FILE 2> $ERROR_FILE
170	PREFIX=${ROOT_FOLDER}/$CRYPTOPP_FOLDER_INST make install >> $STDOUT_FILE 2>> $ERROR_FILE
171	popd >> $STDOUT_FILE 
172	rm cryptopp.zip >> $STDOUT_FILE 2>> $ERROR_FILE
173	rm -rf $CRYPTOPP_FOLDER >> $STDOUT_FILE 2>> $ERROR_FILE
174fi
175
176echo -e "\tDone"
177
178# Gettext
179
180echo -e "\tGetting gettext sources..."
181
182GETTEXT_FOLDER="gettext-source"
183GETTEXT_FOLDER_INST="gettext-inst"
184GETTEXT_URL=`curl -sS http://www.gnu.org/software/gettext/ | grep -m 1 -oE "http://[^\"]+/gettext-([0-9]+\.)+tar.gz" | head -1`
185
186if [ -d $GETTEXT_FOLDER_INST ]; then
187	echo -e "\t\t$GETTEXT_FOLDER_INST already exists, skipping"
188else
189	mkdir $GETTEXT_FOLDER
190	mkdir $GETTEXT_FOLDER_INST
191	curl -L -o gettext.tar.gz $GETTEXT_URL >> $STDOUT_FILE 2>> $ERROR_FILE
192	pushd $GETTEXT_FOLDER >> $STDOUT_FILE 2>> $ERROR_FILE
193	echo -e "\t\tCompiling gettext..."
194	tar --strip-components 1 -zxf ../gettext.tar.gz >> $STDOUT_FILE 2>> $ERROR_FILE
195	./configure CC=gcc$CCVERSION CXX=g++$CCVERSION CPP=cpp$CCVERSION LD=g++$CCVERSION CXXFLAGS="-pthread $ARCHCPPFLAGS $SDK" \
196		CFLAGS="-pthread $ARCHCPPFLAGS $SDK" LDFLAGS="-pthread $SDK" \
197		--disable-debug --disable-shared --prefix=${ROOT_FOLDER}/$GETTEXT_FOLDER_INST >> $STDOUT_FILE 2>> $ERROR_FILE
198	make >> $STDOUT_FILE 2>> $ERROR_FILE
199	make install >> $STDOUT_FILE 2>> $ERROR_FILE
200	popd >> $STDOUT_FILE 2>> $ERROR_FILE
201	rm gettext.tar.gz >> $STDOUT_FILE 2>> $ERROR_FILE
202	rm -rf $GETTEXT_FOLDER >> $STDOUT_FILE 2>> $ERROR_FILE
203fi
204
205echo -e "\tDone."
206
207#libupnp
208
209echo -e "\tGetting libupnp sources..."
210
211LIBUPNP_FOLDER="libupnp-source"
212LIBUPNP_FOLDER_INST="libupnp-inst"
213LIBUPNP_URL=`curl -sS http://sourceforge.net/projects/pupnp/files/ | grep -m 1 -ioE "http://sourceforge.net/[^\"]+/libupnp-([0-9]+\.)+tar.bz2/download" | head -1`
214
215if [ -d $LIBUPNP_FOLDER_INST ]; then
216	echo -e "\t\t$LIBUPNP_FOLDER_INST already exists, skipping"	
217else
218	mkdir $LIBUPNP_FOLDER >> $STDOUT_FILE 2>> $ERROR_FILE
219	mkdir $LIBUPNP_FOLDER_INST >> $STDOUT_FILE 2>> $ERROR_FILE
220	curl -L -o libupnp.tar.bz2 $LIBUPNP_URL  >> $STDOUT_FILE 2>> $ERROR_FILE
221	pushd $LIBUPNP_FOLDER >> $STDOUT_FILE 2>> $ERROR_FILE
222	echo -e "\t\tCompiling libupnp..."	
223	tar --strip-components 1 -jxf ../libupnp.tar.bz2 >> $STDOUT_FILE 2>> $ERROR_FILE
224	./configure CC=gcc$CCVERSION CXX=g++$CCVERSION CPP=cpp$CCVERSION LD=g++$CCVERSION CXXFLAGS="-pthread $ARCHCPPFLAGS $SDK" \
225		CFLAGS="-pthread $ARCHCPPFLAGS $SDK" LDFLAGS="-pthread $SDK" --disable-dependency-tracking \
226		--disable-debug --disable-shared --prefix=${ROOT_FOLDER}/$LIBUPNP_FOLDER_INST >> $STDOUT_FILE 2>> $ERROR_FILE
227	make >> $STDOUT_FILE 2>> $ERROR_FILE
228	make install >> $STDOUT_FILE 2>> $ERROR_FILE
229	popd >> $STDOUT_FILE 2>> $ERROR_FILE
230	rm libupnp.tar.bz2 >> $STDOUT_FILE 2>> $ERROR_FILE
231	rm -rf $LIBUPNP_FOLDER >> $STDOUT_FILE 2>> $ERROR_FILE
232fi
233
234echo -e "\tDone."
235
236#geoip
237
238echo -e "\tGetting GeoIP sources..."
239
240LIBGEOIP_FOLDER="libgeoip-source"
241LIBGEOIP_FOLDER_INST="libgeoip-inst"
242LIBGEOIP_URL="http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz"
243
244if [ -d $LIBGEOIP_FOLDER_INST ]; then
245	echo -e "\t\t$LIBGEOIP_FOLDER_INST already exists, skipping"	
246else
247	mkdir $LIBGEOIP_FOLDER >> $STDOUT_FILE 2>> $ERROR_FILE
248	mkdir $LIBGEOIP_FOLDER_INST >> $STDOUT_FILE 2>> $ERROR_FILE
249	curl -L -o libgeoip.tar.gz $LIBGEOIP_URL >> $STDOUT_FILE 2>> $ERROR_FILE
250	pushd $LIBGEOIP_FOLDER >> $STDOUT_FILE 2>> $ERROR_FILE
251	echo -e "\t\tCompiling GeoIP..." 
252	tar --strip-components 2 -zxf ../libgeoip.tar.gz >> $STDOUT_FILE 2>> $ERROR_FILE
253	./configure CC=gcc$CCVERSION CXX=g++$CCVERSION CPP=cpp$CCVERSION LD=g++$CCVERSION CXXFLAGS="-pthread $ARCHCPPFLAGS $SDK" \
254		CFLAGS="-pthread $ARCHCPPFLAGS $SDK" LDFLAGS="-pthread $SDK" --disable-dependency-tracking \
255		--disable-debug --disable-shared --prefix=${ROOT_FOLDER}/$LIBGEOIP_FOLDER_INST >> $STDOUT_FILE 2>> $ERROR_FILE
256	make >> $STDOUT_FILE 2>> $ERROR_FILE
257	make install >> $STDOUT_FILE 2>> $ERROR_FILE
258	popd >> $STDOUT_FILE 2>> $ERROR_FILE
259	rm libgeoip.tar.gz >> $STDOUT_FILE 2>> $ERROR_FILE
260	rm -rf $LIBGEOIP_FOLDER >> $STDOUT_FILE 2>> $ERROR_FILE
261fi
262
263echo -e "\tDone."
264
265#pkg-config
266
267echo -e "\tGetting pkg-config sources..."
268
269PKGCFG_FOLDER="pkgcfg-source"
270PKGCFG_FOLDER_INST="pkgcfg-inst"
271# pkgconfig introduced a dependency on glib to build on 0.26, and I refuse to build the whole glib for this. 
272# On top of it, glib uses pkgconfig to configure itself... 
273#PKGCFG_FILE=`curl -sS http://pkgconfig.freedesktop.org/releases/ | grep -ioE "pkg-config-([0-9]+\.)+tar.gz" | uniq | sort -r | head -1`
274#PKGCFG_URL="http://pkgconfig.freedesktop.org/releases/${PKGCFG_FILE}"
275PKGCFG_URL="http://pkgconfig.freedesktop.org/releases/pkg-config-0.25.tar.gz"
276
277if [ -f $PKGCFG_FOLDER_INST/bin/pkg-config ]; then
278	echo -e "\t\t$PKGCFG_FOLDER_INST already exists, skipping"	
279else
280	mkdir $PKGCFG_FOLDER >> $STDOUT_FILE 2>> $ERROR_FILE
281	mkdir $PKGCFG_FOLDER_INST >> $STDOUT_FILE 2>> $ERROR_FILE
282	curl -L -o pkgcfg.tar.gz $PKGCFG_URL >> $STDOUT_FILE 2>> $ERROR_FILE
283	pushd $PKGCFG_FOLDER >> $STDOUT_FILE 2>> $ERROR_FILE
284	echo -e "\t\tCompiling pkg-config..."	
285	tar --strip-components 1 -zxf ../pkgcfg.tar.gz >> $STDOUT_FILE 2>> $ERROR_FILE
286	./configure CC=gcc$CCVERSION CXX=g++$CCVERSION CPP=cpp$CCVERSION LD=g++$CCVERSION CXXFLAGS="-pthread $ARCHCPPFLAGS $SDK" \
287		CFLAGS="-pthread $ARCHCPPFLAGS $SDK" LDFLAGS="-pthread $SDK" --disable-dependency-tracking \
288		--disable-debug --disable-shared --prefix=${ROOT_FOLDER}/$PKGCFG_FOLDER_INST >> $STDOUT_FILE 2>> $ERROR_FILE
289	make >> $STDOUT_FILE 2>> $ERROR_FILE
290	make install >> $STDOUT_FILE 2>> $ERROR_FILE
291	popd >> $STDOUT_FILE 2>> $ERROR_FILE
292	if [ -f $PKGCFG_FOLDER_INST/bin/pkg-config ]; then
293		rm -f pkgcfg.tar.gz >> $STDOUT_FILE 2>> $ERROR_FILE
294		rm -rf $PKGCFG_FOLDER >> $STDOUT_FILE 2>> $ERROR_FILE
295	else
296		echo -e " ERROR: check output $STDOUT_FILE and $ERROR_FILE for details. "
297	fi
298fi
299
300
301echo -e "\tDone."
302
303# aMule
304
305echo -e "\tFINALLY compiling aMule..."
306
307pushd $AMULE_FOLDER >> $STDOUT_FILE 2>> $ERROR_FILE
308
309if [ -f configure ]; then
310	echo -e "\t\tConfigure already exists"
311else
312	PATH="${PATH}:${ROOT_FOLDER}/${GETTEXT_FOLDER_INST}/bin" ./autogen.sh >> $STDOUT_FILE 2>> $ERROR_FILE
313fi
314
315if [ -d intl/ ]; then
316	echo -e "\t\tGood: intl folder already exists."
317else
318	mkdir intl >> $STDOUT_FILE 2>> $ERROR_FILE
319	touch intl/Makefile.in >> $STDOUT_FILE 2>> $ERROR_FILE
320	echo "all:" > intl/Makefile >> $STDOUT_FILE 2>> $ERROR_FILE
321	echo "clean:" >> intl/Makefile >> $STDOUT_FILE 2>> $ERROR_FILE
322	echo "" >> intl/Makefile >> $STDOUT_FILE 2>> $ERROR_FILE
323fi
324
325if [ ! -f src/amule ]; then
326	MULECLEAN=YES
327fi
328
329rm -rf ${ROOT_FOLDER}/amule-inst/
330mkdir -p ${ROOT_FOLDER}/amule-inst/
331
332if [ "$MULECLEAN" == "YES" ]; then
333	echo -e "\t\tRunning configure"
334
335	PATH="${PATH}:${ROOT_FOLDER}/${GETTEXT_FOLDER_INST}/bin/:${ROOT_FOLDER}/${PKGCFG_FOLDER_INST}/bin/" \
336	 ./configure CC=gcc$CCVERSION CXX=g++$CCVERSION CPP=cpp$CCVERSION LD=g++$CCVERSION \
337	CXXFLAGS="-pthread $ARCHCPPFLAGS $SDK" CFLAGS="-pthread $ARCHCPPFLAGS $SDK" LDFLAGS="-pthread $SDK" \
338	--enable-nls --disable-dependency-tracking --enable-ccache \
339	--with-wxdir=${ROOT_FOLDER}/${WXFOLDER}/ \
340	--with-crypto-prefix=${ROOT_FOLDER}/$CRYPTOPP_FOLDER_INST \
341	--with-libintl-prefix=${ROOT_FOLDER}/${GETTEXT_FOLDER_INST} \
342	--with-libupnp-prefix=${ROOT_FOLDER}/${LIBUPNP_FOLDER_INST} \
343	--with-geoip-static --with-geoip-headers=${ROOT_FOLDER}/${LIBGEOIP_FOLDER_INST}/include --with-geoip-lib=${ROOT_FOLDER}/${LIBGEOIP_FOLDER_INST}/lib/ \
344	--disable-cas --disable-webserver --disable-amulecmd --disable-amule-gui --disable-wxcas --disable-alc --disable-alcc --disable-amule-daemon \
345	--prefix=${ROOT_FOLDER}/amule-inst/ >> $STDOUT_FILE 2>> $ERROR_FILE
346
347	echo -e "\t\tCleaning compilation"
348
349	make clean >> $STDOUT_FILE 2>> $ERROR_FILE
350fi
351
352echo -e "\t\tCompiling aMule"
353
354make >> $STDOUT_FILE 2>> $ERROR_FILE
355
356echo -e "\tDone."
357
358echo -e "\t\tFaking install"
359
360make install >> $STDOUT_FILE 2>> $ERROR_FILE
361
362echo -e "\tDone."
363
364popd >> $STDOUT_FILE 2>> $ERROR_FILE
365
366echo -e "Getting application bundle and packaging"
367
368rm -rf aMule.app aMule.zip >> $STDOUT_FILE 2>> $ERROR_FILE
369
370cp -R ${AMULE_FOLDER}/aMule.app . >> $STDOUT_FILE 2>> $ERROR_FILE
371
372find aMule.app \( -name .svn -o -name "Makefile*" -o -name src \) -print0 | xargs -0 rm -rf >> $STDOUT_FILE 2>> $ERROR_FILE
373
374echo -e "Copying i18n files..."
375cp -r amule-inst/share/locale aMule.app/Contents/SharedSupport/
376echo -e "Done."
377
378. application_packager.sh ${AMULE_FOLDER}/ >> $STDOUT_FILE 2>> $ERROR_FILE
379
380if [ ! -f aMule.zip ]; then
381	echo "ERROR: aMule.zip was not created. Please review the output files"
382else
383	echo "All Done"
384fi
385
386# Pop root folder.
387popd >> $STDOUT_FILE
388