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