1#! /bin/bash
2
3
4#
5# Copyright (c) 2004-2011 Marcelo Roberto Jimenez ( phoenix@amule.org )
6#
7# All rights reserved. This script is provided under the terms of the GPL.
8#
9
10
11#
12# Example usage: copy all tarballs to a directory, cd to it and
13# $ ./amule_build_install.sh
14#
15
16
17SCRIPT_VERSION="2.0.0"
18
19
20USERHOME=$(echo ~)
21DEFAULT_PREFIX="${USERHOME}/usr"
22DEFAULT_TARDIR=$(pwd)
23DEFAULT_UNTARDIR=${DEFAULT_TARDIR}/untars
24JOBS=4
25
26
27#
28# This function uses three parameters:
29#    $1 - an input parameter specifying a mask for the distribution, 
30#         e.g., 'aMule-CVS-*' for cvs tarballs or 'aMule-*' for distro tarballs.
31#    $2 - an output parameter with the name of the variable that will
32#         receive the full file name of the tar archive
33#    $3 - an output parameter with the name of the variable that will
34#         receive the basename of the distribution
35#
36function lookup_distro {
37    # assign the filename of distribution
38    # the following is equivalent to execute XXX_FILENAME=$(ls ${TARDIR}/$1)
39    eval $2=$(ls ${TARDIR}/$1)
40
41    # Now we use indirection to dereference $2
42    #echo $2    # evaluates to XXX_FILENAME
43    #echo ${!2} # evaluates to /home/user/dir/xxx-y.z.t.tar.gz
44
45    # remove the directory and the extension parts and assing it 
46    # to XXX_DISTRO in $3
47    case ${!2} in
48	*.gz)
49	    eval $3=$(basename ${!2} .tar.gz)
50	    ;;
51	
52	*.bz2)
53	    eval $3=$(basename ${!2} .tar.bz2)
54	    ;;
55	
56	*.zip)
57	    eval $3=$(basename ${!2} .zip)
58	    ;;
59    esac
60}
61
62
63function untar_distro {
64    # $1 evaluates to /home/user/dir/xxx-y.z.t.tar.gz or .bz2
65    local TARCMD=
66    case $1 in
67	*.gz)
68	    TARCMD="tar zxf"
69	    ;;
70	*.bz2)
71	    TARCMD="tar jxf"
72	    ;;
73	*.zip)
74	    TARCMD="unzip -ao"
75	    ;;
76    esac
77    $TARCMD $1
78}
79
80
81function init_package_versions {
82    #
83    # Put the software distributions in directory $TARDIR and the
84    # script does the rest. There can even be a mixture of .tar.gz and
85    # .tar.bz2 because the untarring routine tests it on the fly
86    #
87    # single quotes have to be used here to avoid expansion
88    #
89    lookup_distro 'cryptopp*'  CRYPTOPP_FILENAME CRYPTOPP_DISTRO
90    lookup_distro 'libupnp-*' LIBUPNP_FILENAME LIBUPNP_DISTRO
91    lookup_distro 'wxWidgets-*.*.*.tar.*' WXWIDGETS_FILENAME WXWIDGETS_DISTRO
92    lookup_distro 'aMule-*' AMULE_FILENAME AMULE_DISTRO
93
94    echo
95    echo "Software packacge versions:"
96    echo "    cryptopp  : $CRYPTOPP_DISTRO"
97    echo "    libupnp   : $LIBUPNP_DISTRO"
98    echo "    wxWidgets : $WXWIDGETS_DISTRO"
99    echo "    aMule     : $AMULE_DISTRO"
100    echo
101}
102
103
104function init_environment {
105    echo
106    echo aMule building script, version $SCRIPT_VERSION
107    echo
108
109    # only prompt if we're missing information
110    if [ x$PREFIX = x ] || [ x$TARDIR = x ] || [ x$UNTARDIR = x ] ; then
111
112	PREFIX=${DEFAULT_PREFIX}
113	echo "Where to install?"
114	read -p "[${PREFIX}]: "
115	if [ x$REPLY != x ]; then PREFIX=${REPLY}; fi
116
117	echo
118	echo "Where are the tarballs?"
119	TARDIR=${DEFAULT_TARDIR}
120	read -p "[${TARDIR}]: "
121	if [ x$REPLY != x ]; then TARDIR=${REPLY}; fi
122
123	echo
124	echo "Where to untar?"
125	UNTARDIR=${DEFAULT_UNTARDIR}
126	read -p "[${UNTARDIR}]: "
127	if [ x$REPLY != x ]; then UNTARDIR=${REPLY}; fi
128
129    fi
130
131    # test that we have write permissions to the install dir
132    TST_DIR=${PREFIX}/tmp
133    TST_FILE=${TST_DIR}/test-if-has-write-permission
134    mkdir -p ${TST_DIR}
135    touch ${TST_FILE}
136    if [ ! -w ${TST_FILE} ]; then
137	echo "You don't appear to have write permissions to ${PREFIX}."
138	echo "You must fix that before continuing."
139	exit
140    fi
141    rm -f ${TST_FILE}
142
143    echo
144    echo "Building for:"
145    echo "    --prefix=${PREFIX}"
146    echo "    tarballs are at ${TARDIR}"
147    echo "    tarballs will be untarred at ${UNTARDIR}"
148    echo
149
150    # Initialize package version variables
151    init_package_versions
152}
153
154
155#
156# cryptopp
157#
158function build_cryptopp {
159	CRYPTOPP_INSTALL_DIR="${PREFIX}/cryptopp"
160	CRYPTOPP_SOURCES_DIR="cryptopp_tmpdir"
161
162	rm -rf ${CRYPTOPP_SOURCES_DIR}
163	mkdir -p ${CRYPTOPP_SOURCES_DIR}
164	cd ${CRYPTOPP_SOURCES_DIR}
165
166	untar_distro ${CRYPTOPP_FILENAME}
167
168	make -f GNUmakefile -j${JOBS}
169	PREFIX=${CRYPTOPP_INSTALL_DIR} make install
170	cd ..
171}
172
173
174#
175# libUPnP
176#
177function build_libupnp {
178	untar_distro ${LIBUPNP_FILENAME}
179
180	LIBUPNP_INSTALL_DIR="${PREFIX}/libupnp"
181	cd libupnp-?.?.?
182	./configure \
183		--enable-debug \
184		--prefix=${LIBUPNP_INSTALL_DIR} \
185		&& \
186		make -j${JOBS} > /dev/null && \
187		make install > /dev/null
188	cd ..
189}
190
191
192#
193# wxWidgets
194#
195function build_wxwidgets {
196	untar_distro ${WXWIDGETS_FILENAME}
197	
198	WXWIDGETS_INSTALL_DIR="${PREFIX}/wxWidgets"
199	cd wxWidgets-?.?.?
200	./configure \
201		--enable-mem_tracing \
202		--enable-debug \
203		--disable-optimise \
204		--enable-debug_flag \
205		--enable-debug_info \
206		--enable-debug_gdb \
207		--with-opengl \
208		--enable-gtk2 \
209		--enable-unicode \
210		--enable-largefile \
211		--prefix=${WXWIDGETS_INSTALL_DIR} \
212		&& \
213		make -j${JOBS} > /dev/null &&
214		make install > /dev/null
215	cd ..
216}
217
218
219#
220# aMule
221#
222function build_amule {
223	untar_distro ${AMULE_FILENAME}
224
225	AMULE_INSTALL_DIR="${PREFIX}/amule"
226	cd amule-cvs
227	./configure \
228		--enable-ccache \
229		--with-denoise-level=3 \
230		--enable-debug \
231		--disable-optimize \
232		--enable-verbose \
233		--enable-geoip \
234		--enable-cas \
235		--enable-wxcas \
236		--enable-amule-gui \
237		--enable-webserver \
238		--enable-amulecmd \
239		--enable-amule-daemon \
240		--with-wx-config=${WXWIDGETS_INSTALL_DIR}/bin/wx-config \
241		--prefix=${AMULE_INSTALL_DIR} \
242		--with-crypto-prefix=${CRYPTOPP_INSTALL_DIR} \
243		--with-libupnp-prefix=${LIBUPNP_INSTALL_DIR} \
244		&& \
245		LD_LIBRARY_PATH=${WXWIDGETS_INSTALL_DIR}/lib make -j${JOBS} && \
246		LD_LIBRARY_PATH=${WXWIDGETS_INSTALL_DIR}/lib make install > /dev/null
247}
248
249#
250# Here is where the fun begins...
251#
252init_environment
253
254#
255# Go to ${UNTARDIR}
256#
257mkdir -p ${UNTARDIR}
258cd ${UNTARDIR}
259
260#
261# Build stuff
262#
263build_cryptopp
264build_libupnp
265build_wxwidgets
266build_amule
267
268#
269# Leave ${UNTARDIR}
270#
271cd ..
272
273
274echo 
275echo Finished compilation.
276echo You should run aMule like this:
277echo '$LD_LIBRARY_PATH'=${WXWIDGETS_INSTALL_DIR}/lib:${LIBUPNP_INSTALL_DIR}/lib LANG=en_US.UTF-8 ${AMULE_INSTALL_DIR}/bin/amule
278echo
279echo Of course you may choose to use a different locale.
280echo
281
282