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