epic-ts
[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                 pushd .. >/dev/null
36                         build="$build$(check_unversioned)"
37                 popd >/dev/null
38         elif [ -e .git ]
39         then
40                 tag=$(git describe --tags 2>/dev/null) || true
41                 if test -z "$tag"
42                 then
43                         tag="0.0-0TOP"
44                 fi
45                 tag_paket=${tag%_*}
46                 if [ "$tag_paket" != "$paket" ]
47                 then
48                         tag="0.0-0TOP"
49                 fi
50                 changes=$(check_unversioned)
51                 tag="$tag$changes"
52                 version=${tag%-*}
53                 version=${version##*_}
54                 build=${tag##*-}
55         else
56                 echo "No configuration system found. Cannot determine version."
57                 exit 1
58         fi
59 }
60
61 function check_version {
62         
63         vline=$(head -1 debian/$paket.changelog)
64         vline=${vline%-*}
65         clversion=${vline#*(}
66         if [ $version = "0.0" ]
67         then
68                 version=$clversion
69                 return
70         fi
71
72         # do no check on a modified git repos
73         if [ "${build: -1}" == "M" ]
74         then
75                 return
76         fi
77
78         if [ "$clversion" != $version ]
79         then
80                 echo "version mismatch: git: $version, changelog: $clversion"
81                 exit 1
82         fi
83 }
84 # to build a package you need ...
85 # <package>.cp (optional): copy step for package production
86 # <package>.cpp.sh (optional): commands to setup the C++ compile environment
87
88 if [ $# -lt 1 ]
89 then
90         echo_usage
91         exit 1
92 fi
93
94 if [ ! -d debian ]
95 then
96         echo "run mconfigure in the project base directory with a debian directory in it!"
97         exit 2
98 fi
99
100 paket=$1
101
102 configure=0
103 compile=0
104 pack=0
105 shift
106 while getopts ":acdp" opt; do
107         case $opt in
108                 a) ARCH=$OPTARG
109                         ;;
110                 c) configure=1
111                         ;;
112                 d)      compile=1
113                         ;;
114                 p)      pack=1
115                         ;;
116                 \?) echo "Invalid option: -$OPTARG"
117                         echo_usage
118                         exit 1
119                         ;;
120                 :) echo "Option -$OPTARG requires an argument."
121                         echo_usage
122                         exit 1
123                         ;;
124         esac
125 done
126
127 # if nothing is selected, select all
128 if [ $configure -eq 0 -a $compile -eq 0 -a $pack -eq 0 ]
129 then
130         configure=1
131         compile=1
132         pack=1
133 fi
134
135 if [ $configure -eq 1 ]
136 then
137         # delete changelog and control
138         rm debian/changelog debian/control || true
139         
140         # mconfigure builds 2 environment files: setenv.sh + rules.pre
141         # setenv.sh is finally appended to rules.pre
142         
143         dpkg-architecture > debian/setenv.sh
144         echo "" > debian/rules.pre
145         
146         echo "paket=$paket" >> debian/setenv.sh
147 #       shift
148         ARCH=""
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         elif [ -f debian/$paket.cp ]
252         then
253                 echo "PACK=zip" >> debian/rules.pre
254         else
255                 echo "PACK=version" >> debian/rules.pre
256         fi
257 fi
258
259 if [ $compile -eq 1 -a ! -e debian/$paket.build ]
260 then
261         echo "no debian/build.sh: skipping build step"
262         compile=0
263 fi
264 if [ $compile -eq 1 ]
265 then
266         # build artefacts
267         . debian/setenv.sh
268         debian/rules build
269 fi
270
271 if [ $pack -eq 1 ]
272 then
273         # build package
274         . debian/setenv.sh
275         debian/rules pack
276 fi
277