-
Notifications
You must be signed in to change notification settings - Fork 10
/
make-sb2-image-meta.sh
executable file
·164 lines (140 loc) · 4.16 KB
/
make-sb2-image-meta.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
#!/bin/bash
#
# Creates meta data file for the given sb2 image file
#
# Copyright (C) 2022 Jolla Ltd.
# All rights reserved.
#
# You may use this file under the terms of BSD license as follows:
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the Jolla Ltd nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
set -o nounset
set -o pipefail
synopsis()
{
cat <<END
usage: make-sb2-image-meta.sh <sb2-image>
END
}
short_usage()
{
cat <<END
$(synopsis)
Try 'make-sb2-image-meta.sh --help' for more information.
END
}
usage()
{
less --quit-if-one-screen <<END
$(synopsis)
Create meta data file for the given sb2 image file.
These meta data files are used by sdk-manage at sb2 image installation time.
For the <sb2-image> file a corresponding meta data file will be created in the
current working directory, with the same base name but the ".meta" suffix
added.
END
}
fatal()
{
echo "make-sb2-image-meta.sh: fatal: $*" >&2
}
bad_usage()
{
fatal "$*"
short_usage >&2
}
with_tmp_file()
{
local file=$1 cmd=("${@:2}")
local tmp_file=
with_tmp_file_cleanup()
(
trap 'echo cleaning up...' INT TERM HUP
if [[ $tmp_file ]]; then
rm -f "$tmp_file"
fi
)
trap 'with_tmp_file_cleanup; trap - RETURN' RETURN
trap 'return 1' INT TERM HUP
tmp_file=$(mktemp "$file.XXX") || return
if "${cmd[@]}" <&3 >"$tmp_file"; then
cat <"$tmp_file" >"$file" || return
else
return $?
fi
} 3<&0 <&-
get_meta_data()
{
local archive=$1
local ssu_ini=
ssu_ini=$(tar -x --to-stdout -f "$archive" ./etc/ssu/ssu.ini) || return
local brand=
brand=$(sed -n 's/^brand=//p' <<<"$ssu_ini") || return
[[ $brand ]] || { fatal "Failed to determine Brand name"; return 1; }
local release=
release=$(sed -n 's/^release=//p' <<<"$ssu_ini") || return
[[ $release ]] || { fatal "Failed to determine Release version"; return 1; }
cat <<END
[General]
meta-version=1
brand=$brand
release=$release
END
}
main()
{
local opt_archive=
while (( $# > 0 )); do
case $1 in
-h)
short_usage
return
;;
--help)
usage
return
;;
-*)
bad_usage "Unexpected argument: $1"
return 1
;;
*)
if [[ ! $opt_archive ]]; then
opt_archive=$1
else
bad_usage "Unexpected argument: $1"
return 1
fi
;;
esac
shift
done
if [[ ! $opt_archive ]]; then
bad_usage "Argument expected"
return 1
fi
local meta="$(basename "$opt_archive").meta"
with_tmp_file "$meta" get_meta_data "$opt_archive" || return
}
main "$@"