Dirty trix to fix linx.
[public/netxms.git] / packages / linux / redhat / package.sh
1 #!/bin/bash
2
3 source ./vars.sh
4
5 script_dir="$(pwd)"
6
7 function cleanup {
8 cd "$script_dir"
9 [ "$dont_cleanup" ] || ./clean.sh
10 }
11
12 trap "cleanup" EXIT
13
14 function usage {
15 cat <<USAGE
16 Usage:
17 ${0#*/} options
18
19 Available options:
20 -ver ver
21 Version of the artifact to build
22
23 -a artifact's name
24 Build the specified artifact. By default,
25 artifacts will be created based on the artifact
26 directories in the current path
27
28 -dc
29 Don't cleanup
30
31 -h, --help
32 Print this help and exit
33
34 USAGE
35 exit 1
36 }
37
38 function check_ver {
39 [ $# -ne 2 ] && return 1
40
41 "$1" &> /dev/null
42 [ "$?" -eq 127 ] && \
43 echo "You don't seem to have $1 installed!" && \
44 return 1
45
46 [[ "$($1 --version)" =~ ([0-9].[0-9]*.[0-9]*) ]] && \
47 ver="${BASH_REMATCH[1]}" || return 1
48
49 (IFS='.'; ver=($ver); req_ver=($2)
50 ver_length="${#ver[@]}"
51 for (( i=0; i < ver_length; i++ ))
52 do
53 [ "${ver[$i]}" -ge "${req_ver[$i]}" ] || return 1
54 done)
55 }
56
57 [ $# -lt 1 ] && usage
58
59 check_ver rpmbuild 4.10.1 || exit 1
60
61 while [ $# -gt 0 ]
62 do
63 case $1 in
64 -h | --help)
65 usage;;
66
67 -ver)
68 [ ! -z "$2" ] && ver="$2" || shift
69 shift 2;;
70
71 -a)
72 [ ! -z "$2" ] && artifacts=("${artifacts[@]}" "$2") || shift
73 shift 2;;
74
75 -dc)
76 dont_cleanup=true
77 shift;;
78
79 *)
80 echo "Unknown option: $1"
81 exit 1;;
82 esac
83 done
84
85 [ -z "$ver" ] && echo "No version provided!" && exit 1
86 #[ "$verbose" ] && output=1 || output=/dev/null
87
88 if [ -z "$artifacts" ]
89 then
90 for file in `ls`
91 do
92 [ -d "$file" ] && \
93 if [ -e "$file/$file.conf" ]
94 then
95 echo "* Artifact $file found, adding to the artifact list."
96 artifacts=("${artifacts[@]}" "$file")
97 else
98 echo "$file - not an artifact, skipping."
99 fi
100 done
101 echo
102 fi
103
104 [ -z "$artifacts" ] && echo "No artifacts to build." && exit 1
105
106 $(./init.sh)
107
108 source="netxms-$ver"
109 source_archive="$source.tar.gz"
110 web_archive="http://www.netxms.org/download/archive/$source_archive"
111 top_dir="$script_dir/$DIR_ROOT"
112 source_dir="$top_dir/$DIR_SOURCE"
113 spec_dir="$top_dir/$DIR_SPEC"
114 rpm_dir="$top_dir/$DIR_RPMS"
115 build_root_dir="$top_dir/$DIR_BUILDROOT"
116 build_root="$build_root_dir/netxms-$ver"
117 build_dir="$top_dir/$DIR_BUILD"
118
119 if [ -e "$source_archive" ]
120 then
121 echo "$source_archive found in the current directory."
122 cp -f "./$source_archive" "$source_dir"
123 else
124 echo "$source_archive isn't found in the current directory."
125 wget &> /dev/null
126 wget_exit="$?"
127 if [ "$wget_exit" -ne 127 ]
128 then
129 echo -e "Downloading from the web.\n"
130 wget -N -P "$source_dir" "$web_archive"
131 [ "$?" -ne 0 ] && exit 1
132 else
133 $(curl &> /dev/null);
134 if [ "$?" -ne 127 ]
135 then
136 echo -e "Downloading from the web.\n"
137 cd "$source_dir" && curl -R -O "$web_archive"
138 [ "$?" -ne 0 ] && exit 1
139 else
140 echo "You don't seem to have a download manager installed."
141 echo "Supported managers are wget and curl."
142 exit 1
143 fi
144 fi
145
146 unset wget_exit
147 fi
148
149 echo -n "Extracting sources..."
150 tar -xf "$source_dir/$source_archive" -p -C "$build_dir"
151 if [ $? -ne 0 ]
152 then
153 echo -e \
154 "\nExtraction failed!"
155 exit 1
156 else
157 echo "OK."
158 fi
159
160 # Directory hacks
161 find "$build_dir" -name nxsrvapi.h -exec \
162 sed -i -e 's;#ifndef LIBDIR;#undef LIBDIR\n#ifndef LIBDIR;' \
163 -e 's;#ifndef DATADIR;#undef DATADIR\n#ifndef DATADIR;' \
164 -e 's;#define DEFAULT_DATA_DIR.*;#define DEFAULT_DATA_DIR _T("/usr/share/netxms");' \
165 -e 's;#ifndef PKGLIBDIR;#undef PKGLIBDIR\n#ifndef PKGLIBDIR;' {} \;
166 find "$build_dir" -name libnxdb.h -exec \
167 sed -i -e 's;#include <nxdbapi.h>;#include <nxdbapi.h>\n#define PKGLIBDIR _T("/usr/lib/netxms");' {} \;
168
169 cd "$build_dir/netxms-$ver"
170
171 echo -n "Configuring sources..."
172 [ -e "./configure" ] || ./reconf &> /dev/null
173 ./configure --prefix="$build_root/usr" --enable-unicode --with-server \
174 --with-odbc --with-sqlite --with-pgsql --with-mysql --with-agent &> /dev/null
175 if [ $? -ne 0 ]
176 then
177 echo -e \
178 "\nConfiguration failed, make sure you have all the"\
179 "required dev packages installed!"
180 exit 1
181 else
182 echo "OK."
183 fi
184
185 echo -n "Building sources..."
186 make &> /dev/null
187 if [ $? -ne 0 ]
188 then
189 echo -e \
190 "\nBuild process failed, make sure you have all the"\
191 "required dev packages installed!"
192 exit 1
193 else
194 echo "OK."
195 fi
196
197 echo -n "Installing binaries..."
198 make install &> /dev/null
199 if [ $? -ne 0 ]
200 then
201 echo -e \
202 "\nInstallation failed!"
203 exit 1
204 else
205 echo "OK."
206 fi
207
208 echo
209
210 mkdir -p "$build_root/etc/init.d"
211 cp "$build_dir/$source/contrib/startup/redhat/netxmsd" "$build_root/etc/init.d"
212 cp "$build_dir/$source/contrib/startup/redhat/nxagentd" "$build_root/etc/init.d"
213
214 find "$build_root" -name *.la -exec rm -f {} \;
215 #find "$build_root" -name *.la \
216 # -exec sed -i -e "s;libdir='.*';libdir='/usr/lib';" {} \;
217
218 cd "$build_root/usr/lib"
219 for nsm in netxms/*.nsm
220 do
221 libname="${nsm##*/}"
222 libname="libnsm_${libname%.*}.so"
223 ln -sf "$nsm" "$libname"
224 unset nsm libname
225 done
226 for ddr in netxms/dbdrv/*.ddr
227 do
228 libname="${ddr##*/}"
229 libname="libnxddr_${libname%.*}.so"
230 ln -sf "$ddr" "$libname"
231 unset ddr libname
232 done
233 cd "$script_dir"
234
235 for art in ${artifacts[@]}
236 do
237 echo "Building artifact $art:"
238 specfile="$spec_dir/$art.spec"
239 yes | ./gen_spec.sh -n "$art" -ver "$ver" -o "$spec_dir" &> /dev/null
240 [ $? -ne 0 ] && exit 1
241 rpmbuild --noclean --define "_topdir `pwd`/$DIR_ROOT" \
242 --buildroot="$build_root" -bb "$specfile" &> /dev/null
243 if [ $? -eq 0 ]
244 then
245 mkdir -p rpms
246 art_rpm="$art-$ver.`uname -m`.rpm"
247 install -m 755 -t "$DIR_OUT" "$rpm_dir/$art_rpm" &> /dev/null
248 echo "$art built successfully."
249 echo -e "$art_rpm saved to ./$DIR_OUT/\n"
250 else
251 echo "$art: build failed!"
252 exit 1
253 fi
254 unset specfile art art_rpm
255 done