Quote test string
[git-tools-moved-to-github.git] / id-deb-build / id-pbuild.sh
1 #!/bin/sh
2 # This script builds Debian packages for one or more distributions
3 # in i386/amd64 architectures. It uses pbuilder. pbuilder requires root
4 # privilege and so this script should be invoked with sudo.
5
6 usage()
7 {
8     cat <<EOF
9 Usage: id-pbuild.sh [OPTIONS]
10 Options:
11         [--debian=distros]
12         [--ubuntu=distros]
13         [--centos=distros]
14         [--upload]
15         [--concurrency=N]
16         [--product=X]
17 EOF
18     exit $1
19 }
20
21 # things that might be set by options
22 upload=false
23 do_help=false
24 concurrency=4
25 ARCHS="i386 amd64"
26
27 if test -f IDMETA; then
28     . ./IDMETA
29 fi
30 while test $# -gt 0; do
31     case "$1" in
32         -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
33         *) optarg= ;;
34     esac
35     
36     case $1 in
37         --debian=*)
38             DEBIAN_DIST=$optarg
39             ;;
40         --ubuntu=*)
41             UBUNTU_DIST=$optarg
42             ;;
43         --centos=*)
44             CENTOS_DIST=$optarg
45             ;;
46         --upload)
47             upload=true
48             ;;
49         --concurrency=*)
50             concurrency=$optarg
51             ;;
52         --product=*)
53             PRODUCT=$optarg
54             ;;
55         -*)
56             do_help=yes
57             ;;
58     esac
59     shift
60 done
61
62 if test "$do_help" = "yes"; then
63     usage 1 1>&2
64 fi
65
66 D0=`dirname $0`
67 ID_DEB_BUILD=`cd $D0; pwd`
68 PBUILDROOT=${ID_DEB_BUILD}/id-pbuild-root.sh
69 if test ! -x $PBUILDROOT; then
70     echo "$PBUILDROOT not found"
71     exit 1
72 fi
73
74 MKDEBSRC=${ID_DEB_BUILD}/id-mk-deb-src.sh
75 if test ! -x $MKDEBSRC; then
76     echo "$MKDEBSRC not found"
77     exit 1
78 fi
79
80 if $upload; then
81     :
82 else
83     if test "${SUDO_USER}"; then
84         if test ! -d deb-src; then
85             $MKDEBSRC
86         fi
87     else
88         if test -d deb-src; then
89             rm -f deb-src/*.gz
90             if test $? -ne 0; then
91                 echo "deb-src could not be removed. Remove deb-src manually"
92                 exit 1
93             fi
94         fi
95         $MKDEBSRC
96     fi
97 fi
98 cd deb-src
99
100 for f in *.dsc; do
101     if test -f "$f"; then
102         PKG=$f
103     fi
104 done
105 if test -z "$PKG"; then
106     echo "Specify .dsc file"
107     exit 1
108 fi
109 if test ! -e "$PKG"; then
110     echo "${PKG} does not exist"
111     exit 1
112 fi
113 if test -z "$PRODUCT"; then
114     PRODUCT=`echo *.dsc|sed 's/_.*//g'`
115 fi
116 if grep "Architecture: all" $PKG >/dev/null; then
117     USE_ARCHS=i386
118 else
119     USE_ARCHS=$ARCHS
120 fi
121 echo "Dsc $PKG . Product $PRODUCT"
122 echo "Ubuntu distros: $UBUNTU_DIST"
123 echo "Debian distros: $DEBIAN_DIST"
124 echo "Centos distros: $CENTOS_DIST"
125 if test "${SUDO_USER}"; then
126     echo "Running as sudo."
127     echo "Tip: You can avoid it by adding the following in /etc/sudoers"
128     echo "${SUDO_USER} ALL=NOPASSWD: ${ID_DEB_BUILD}/id-pbuild-root.sh"
129     HOME_EXPORT=/home/${SUDO_USER}
130 else
131     sudo -k
132     HOME_EXPORT=$HOME
133 fi
134 if test ! -f ${HOME_EXPORT}/.pbuilderrc; then
135     echo "${HOME_EXPORT}/.pbuilderrc missing"
136     exit 1
137 fi
138
139 if $upload; then
140     for dist in ${CENTOS_DIST}; do
141         arch=amd64
142         prodver=${PRODUCT}-${VERSION}
143         if ssh $dist "cd rpmbuild/SOURCES/${prodver} && ../../../proj/git-tools/id-rpm-build/upload-rpms.sh ${PRODUCT}.spec"; then
144             :
145             else
146             echo "Upload failed for $dist"
147             exit 1
148         fi
149     done
150     for dist in ${UBUNTU_DIST}; do
151         scp ${dist}-*/* ftp.indexdata.dk:/home/ftp/pub/${PRODUCT}/ubuntu/${dist}
152     done
153     for dist in ${DEBIAN_DIST}; do
154         scp ${dist}-*/* ftp.indexdata.dk:/home/ftp/pub/${PRODUCT}/debian/${dist}
155     done
156     echo ""
157     echo "===================================================="
158     echo "Remember to update APT/YUM on ftp.indexdata.dk !!!"
159     echo ""
160     echo "ssh ftp.indexdata.dk"
161     echo "cd ~/proj/git-tools/update-archive"
162     echo "sudo su"
163     echo "./update-archive.sh"
164     if test "${CENTOS_DIST}"; then
165         echo "./update-yum-archive.sh"
166     fi
167     echo "===================================================="
168     exit 0
169 fi
170
171 rm -f *.log
172
173 # copy tars to centos build hosts
174 for dist in ${CENTOS_DIST}; do
175     arch=amd64
176     prodver=${PRODUCT}-${VERSION}
177     tarname=${prodver}.tar.gz
178     if test ! -f ../${tarname}; then
179         echo "$tarname: missing"
180         exit 1
181     fi
182
183     if ssh $dist "test -f .rpmmacros && test -d rpmbuild/SOURCES && test -d rpmbuild/RPMS && test -d rpmbuild/SRPMS && test -x proj/git-tools/id-rpm-build/upload-rpms.sh"; then
184         :
185     else
186         echo "Host $dist does not seem to be prepared with git-tools and rpmbuild directories"
187         exit 1
188     fi
189
190     if scp ../${tarname} $dist:rpmbuild/SOURCES/ ; then
191         :
192     else
193         echo "Could not copy $tarname to $dist"
194         exit 1
195     fi
196     if ssh $dist "cd rpmbuild/SOURCES && rm -fr $prodver && tar zxf $tarname && cd ${prodver} && rpmbuild -ba ${PRODUCT}.spec" >${dist}-${arch}.log 2>&1; then
197         :
198     else
199         echo "FAILED: See ${dist}-${arch}.log"
200         exit 1
201     fi
202 done
203
204 # call our privileged wrapper to clean the directories, no --dsc option
205 for dist in ${DEBIAN_DIST} ${UBUNTU_DIST}; do
206     for arch in ${USE_ARCHS}; do
207         sudo -n $PBUILDROOT --home=${HOME_EXPORT} --dist=$dist --arch=$arch
208         if test $? -ne 0; then
209             echo "FAILED"
210             echo "Run id-pbuild.sh using sudo or add yourself to /etc/sudoers:"
211             echo "${USER} ALL=NOPASSWD: ${ID_DEB_BUILD}/id-pbuild-root.sh"
212             exit 1
213         fi
214     done
215 done
216
217 # call our privileged wrapper to do the real work, with --dsc option
218 i=0
219 date
220 for dist in ${DEBIAN_DIST} ${UBUNTU_DIST}; do
221     for arch in ${USE_ARCHS}; do
222         sudo -n $PBUILDROOT --home=${HOME_EXPORT} \
223             --dist=$dist --arch=$arch --dsc=$PKG >${dist}-${arch}.log 2>&1 &
224         i=`expr $i + 1`
225         if test $i -eq $concurrency; then
226             i=0
227             wait
228             if test -f ${dist}-${arch}/*.dsc; then
229                 :
230             else
231                 echo "FAILED: No .dsc file for ${dist}-${arch}"
232                 exit 1
233             fi
234         fi
235     done
236 done
237 wait
238 for dist in ${DEBIAN_DIST} ${UBUNTU_DIST}; do
239     for arch in ${USE_ARCHS}; do
240         if test -f ${dist}-${arch}/*.dsc; then
241             :
242         else
243             echo "FAILED: No .dsc file for ${dist}-${arch}"
244             exit 1
245         fi
246     done
247 done
248
249 date
250 echo "Done"
251 # Local Variables:
252 # mode:shell-script
253 # sh-indentation: 2
254 # sh-basic-offset: 4
255 # End: