-
Notifications
You must be signed in to change notification settings - Fork 45
/
install-linux.sh
367 lines (283 loc) · 7.92 KB
/
install-linux.sh
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/bin/bash
set -e
pushd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null
_SRCDIR=$(pwd)
_TMPDIR=$(readlink -f /tmp)
_ID='owon-vds-tiny'
_NAME='OWON VDS1022'
_GENERICNAME='Oscilloscope'
_SUMMARY='Application for the OWON VDS1022 oscilloscope'
_VERSION=$(<version.txt)
_VENDOR='Copyright (C) Fujian Lilliput Optoelectronics Technology Co.,Ltd'
_CATEGORIES='Utility;Electronics;Engineering'
_HOMEPAGE='https://github.com/florentbr/OWON-VDS1022'
_DESCRIPTION='Modified software for the OWON VDS1022 oscilloscope'
_PREINSTALL="
true
"
_POSTINSTALL="
rm -f /home/*/.$_ID/preferences* || true
udevadm control --reload-rules
udevadm trigger
"
_POSTREMOVE="
rm -fr /home/*/.$_ID || true
"
main () {
[ "$EUID" = 0 ] || raise "This script requires eleveted privileges."
echo "==========================================================="
echo " Build package "
echo "==========================================================="
echo 'Check environement ...'
local arch=$(dpkg --print-architecture 2>/dev/null || uname -m)
case "$arch" in
amd64|x86_64) arch=amd64 ;;
arm64|aarch64*) arch=arm64 ;;
i?86) arch=i386 ;;
esac
[ -d "$_SRCDIR/lib/linux/$arch" ] || raise "Architecture not supported: ${arch}"
local packager
for packager in pacman dnf yum zypper ppm apt / ; do
[ $packager = / ] && raise 'Package manager not supported'
command -v $packager >/dev/null && break
done
local tmpdir=$(mktemp -d)
pushd "$tmpdir" >/dev/null
chmod 0755 .
case $packager in
apt) build-deb $arch ;;
pacman) build-pac $arch ;;
dnf|zypper|yum) build-rpm $arch ;;
ppm) build-pet $arch ;;
esac
popd >/dev/null
rm -rf "${tmpdir}"
chmod 0755 "$_PACKAGE"
printf "\nPackage:\n${_PACKAGE}\n\n"
echo "==========================================================="
echo " Install package ${_PACKAGE##*/}"
echo "==========================================================="
case "$packager" in
apt) apt install --reinstall "$_PACKAGE" ;;
pacman) pacman -U "$_PACKAGE" ;;
dnf) dnf install "$_PACKAGE" ;;
zypper) zypper install "$_PACKAGE" ;;
yum) yum install "$_PACKAGE" ;;
ppm) pkg -f install "$_PACKAGE" ;;
*) raise "Packager not supported" ;;
esac
env -i /bin/bash -c 'type java >/dev/null 2>&1' || raise "Java not found!"
echo -e "\nSUCCESS !\n"
}
build-deb () {
local arch=$1
write_files "$arch"
local size=$(du -s -k | egrep -o '^[0-9]+')
echo 'Build debian package ...'
write DEBIAN/control <<EOF
Package: ${_ID}
Source: ${_ID}
Version: ${_VERSION}
Section: non-free/electronics
Depends: libusb-1.0-0, libc6, java-runtime
Architecture: ${arch}
Vendor: ${_VENDOR}
Installed-Size: ${size}
Maintainer: na <na>
Homepage: ${_HOMEPAGE}
Description: ${_SUMMARY}
${_DESCRIPTION}
EOF
write DEBIAN/preinst +x <<EOF
#!/bin/bash
${_PREINSTALL}
EOF
write DEBIAN/postinst +x <<EOF
#!/bin/bash
[ "\$1" = configure ] || exit 0
${_POSTINSTALL}
EOF
write DEBIAN/postrm +x <<EOF
#!/bin/bash
[ "\$1" = remove ] || exit 0
${_POSTREMOVE}
EOF
_PACKAGE="$_TMPDIR/$_ID-$_VERSION.$arch.deb"
rm -f "$_PACKAGE"
dpkg-deb -b -Zgzip . "$_PACKAGE" >/dev/null || exit 1
}
build-pac () {
local arch=$1
write_files "$arch"
local size=$(du -s -b | egrep -o '^[0-9]+')
case "$arch" in
amd64|x86_64) arch=x86_64 ;;
arm64|aarch64*) arch=aarch64 ;;
esac
echo 'Build pacman package ...'
write .PKGINFO +x <<EOF
pkgname = ${_ID}
pkgbase = ${_ID}
pkgver = ${_VERSION}
pkgdesc = ${_SUMMARY}
url = ${_HOMEPAGE}
builddate = $(date -u '+%s')
packager = na <na>
size = ${size}
arch = ${arch}
depend = libusb
depend = java-runtime
EOF
write .INSTALL +x <<EOF
pre_install () {${_PREINSTALL}}
pre_upgrade () {${_PREINSTALL}}
post_install () {${_POSTINSTALL}}
post_upgrade () {${_POSTINSTALL}}
post_remove () {${_POSTREMOVE}}
EOF
_PACKAGE="$_TMPDIR/$_ID-$_VERSION.$arch.pac"
rm -f "$_PACKAGE"
tar -czvf "$_PACKAGE" .PKGINFO .INSTALL * >/dev/null || exit 1
}
build-rpm () {
local arch=$1
mkdir BUILD BUILDROOT RPMS SOURCES SPECS SRPMS
pushd BUILDROOT >/dev/null
write_files "$arch"
local files=$(find ! -type d | egrep -o '/.*')
case "$arch" in
amd64|x86_64) arch=x86_64 ;;
arm64|aarch64*) arch=aarch64 ;;
esac
popd >/dev/null
echo 'Build rpm package ...'
write "SPECS/$_ID.spec" <<EOF
Name: ${_ID}
Version: ${_VERSION/-*/}
Release: ${_VERSION/*-/}
Summary: ${_SUMMARY}
Group: Applications/Engineering
License: Multiple
Vendor: ${_VENDOR}
URL: ${_HOMEPAGE}
Packager: na <na>
Requires: libusb-1.0.so.0, libc.so.6, jre
AutoReqProv: no
%define _binary_payload w6.gzdio
%description
${_DESCRIPTION}
%files
${files}
%pre -p /bin/bash ${_PREINSTALL}
%post -p /bin/bash ${_POSTINSTALL}
%postun -p /bin/bash ${_POSTREMOVE}
EOF
rpmbuild -bb\
--define "_topdir $PWD"\
--buildroot "$PWD/BUILDROOT"\
--target "$arch" "SPECS/$_ID.spec"\
--noclean --nocheck --quiet\
> /dev/null || exit 1
_PACKAGE="$_TMPDIR/$_ID-$_VERSION.$arch.rpm"
rm -f "$_PACKAGE"
mv RPMS/*/*.rpm "$_PACKAGE" || exit 1
}
build-pet () {
local arch=$1
mkdir "$_ID-$_VERSION.$arch"
pushd "$_ID-$_VERSION.$arch" >/dev/null
write_files "$arch"
local size=$(du -s -k | egrep -o '^[0-9]+')
case "$arch" in
amd64|x86_64) arch=x86_64 ;;
i?86) arch=i386 ;;
esac
echo 'Build pet package ...'
local f01=$_ID-$_VERSION.$arch #pkgname
local f02=$_ID #nameonly
local f03=$_VERSION #version
local f04= #pkgrelease
local f05= #category
local f06=${size}K #size
local f07= #path
local f08=$_ID-$_VERSION.$arch.pet #fullfilename
local f09=+libusb-1.0,+libc6,+java-runtime #dependencies
local f10=$_SUMMARY #description
local f11= #compileddistro
local f12= #compiledrelease
local f13= #repo
write pet.specs <<< "$f01|$f02|$f03|$f04|$f05|$f06|$f07|$f08|$f09|$f10|$f11|$f12|$f13|"
write pinstall.sh +x <<< "${_POSTINSTALL}" # post-install
write puninstall.sh +x <<< "${_POSTREMOVE}" # post-uninstall
popd >/dev/null
_PACKAGE="$_TMPDIR/$_ID-$_VERSION.$arch.pet"
rm -f "$_PACKAGE"
tar -czvf "$_PACKAGE" * >/dev/null
md5sum -b "$_PACKAGE" | cut -z -c 1-32 | tr -d '\0' >> "$_PACKAGE"
}
write_files () {
local arch=$1
echo 'Add program files ...'
copy "$_SRCDIR"/{fwr,doc,version.txt} "opt/$_ID/"
copy "$_SRCDIR"/lib/linux/$arch/* "opt/$_ID/lib/"
copy "$_SRCDIR"/lib/*.jar "opt/$_ID/lib/"
write "opt/$_ID/launch" +x <<EOF
#!/bin/bash
java -cp '/opt/$_ID/lib/*' com.owon.vds.tiny.Main
EOF
echo 'Add desktop menu ...'
mkdir -p usr/bin
ln -s "/opt/$_ID/launch" "usr/bin/$_ID"
for px in 32 48 64 96 128 256 ; do
copy "$_SRCDIR/ico/icon-${px}.png" "usr/share/icons/hicolor/${px}x${px}/apps/$_ID.png"
done
write "usr/share/applications/$_ID.desktop" <<EOF
[Desktop Entry]
Name=${_NAME}
GenericName=${_GENERICNAME}
Comment=${_SUMMARY}
Icon=$_ID
Terminal=false
Type=Application
Exec=java -cp "/opt/$_ID/lib/*" com.owon.vds.tiny.Main
Categories=${_CATEGORIES};
StartupWMClass=com-owon-vds-tiny-Main
EOF
write "usr/share/metainfo/$_ID.appdata.xml" <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<component type="desktop">
<id>${_ID}.desktop</id>
<name>${_NAME}</name>
<summary>${_SUMMARY}</summary>
<description><p>${_DESCRIPTION}</p></description>
<launchable type="desktop-id">${_ID}.desktop</launchable>
<url type="homepage">${_HOMEPAGE}</url>
<provides><binary>${_ID}</binary></provides>
</component>
EOF
echo 'Add usb permissions ...'
write "etc/udev/rules.d/70-$_ID.rules" <<EOF
SUBSYSTEMS=="usb", ATTRS{idVendor}=="5345", ATTRS{idProduct}=="1234", MODE="0666"
EOF
}
copy () {
# source, dest folder or file
if [[ "${!#}" == */ ]] || [[ -d "${!#}" ]] ; then
mkdir -p "${!#}"
cp --no-preserve=mode,ownership -r "$@"
else
mkdir -p "$(dirname "${!#}")"
cp --no-preserve=mode,ownership "$@"
fi
}
write () {
# 1: fname, 2: optional chmod, stdin: text
mkdir -p "$(dirname "$1")"
echo -e "$(</dev/stdin)" > "$1"
[ -z "$2" ] || chmod "$2" "$1"
}
raise () {
echo -e "Error: $1" >&2
exit 1
}
main "$@"