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