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