-
Notifications
You must be signed in to change notification settings - Fork 0
/
debutize
executable file
·284 lines (240 loc) · 6.87 KB
/
debutize
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/bin/bash
#
# debutize - A simple .deb builder
#
loglvl=2
# log messages to stderr
_log() {
[[ $loglvl -lt $1 ]] && echo $2: $3 >&2
}
# handle error messages
_err() {
_log 1 "error" $1
[[ ! -z $2 ]] && exit $2
}
# handle warning messages
_warn() {
_log 2 "warning" $1
}
if [[ ! "$(whoami)" = "root" ]]; then
_warn "not running as root!"
fi
helpersdir="/usr/share/debutize/helpers"
if [[ ! -z "$DEBUTIZE_HELPERSDIR" ]]; then
helpersdir="$DEBUTIZE_HELPERSDIR"
fi
if [[ -z "$1" ]]; then
echo "usage: $0 dir [ name [ version ] ]"
exit 1
fi
origin=$(pwd)
dir=$1
if [[ ! -d "$dir" ]]; then
_err "directory '$dir' doesn't exist" 1
fi
(
if [[ ! "$dir" = "$(pwd)" ]]; then
cd $dir
fi
# default values
pkg_architecture="all"
pkg_depends=""
pkg_section=""
pkg_priority="extra"
pkg_maintainer=""
pkg_description=""
targets="conf/:/etc/$pkg_name/ etc/:/etc/$pkg_name/ bin/:/usr/sbin/ lib/:/usr/lib/$pkg_name/"
conffiles="/etc/$pkg_name:*.conf"
helpers=""
# load project config
if [[ ! -f "./.debutize.conf" ]]; then
_err ".debutize.conf not found" 1
fi
. "$(pwd)/.debutize.conf"
# environmental overrides
if [[ ! -z "$DEBUTIZE_PKG_NAME" ]]; then
pkg_name=$DEBUTIZE_PKG_NAME
fi
if [[ ! -z "$DEBUTIZE_PKG_VERSION" ]]; then
pkg_version=$DEBUTIZE_PKG_VERSION
fi
# command-line overrides
if [[ ! -z "$2" ]]; then
pkg_name=$2
fi
if [[ ! -z "$3" ]]; then
pkg_version=$3
fi
# check that required values are defined
for i in name version architecture maintainer description; do
if [[ -z "$(eval "echo \$pkg_$i")" ]]; then
_err "pkg_$i must be defined" 2
fi
done
# determine build directory
tmpdir="/tmp/debutize_$(echo "$pkg_name $pkg_version $(date)" | sha1sum | cut -d' ' -f1)"
# delete the build directory if it already exists
if [[ -d "$tmpdir" ]]; then
rm -r "$tmpdir"
fi
# make the build directory
echo -n "creating temporary directory $tmpdir..."
mkdir -p "$tmpdir/deb/DEBIAN"
chmod 700 "$tmpdir"
echo "done"
# run prerun command
if [[ ! -z "$prerun" ]]; then
echo -n "running prerun command..."
( cd $origin; /bin/sh -c "$origin/$prerun $tmpdir" > /dev/null )
echo "done"
fi
# populate the control file
echo -n "generating DEBIAN/control..."
echo "Package: $pkg_name
Version: $pkg_version
Architecture: $pkg_architecture
Depends: $pkg_depends
Section: $pkg_section
Priority: $pkg_priority
Maintainer: $pkg_maintainer
Description: $pkg_description" >> $tmpdir/deb/DEBIAN/control
echo "done"
# if specified, copy etc targets
if [[ ! -z "$targets" ]]; then
echo "copying targets..."
for pair in $targets; do
src=$(echo $pair | cut -d: -f1)
if [[ ! -z "$(echo $pair | grep ':')" ]]; then
dst=$(echo $pair | cut -d: -f2 | sed 's/^\///')
fi
if [[ ! -e "$src" ]]; then
_warn "$src not found, skipping" " "
continue
fi
if [[ -z "$dst" ]]; then
dst=$src
fi
if [[ ! -z "$(echo $src | grep -e '/$')" ]]; then
mkdir -p $tmpdir/deb/$dst
echo " copying $src* to $tmpdir/deb/$dst"
cp -rp $src* $tmpdir/deb/$dst
else
if [[ ! -z "$(echo $dst | grep -e '/$')" ]]; then
mkdir -p $tmpdir/deb/$dst
else
mkdir -p $tmpdir/deb/$(dirname $dst)
fi
echo " copying $src to $tmpdir/deb/$dst"
cp -rp $src $tmpdir/deb/$dst
fi
done
echo "done"
fi
# populate the conffiles file
echo "generating DEBIAN/conffiles..."
for pair in $conffiles; do
base=$(echo $pair | cut -d: -f1 | sed 's/^\///')
glob=$(echo $pair | cut -d: -f2 -s)
if [[ ! -d "$tmpdir/deb/$base" ]]; then
_warn "confspec $base not found, skipping"
continue
fi
if [[ -d "$tmpdir/deb/$base" ]]; then
if [ -z "$glob" ]; then
glob="*"
fi
echo " including all files under /$base"
(
cd $tmpdir/deb
find $base -name "$glob" -type f -print0 | xargs -0 -I? echo /? >> "DEBIAN/conffiles"
)
else
echo " including /$base"
echo "/$base" >> "$tmpdir/deb/DEBIAN/conffiles"
fi
done
echo "done"
# populate the md5sums and sha1sums files
echo -n "generating DEBIAN/md5sums and DEBIAN/sha1sums..."
( cd $tmpdir/deb | find . -type f | grep -v -e '^DEBIAN' | xargs md5sum | sed s/\ ^\.// >> "$tmpdir/deb/DEBIAN/md5sums" )
( cd $tmpdir/deb | find . -type f | grep -v -e '^DEBIAN' | xargs sha1sum | sed s/\ ^\.// >> "$tmpdir/deb/DEBIAN/sha1sums" )
echo "done"
# process any defined helper commands
if [[ ! -z "$helpers" ]]; then
echo "processing helpers..."
for pattern in $helpers; do
for helper in $(find $helpersdir -name "$pattern" -type f); do
type=$(basename $helper | cut -d. -f2)
case $type in
postinst|preinst|postrm|prerm)
echo " $(basename $helper)"
script="$tmpdir/deb/DEBIAN/$type"
if [[ ! -f "$script" ]]; then
echo -e "#!/bin/sh\nset -e\n" > $script
chmod +x $script
if [[ ! -z "$service_name" ]]; then
echo "SERVICE_NAME=\"$service_name\"" >> $script
fi
fi
echo "### start $helper" >> $script
cat $helper >> $script
echo "### end $helper" >> $script
;;
*)
_err "helper not found '$helper'" 3
;;
esac
done
done
echo "done"
fi
# run postrun command
if [[ ! -z "$postrun" ]]; then
echo -n "running postrun command..."
( cd $tmpdir/deb; /bin/sh -c "$origin/$postrun $origin" > /dev/null )
echo "done"
fi
# apply permissions map if available
if [[ -f "$origin/.debutize.pmap" ]]; then
echo -n "applying permission map..."
{
while read line; do
mode=$(echo $line | cut -d: -f1)
user=$(echo $line | cut -d: -f2)
group=$(echo $line | cut -d: -f3)
path=$(echo $line | cut -d: -f4)
recurse=$(echo $line | cut -d: -f5)
if [[ "$recurse" == "R" ]]; then
opts="-R"
fi
chmod $opts $mode "$tmpdir/deb/$path"
chown $opts $user "$tmpdir/deb/$path"
chgrp $opts $group "$tmpdir/deb/$path"
done
} < "$origin/.debutize.pmap"
echo "done"
fi
# prepare package name
pkgdst="${pkg_name}_${pkg_version}"
if [[ ! -z "$pkg_architecture" ]]; then
pkgdst="${pkgdst}_${pkg_architecture}"
fi
# build package
echo -n "building package..."
failed=0
dpkg-deb -b "$tmpdir/deb" $pkgdst.deb > /dev/null
if [[ ! $? = 0 ]]; then
failed=1
echo "failed!"
else
echo "done"
fi
echo -n "cleaning up..."
rm -r "$tmpdir"
echo "done"
if [[ $failed = 0 ]]; then
echo "built $pkgdst.deb"
else
echo "failed to build $pkgdst.deb due to errors"
fi
)