]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - builder.sh
0ce45b38c13afeb3e9e86896932d740f6dd8fe8e
[packages/rpm-build-tools.git] / builder.sh
1 #!/bin/sh
2 # -----------
3 # Exit codes:
4 #       0 - succesful
5 #       1 - help dispayed
6 #       2 - no spec file name in cmdl parameters
7 #       3 - spec file not stored in repo
8 #       4 - some source, apatch or icon files not stored in repo
9 #       5 - build package no succed
10
11 VERSION="\
12 Build package utility from PLD CVS repository
13 V 0.8 (C) 1999 Tomasz K³oczko".
14
15 PATH="/bin:/usr/bin:/usr/sbin:/sbin:/usr/X11R6/bin"
16
17 COMMAND="build"
18
19 SPECFILE=""
20 BE_VERBOSE=""
21 QUIET=""
22 CLEAN=""
23 DEBUG=""
24 NOURLS=""
25 CVSROOT=${CVSROOT:-""}
26 LOGFILE=""
27 CHMOD="yes"
28
29 PATCHES=""
30 SOURCES=""
31 ICONS=""
32 PACKAGE_RELEASE=""
33 PACKAGE_VERSION=""
34 PACKAGE_NAME=""
35
36 if [ -f ~/etc/builderrc ]; then
37   . ~/etc/builderrc
38 elif [ -f ~/.builderrc ]; then
39   . ~/.builderrc
40 fi
41
42 #---------------------------------------------
43 # functions
44
45 usage()
46 {
47     if [ -n "$DEBUG" ]; then set -xv; fi
48     echo "\
49 Usage: builder [-D] [--debug] [-V] [--version] [-a] [--as_anon] [-b] [-ba]
50         [--build] [-bb] [--build-binary] [-bs] [--build-source]
51         [-d <cvsroot>] [--cvsroot <cvsroot>] [-g] [--get] [-h] [--help]
52         [-l <logfile>] [-m] [--mr-proper] [--logtofile <logfile>] [-q] [--quiet]
53         [-r <cvstag>] [--cvstag <cvstag>] [-u] [--no-urls] [-v] [--verbose]
54         <package>.spec
55
56         -D, --debug     - enable script debugging mode,
57         -V, --version   - output builder version
58         -a, --as_anon   - get files via pserver as cvs@anoncvs.pld.org.pl,
59         -b, -ba,
60         --build         - get all files from CVS repo or HTTP/FTP and build
61                           package from <package>.spec,
62         -bb,
63         --build-binary  - get all files from CVS repo or HTTP/FTP and build
64                           binary only package from <package>.spec,
65         -bs,
66         --build-source  - get all files from CVS repo or HTTP/FTP and only
67                           pack them into src.rpm,
68         -c, --clean     - clean all temporarily created files (in BUILD,
69                           SOURCES, SPECS and \$RPM_BUILD_ROOT),
70                           SOURCES, SPECS and \$RPM_BUILD_ROOT),
71         -d, --cvsroot   - setup \$CVSROOT,
72         -g, --get       - get <package>.spec and all related files from
73                           CVS repo or HTTP/FTP,
74         -h, --help      - this message,
75         -l, --logtofile - log all to file,
76         -m, --mr-proper - only remove all files relayted to spec file and
77                           all work resources,
78         -q, --quiet     - be quiet,
79         -r, --cvstag    - build package using resources from specified CVS
80                           tag,
81         -u, --no-urls   - try to get sources only from CVS repo,
82         -v, --verbose   - be verbose,
83
84 "
85 }
86
87 parse_spec()
88 {
89     if [ -n "$DEBUG" ]; then set -xv; fi
90
91     sed -e "s#%prep#%dump#I" $SPECFILE | grep -v -i "^Icon\:" > $SPECFILE.__
92
93     SOURCES="`rpm -bp --test $SPECFILE.__ 2>&1 | awk '/ SOURCEURL[0-9]+/ {print $3}'`"
94     PATCHES="`rpm -bp --test $SPECFILE.__ 2>&1 | awk '/ PATCHURL[0-9]+/ {print $3}'`"
95     ICONS="`awk '/^Icon:/ {print $2}' ${SPECFILE}`"
96     PACKAGE_NAME="`rpm -bp --test $SPECFILE.__ 2>&1 | awk '/ name/ {print $3}'`"
97     PACKAGE_VERSION="`rpm -bp --test $SPECFILE.__ 2>&1 | awk '/ PACKAGE_VERSION/ {print $3}'`"
98     PACKAGE_RELEASE="`rpm -bp --test $SPECFILE.__ 2>&1 | awk '/ PACKAGE_RELEASE/ {print $3}'`"
99
100     rm -f $SPECFILE.__
101
102     if [ -n "$BE_VERBOSE" ]; then
103         echo "- Sources :  `nourl $SOURCES`" 
104         if [ -n "$PATCHES" ]; then
105                 echo "- Patches :  `nourl $PATCHES`"
106         else
107                 echo "- Patches :  *no patches needed*"
108         fi
109         if [ -n "$ICONS" ]; then
110             echo "- Icon    :  `nourl $ICONS`"
111         else
112             echo "- Icon    :  *no package icon*"
113         fi
114         echo "- Name    : $PACKAGE_NAME"
115         echo "- Version : $PACKAGE_VERSION"
116         echo "- Release : $PACKAGE_RELEASE"
117     fi
118 }
119
120 Exit_error()
121 {
122     if [ -n "$DEBUG" ]; then set -xv; fi
123
124     cd $__PWD
125
126     case "$@" in
127     "err_no_spec_in_cmdl" )
128         echo "ERROR: spec file name not specified.";
129         exit 2 ;;
130     "err_no_spec_in_repo" )
131         echo "Error: spec file not stored in CVS repo.";
132         exit 3 ;;
133     "err_no_source_in_repo" )
134         echo "Error: some source, patch or icon files not stored in CVS repo.";
135         exit 4 ;;
136     "err_build_fail" )
137         echo "Error: package build failed.";
138         exit 5 ;;
139     esac
140 }
141
142 init_builder()
143 {
144     if [ -n "$DEBUG" ]; then set -xv; fi
145
146     SOURCE_DIR="`rpm --eval "%{_sourcedir}"`"
147     SPECS_DIR="`rpm --eval "%{_specdir}"`"
148
149     __PWD=`pwd`
150 }
151
152 get_spec()
153 {
154     if [ -n "$DEBUG" ]; then set -xv; fi
155
156     cd $SPECS_DIR
157
158     OPTIONS="up "
159
160     if [ -n "$CVSROOT" ]; then
161         OPTIONS="-d $CVSROOT $OPTIONS"
162     fi
163     if [ -n "$CVSTAG" ]; then
164         OPTIONS="$OPTIONS -r $CVSTAG"
165     else
166         OPTIONS="$OPTIONS -A"
167     fi
168
169     cvs $OPTIONS $SPECFILE
170     if [ "$?" -ne "0" ]; then
171         Exit_error err_no_spec_in_repo;
172     fi
173         if [ ! -f "$SPECFILE" ]; then
174         Exit_error err_no_spec_in_repo;
175         fi
176     
177     if [ "$CHMOD" = "yes" ]; then
178         chmod 444 $SPECFILE
179     fi
180     unset OPTIONS
181 }
182
183 get_all_files()
184 {
185     if [ -n "$DEBUG" ]; then set -xv; fi
186
187     if [ -n "$SOURCES$PATCHES$ICONS" ]; then
188         cd $SOURCE_DIR
189
190         OPTIONS="up "
191         if [ -n "$CVSROOT" ]; then
192             OPTIONS="-d $CVSROOT $OPTIONS"
193         fi
194         if [ -n "$CVSTAG" ]; then
195             OPTIONS="$OPTIONS -r $CVSTAG"
196         else
197             OPTIONS="$OPTIONS -A"
198         fi
199
200         cvs $OPTIONS `nourl $SOURCES $PATCHES $ICONS`
201         
202         if [ -z "$NOURLS" ]; then
203                 for i in $SOURCES $PATCHES $ICONS; do
204                         if [ ! -f "`nourl $i`" ]&&\
205                         [ `echo $i | grep 'ftp://\|http://'` ]; then
206                                 wget -c -t0 "$i"
207                         fi
208                 done
209         fi
210         
211         for i in $SOURCES $PATCHES $ICONS; do
212                 if [ ! -f "`nourl $i`" ]; then
213                         Exit_error err_no_source_in_repo;
214                 fi
215         done
216
217         if [ "$CHMOD" = "yes" ]; then
218             chmod 444 `nourl $SOURCES $PATCHES $ICONS`
219         fi
220         unset OPTIONS
221     fi
222 }
223
224 build_package()
225 {
226     if [ -n "$DEBUG" ]; then set -xv; fi
227
228     cd $SPECS_DIR
229     case "$COMMAND" in
230         build )
231             BUILD_SWITCH="-ba" ;;
232         build-binary )
233             BUILD_SWITCH="-bb" ;;
234         build-source )
235             BUILD_SWITCH="-bs" ;;
236     esac
237     rpm $BUILD_SWITCH -v $QUIET $CLEAN $SPECFILE
238
239     if [ "$?" -ne "0" ]; then
240         Exit_error err_build_fail;
241     fi
242     unset BUILD_SWITCH
243 }
244
245 nourl()
246 {
247         echo "$@" | sed 's#\<\(ftp\|http\)://.*/##g'
248 }
249 #---------------------------------------------
250 # main()
251
252 if [ "$#" = 0 ]; then
253     usage;
254     exit 1
255 fi
256
257 while test $# -gt 0 ; do
258     case "${1}" in
259         -D | --debug )
260             DEBUG="yes"; shift ;;
261         -V | --version )
262             COMMAND="version"; shift ;;
263         -a | --as_anon )
264             CVSROOT=":pserver:cvs@anoncvs.pld.org.pl:/cvsroot"; shift ;;
265         -b | -ba | --build )
266             COMMAND="build"; shift ;;
267         -bb | --build-binary )
268             COMMAND="build-binary"; shift ;;
269         -bs | --build-source )
270             COMMAND="build-source"; shift ;;
271         -c | --clean )
272             CLEAN="--clean --rmspec --rmsource"; shift ;;
273         -d | --cvsroot )
274             shift; CVSROOT="${1}"; shift ;;
275         -g | --get )
276             COMMAND="get"; shift ;;
277         -h | --help )
278             COMMAND="usage"; shift ;;
279         -l | --logtofile )
280             shift; LOGFILE="${1}"; shift ;;
281         -m | --mr-proper )
282             COMMAND="mr-proper"; shift ;;
283         -q | --quiet )
284             QUIET="--quiet"; shift ;;
285         -r | --cvstag )
286             shift; CVSTAG="${1}"; shift ;;
287         -v | --verbose )
288             BE_VERBOSE="1"; shift ;;
289         -u | --no-urls )
290             NOURLS="yes"; shift ;;
291         * )
292             SPECFILE="${1}"; shift ;;
293     esac
294 done
295
296 if [ -n "$DEBUG" ]; then set -xv; fi
297
298 case "$COMMAND" in
299     "build" | "build-binary" | "build-source" )
300         init_builder;
301         if [ -n "$SPECFILE" ]; then
302             get_spec;
303             parse_spec;
304             get_all_files;
305             build_package;
306         else
307             Exit_error err_no_spec_in_cmdl;
308         fi
309         ;;
310     "get" )
311         init_builder;
312         if [ -n "$SPECFILE" ]; then
313             get_spec;
314             parse_spec;
315             get_all_files;
316         else
317             Exit_error err_no_spec_in_cmdl;
318         fi
319         ;;
320     "mr-proper" )
321         rpm --clean --rmsource --rmspec --force --nodeps $SPECFILE
322         ;;
323     "usage" )
324         usage;;
325     "version" )
326         echo "$VERSION";;
327 esac
328
329 cd $__PWD
This page took 0.085087 seconds and 2 git commands to generate.