posaune
[projects.git] / tools / make / mconfigure
1 #!/bin/bash
2 set -e
3
4 function echo_usage {
5         echo "usage: mconfigure [options] <package>"
6         echo "  -a <arch>"
7         echo "  -c : configure"
8         echo "  -d : compile"
9         echo "  -p : pack default: do all"
10         echo "  -o : support old Debian 7 format"
11 }
12
13 function check_unversioned {
14         # has to be called in the projects base directory
15         # result: "M" in stdout or nothing
16         set -e
17         
18         # check if build is running in a sandbox
19         if ! [ -f .cm.ignore ]
20         then
21                 # we create one
22                 echo ".cm.ignore" > .cm.ignore
23                 echo "debian.*" >> .cm.ignore
24                 echo "up.*" >> .cm.ignore
25         fi
26         
27         project=$(pwd)
28         project=${project##*/}
29         pushd .. >/dev/null
30                 mCM $project -b -an > /dev/null
31                 wc=$(wc $project.batch)
32                 rm $project.batch
33                 if [ "${wc:0:5}" != "0 0 0" ]
34                 then
35                         echo "M"
36                 fi
37         popd >/dev/null
38 }
39
40 function set_build {
41         set -e
42         if [ -e .svn ]
43         then
44                 version="0.0"
45                 build=$(svnversion)
46                 build=${build/:/-}
47                 build="$build$(check_unversioned)"
48         elif [ -e .git ]
49         then
50                 tag=$(git describe --tags --match "${paket}_*" 2>/dev/null) || true
51                 if test -z "$tag"
52                 then
53                         tag="0.0-0TOP"
54                 fi
55                 changes=$(check_unversioned)
56                 tag="$tag$changes"
57                 version=${tag%-*}
58                 version=${version##*_}
59                 build=${tag##*-}
60         else
61                 echo "No configuration system found. Cannot determine version."
62                 exit 1
63         fi
64 }
65
66 function check_version {
67         set -e
68         
69         vline=$(head -1 debian/$paket.changelog)
70         vline=${vline%-*}
71         clversion=${vline#*(}
72         if [ $version = "0.0" ]
73         then
74                 version=$clversion
75                 return
76         fi
77
78         # do no check on a modified git repos
79         if [ "${build: -1}" == "M" ]
80         then
81                 return
82         fi
83
84         if [ "$clversion" != $version ]
85         then
86                 echo "version mismatch: git: $version, changelog: $clversion"
87                 exit 1
88         fi
89 }
90
91 if [ ! -d debian ]
92 then
93         echo "run mconfigure in the project base directory with a debian directory in it!"
94         exit 2
95 fi
96
97 configure=0
98 compile=0
99 pack=0
100 while getopts "a:cdpo" opt; do
101         case $opt in
102                 a) ARCH=$OPTARG
103                         ;;
104                 c) configure=1
105                         ;;
106                 d)      compile=1
107                         ;;
108                 p)      pack=1
109                         ;;
110                 o)      oldpack="-Zgzip"
111                         ;;
112                 \?) echo "Invalid option: -$OPTARG"
113                         echo_usage
114                         exit 1
115                         ;;
116                 :) echo "Option -$OPTARG requires an argument."
117                         echo_usage
118                         exit 1
119                         ;;
120         esac
121 done
122 shift $((OPTIND -1))
123
124 if [ $# -lt 1 ]
125 then
126         echo_usage
127         exit 1
128 fi
129
130 paket=$1
131
132 # if nothing is selected, select all
133 if [ $configure -eq 0 -a $compile -eq 0 -a $pack -eq 0 ]
134 then
135         configure=1
136         compile=1
137         pack=1
138 fi
139
140 if [ $configure -eq 1 ]
141 then
142         # delete changelog and control
143         rm debian/changelog debian/control 2>/dev/null || true
144         
145         # mconfigure builds 2 environment files: setenv.sh + rules.pre
146         
147         if [ -n "$ARCH" ]
148         then
149                 arch_opt="-a $ARCH"
150         fi
151         dpkg-architecture $arch_opt > debian/setenv.sh
152         echo "" > debian/rules.pre
153         
154         echo "paket=$paket" >> debian/setenv.sh
155         echo "oldpack=\"$oldpack\"" >> debian/setenv.sh
156
157         set_build
158         check_version
159         echo "version=$version" >> debian/setenv.sh
160         echo "build=$build" >> debian/setenv.sh
161
162         # get repository name
163 #       tmp=$(pwd)
164 #       pwd=${tmp##*/}
165 #       pwd=${pwd,,*}
166 #       echo "pwd=$pwd" >> debian/rules.pre
167
168         echo "building $paket with build $version-$build"
169
170         pushd debian >/dev/null
171         # ./debian -------------------------------------
172
173         ln -sf /usr/share/mbuild/rules .
174
175         if [ -e tmp ]
176         then
177                 rm -rf tmp
178         fi
179
180         # check for pre/post installation scripts
181         if [ -f $paket.preinst ]
182         then
183                 echo "add_inst_tgt += debian/tmp/DEBIAN/preinst" >> rules.pre
184         fi
185         if [ -f $paket.postinst ]
186         then
187                     echo "add_inst_tgt += debian/tmp/DEBIAN/postinst" >> rules.pre
188         fi
189         if [ -f $paket.prerm ]
190         then
191                     echo "add_inst_tgt += debian/tmp/DEBIAN/prerm" >> rules.pre
192         fi
193         if [ -f $paket.postrm ]
194         then
195                     echo "add_inst_tgt += debian/tmp/DEBIAN/postrm" >> rules.pre
196         fi
197         
198         mkdir -p tmp/DEBIAN
199
200         # add export to setenv.sh
201         sed -i "s/^/export /" setenv.sh
202
203         popd >/dev/null
204         # ./. ---------------------------------------------
205
206         # copy package control
207         if [ -f debian/$paket.control ]
208         then
209                 # proceed
210                 echo
211         else
212                 echo "warning: control file not found"
213                 echo "  this file is necessary for any package production."
214         fi
215         
216         # build prepare
217         if [ -x debian/$paket.prepare ]
218         then
219                 . debian/setenv.sh
220                 debian/$paket.prepare
221         fi
222         if [ -e debian/$paket.build ]
223         then
224                 if [ -e ../build ]
225                 then
226                         rm -rf ../build
227                         mkdir ../build
228                 fi
229                 if grep -- "-prepare" debian/$paket.build >/dev/null
230                 then
231                         debian/rules sync
232                         debian/$paket.build -prepare
233                 fi
234                 # sync build dir
235                 . debian/setenv.sh
236                 debian/rules sync
237                 
238                 # executing prebuild
239                 if [ -x debian/$paket.prebuild ]
240                 then
241                         pushd ~/build >/dev/null
242                         debian/$paket.prebuild
243                         popd >/dev/null
244                 fi
245         fi
246         
247         # pack prepare
248         if [ -f debian/$paket.cp -a -f debian/$paket.control ]
249         then
250                 echo "PACK=binary" >> debian/rules.pre
251                 if grep "^Architecture: *all" debian/$paket.control >/dev/null
252                 then
253                         echo "arch = all" >> debian/rules.pre
254                         echo "BINARY_INDEP = copy ../${paket}_$version-${build}_all.deb" >> debian/rules.pre
255                 else
256                         echo 'arch = ${DEB_HOST_ARCH}' >> debian/rules.pre
257                         echo "BINARY_ARCH = copy ../${paket}_$version-${build}_"'$(arch).deb' >> debian/rules.pre
258                 fi
259         elif [ -f debian/$paket.cp ]
260         then
261                 echo "PACK=zip" >> debian/rules.pre
262         else
263                 echo "PACK=version" >> debian/rules.pre
264         fi
265 fi
266
267 if [ $compile -eq 1 -a ! -e debian/$paket.build ]
268 then
269         echo "no debian/$paket.build: skipping build step"
270         compile=0
271 fi
272 if [ $compile -eq 1 ]
273 then
274         # build artefacts
275         . debian/setenv.sh
276         debian/rules build
277 fi
278
279 if [ $pack -eq 1 ]
280 then
281         # build package
282         . debian/setenv.sh
283         debian/rules pack
284 fi
285