2372d5422145ea3c58bf69b2fbbd8054d5770d6e
[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 cd "$build_dir/netxms-$ver"
161
162 echo -n "Configuring sources..."
163 [ -e "./configure" ] || ./reconf &> /dev/null
164 ./configure --prefix="$build_root/usr" --enable-unicode --with-server \
165 --with-odbc --with-sqlite --with-pgsql --with-mysql --with-agent &> /dev/null
166 if [ $? -ne 0 ]
167 then
168 echo -e \
169 "\nConfiguration failed, make sure you have all the"\
170 "required dev packages installed!"
171 exit 1
172 else
173 echo "OK."
174 fi
175
176 echo -n "Building sources..."
177 make &> /dev/null
178 if [ $? -ne 0 ]
179 then
180 echo -e \
181 "\nBuild process failed, make sure you have all the"\
182 "required dev packages installed!"
183 exit 1
184 else
185 echo "OK."
186 fi
187
188 echo -n "Installing binaries..."
189 make install &> /dev/null
190 if [ $? -ne 0 ]
191 then
192 echo -e \
193 "\nInstallation failed!"
194 exit 1
195 else
196 echo "OK."
197 fi
198
199 mkdir -p "$build_root/etc/init.d"
200 cp "$build_dir/$source/contrib/startup/redhat/netxmsd" "$build_root/etc/init.d"
201 cp "$build_dir/$source/contrib/startup/redhat/nxagentd" "$build_root/etc/init.d"
202
203 find "$build_root" -name *.la -exec rm -f {} \;
204 #find "$build_root" -name *.la \
205 # -exec sed -i -e "s;libdir='.*';libdir='/usr/lib';" {} \;
206
207 cd "$build_root/usr/lib"
208 for nsm in netxms/*.nsm
209 do
210 libname="${nsm##*/}"
211 libname="libnsm_${libname%.*}.so"
212 ln -sf "$nsm" "$libname"
213 unset nsm libname
214 done
215 for ddr in netxms/dbdrv/*.ddr
216 do
217 libname="${ddr##*/}"
218 libname="libnxddr_${libname%.*}.so"
219 ln -sf "$ddr" "$libname"
220 unset ddr libname
221 done
222 cd "$script_dir"
223
224 for art in ${artifacts[@]}
225 do
226 echo "Building artifact $art:"
227 specfile="$spec_dir/$art.spec"
228 yes | ./gen_spec.sh -n "$art" -ver "$ver" -o "$spec_dir" &> /dev/null
229 [ $? -ne 0 ] && exit 1
230 rpmbuild --noclean --define "_topdir `pwd`/$DIR_ROOT" \
231 --buildroot="$build_root" -bb "$specfile" &> /dev/null
232 if [ $? -eq 0 ]
233 then
234 mkdir -p rpms
235 art_rpm="$art-$ver.`uname -m`.rpm"
236 install -m 755 -t "$DIR_OUT" "$rpm_dir/$art_rpm" &> /dev/null
237 echo "$art built successfully."
238 echo -e "$art_rpm saved to ./$DIR_OUT/\n"
239 else
240 echo "$art: build failed!"
241 exit 1
242 fi
243 unset specfile art art_rpm
244 done