posaune
[projects.git] / tools / make / mconfigure
1 #!/bin/bash
2 set -e
3
4 function echo_usage {
5         echo "usage: mconfigure <package> [options]"
6         echo "  -a <arch>"
7         echo "  -c : configure"
8         echo "  -d : compile"
9         echo "  -p : pack default: do all"
10 }
11
12 function check_unversioned {
13         # has to be called in the projects base directory
14         # result: "M" in stdout or nothing
15         
16         project=$(pwd)
17         project=${project##*/}
18         pushd .. >/dev/null
19                 mCM $project -b -an > /dev/null
20                 wc=$(wc $project.batch)
21                 rm $project.batch
22                 if [ "${wc:0:5}" != "0 0 0" ]
23                 then
24                         echo "M"
25                 fi
26         popd >/dev/null
27 }
28
29 function set_build {
30         if [ -e .svn ]
31         then
32                 version="0.0"
33                 build=$(svnversion)
34                 build=${build/:/-}
35                 build="$build$(check_unversioned)"
36         elif [ -e .git ]
37         then
38                 tag=$(git describe --tags 2>/dev/null) || true
39                 if test -z "$tag"
40                 then
41                         tag="0.0-0TOP"
42                 fi
43                 tag_paket=${tag%_*}
44                 if [ "$tag_paket" != "$paket" ]
45                 then
46                         tag="0.0-0TOP"
47                 fi
48                 changes=$(check_unversioned)
49                 tag="$tag$changes"
50                 version=${tag%-*}
51                 version=${version##*_}
52                 build=${tag##*-}
53         else
54                 echo "No configuration system found. Cannot determine version."
55                 exit 1
56         fi
57 }
58
59 function check_version {
60         
61         vline=$(head -1 debian/$paket.changelog)
62         vline=${vline%[-)]*}
63         clversion=${vline#*(}
64         if [ $version = "0.0" ]
65         then
66                 version=$clversion
67                 return
68         fi
69
70         # do no check on a modified git repos
71         if [ "${build: -1}" == "M" ]
72         then
73                 return
74         fi
75
76         if [ "$clversion" != $version ]
77         then
78                 echo "version mismatch: git: $version, changelog: $clversion"
79                 exit 1
80         fi
81 }
82 # to build a package you need ...
83 # <package>.cp (optional): copy step for package production
84 # <package>.cpp.sh (optional): commands to setup the C++ compile environment
85
86 if [ $# -lt 1 ]
87 then
88         echo_usage
89         exit 1
90 fi
91
92 if [ ! -d debian ]
93 then
94         echo "run mconfigure in the project base directory with a debian directory in it!"
95         exit 2
96 fi
97
98 paket=$1
99
100 configure=0
101 compile=0
102 pack=0
103 shift
104 while getopts "a:cdp" opt; do
105         case $opt in
106                 a) ARCH=$OPTARG
107                         ;;
108                 c) configure=1
109                         ;;
110                 d)      compile=1
111                         ;;
112                 p)      pack=1
113                         ;;
114                 \?) echo "Invalid option: -$OPTARG"
115                         echo_usage
116                         exit 1
117                         ;;
118                 :) echo "Option -$OPTARG requires an argument."
119                         echo_usage
120                         exit 1
121                         ;;
122         esac
123 done
124
125 # if nothing is selected, select all
126 if [ $configure -eq 0 -a $compile -eq 0 -a $pack -eq 0 ]
127 then
128         configure=1
129         compile=1
130         pack=1
131 fi
132
133 if [ $configure -eq 1 ]
134 then
135         # delete changelog and control
136         rm debian/changelog debian/control 2>/dev/null || true
137         
138         # mconfigure builds 2 environment files: setenv.sh + rules.pre
139         
140         if [ -n "$ARCH" ]
141         then
142                 arch_opt="-a $ARCH"
143         fi
144         dpkg-architecture $arch_opt > debian/setenv.sh
145         echo "" > debian/rules.pre
146         
147         echo "paket=$paket" >> debian/setenv.sh
148
149         cpp_build=0
150
151         set_build
152         check_version
153         echo "version=$version" >> debian/setenv.sh
154         echo "build=$build" >> debian/setenv.sh
155
156         # get repository name
157 #       tmp=$(pwd)
158 #       pwd=${tmp##*/}
159 #       pwd=${pwd,,*}
160 #       echo "pwd=$pwd" >> debian/rules.pre
161
162         echo "building $paket with build $version-$build"
163
164         pushd debian >/dev/null
165         # ./debian -------------------------------------
166
167         ln -sf /usr/share/mbuild/rules .
168
169         if [ -e tmp ]
170         then
171                 rm -rf tmp
172         fi
173
174         # check for pre/post installation scripts
175         if [ -f $paket.preinst ]
176         then
177                 echo "add_inst_tgt += debian/tmp/DEBIAN/preinst" >> rules.pre
178         fi
179         if [ -f $paket.postinst ]
180         then
181                     echo "add_inst_tgt += debian/tmp/DEBIAN/postinst" >> rules.pre
182         fi
183         if [ -f $paket.prerm ]
184         then
185                     echo "add_inst_tgt += debian/tmp/DEBIAN/prerm" >> rules.pre
186         fi
187         if [ -f $paket.postrm ]
188         then
189                     echo "add_inst_tgt += debian/tmp/DEBIAN/postrm" >> rules.pre
190         fi
191         
192         #cat setenv.sh >> rules.pre
193
194         mkdir -p tmp/DEBIAN
195
196         # load util functions for C/C++ - build
197         if [ -f $paket.cpp.sh ]
198         then
199                 cpp_build=1
200                 . projects/tools/make/c_configure.sh
201                 . $paket.cpp.sh
202         fi
203         
204         # add export to setenv.sh
205         sed -i "s/^/export /" setenv.sh
206
207         popd >/dev/null
208         # ./. ---------------------------------------------
209
210         # copy package control
211         if [ -f debian/$paket.control ]
212         then
213                 # proceed
214                 echo
215         else
216                 # try old fashoned way
217                 control=$(find etc -name $paket.control) || true
218                 if [ -z "$control" ]
219                 then
220                         echo "warning: control file not found"
221                         echo "  this file is necessary for any package production."
222                 else    
223                         echo "Source: $paket
224 Section: main
225 Priority: optional
226 Maintainer: WagnerTech UG <mail@wagnertech.de>
227 " > debian/$paket.control
228                         grep -v "Version:" $control |grep -v "Maintainer:" >> debian/$paket.control 
229                 fi
230         fi
231         
232         # build prepare
233         if [ -e debian/$paket.build ]
234         then
235                 if [ -e ../build ]
236                 then
237                         rm -rf ../build
238                         mkdir ../build
239                 fi
240                 if grep -- "-prepare" debian/$paket.build >/dev/null
241                 then
242                         debian/rules sync
243                         debian/$paket.build -prepare
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/build.sh: 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