-
Notifications
You must be signed in to change notification settings - Fork 864
/
sysidk
executable file
·361 lines (319 loc) · 10.3 KB
/
sysidk
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
#!/bin/bash
# sysidk: System Image Development Kit
# Potentially stand-alone access to the sysidk scripts and installation
# management
cmd="$0"
cmd_dir="`dirname -- "$cmd"`"
cmd_dir=`(cd "$cmd_dir">/dev/null;pwd)`
cmd_name="`basename -- $cmd`"
dir_sysidk="$HOME/.sysidk"
rc_sysidk="sysidk.rc"
env_sysidk="sysidk.env"
default_dir_bin="/opt/sysidk/bin"
default_dir_pkg="/var/sysidk"
default_dir_localbin="$dir_sysidk/bin"
default_dir_localpkg="$dir_sysidk/pkg"
sysidk_users=users
[ -z "$DIR_BCM_RELEASES" ] || default_dir_pkg="$DIR_BCM_RELEASES"
help()
{
if [ "_$1" = "_commands" ]; then
sysidk_help_commands
else
echo "syntax: <command> <options>"
echo " Run a system image development kit command"
echo "syntax: --help commands"
echo " List the <commands>"
echo "syntax: <command> --help"
echo " Help about command"
echo
echo "syntax: --setup [-l] [-b <install_dir>] [-p <pkg_dir>] <scripts package> "
echo " Place sysidk scripts in the installation directory"
echo " -l Use local directories - no need to setup with root access"
echo " -b Set binary directory for scripts"
echo " ($default_dir_bin by default or "
echo " $default_dir_localbin if using -l)"
echo " -p Set directory for storing packages"
echo " ($default_dir_pkg by default or "
echo " $default_dir_localpkg if using -l)"
echo "syntax: --add <package>..."
echo " Install sysidk packages"
echo " (Use 'sysidk sysipk -h' for other package management options)"
echo
echo "syntax: --install [-l] [-b <install_dir>] [-p <pkg_dir>] <scripts package> "
echo " Setup (as --setup below) and install sysidk commands into your"
echo " shell sessions (on next logon)"
echo "syntax: --uninstall"
echo " Uninstall sysidk commands from your shell sessions (effective"
echo " after you next logon)"
echo
echo "syntax: --bindir"
echo " Show installed binary directory"
echo "syntax: --pkgdir"
echo " Show installed packages directory"
fi
}
content_env()
{ local scripts="$1"; shift
local pkgs="$1"; shift
cat <<EOF
# do no edit - written by $cmd_name --setup
# this file is intended to be 'source'd by a bourne shell
# last updated `date`
dir_bcm_scripts="$scripts"
DIR_BCM_RELEASES="$pkgs"
EOF
return $?
}
content_rc()
{ cat <<EOF
# do no edit - written by $cmd_name --setup
# this file is intended to be 'source'd by a bourne shell during shell startup
# last updated `date`
source "$dir_sysidk/$env_sysidk"
export DIR_BCM_RELEASES
if [ -f "\$dir_bcm_scripts/bcm_setup" ]; then
source "\$dir_bcm_scripts/bcm_setup"
fi
EOF
return $?
}
setup_dir_sysidk()
{ local do_update=false
local rc=0
[ "_$1" = "_-u" ] && { do_update=true; shift; }
if [ ! -d "$dir_sysidk" ]; then
mkdir "$dir_sysidk"
do_update=true
fi
if [ ! -d "$dir_sysidk" ]; then
echo "$cmd_name: can't set up $dir_sysidk - sorry"
rc=1
fi
return $rc
}
check_have_scripts()
{ local rc=0
if [ -z "$dir_bcm_scripts" ]; then
echo "$cmd_name: Sorry - you need to install the scripts">&2
echo "$cmd_name: use --setup or --install">&2
rc=2
elif [ ! -d "$dir_bcm_scripts" ]; then
echo "$cmd_name: Your scripts directory doesn't exist - $dir_bcm_scripts">&2
echo "$cmd_name: try reinstalling using --setup or --install?">&2
rc=1
fi
return $rc
}
check_have_packages()
{ local rc=0
if [ -z "$DIR_BCM_RELEASES" ]; then
echo "$cmd_name: Sorry - you need to install the scripts">&2
echo "$cmd_name: use --setup or --install">&2
rc=2
elif [ ! -d "$DIR_BCM_RELEASES" ]; then
echo "$cmd_name: Your packages directory doesn't exist - $DIR_BCM_RELEASES">&2
echo "$cmd_name: try reinstalling using --setup or --install?">&2
rc=1
fi
return $rc
}
sysidk_help_commands()
{
if setup_dir_sysidk; then
[ -r "$dir_sysidk/$env_sysidk" ] && . "$dir_sysidk/$env_sysidk"
if check_have_scripts; then
echo "$cmd_name: Commands"
ls "$dir_bcm_scripts" | grep -v "^lib$" | xargs -I ^ echo " ^"
fi
fi
}
sysidk_add()
{ local rc=0
if ! PATH="$dir_bcm_scripts:$PATH" type sysipk>/dev/null 2>/dev/null; then
echo "$cmd_name: your installed scripts don't include the 'sysipk' command">&2
echo "$cmd_name: add the scripts package first?">&2
rc=1
else
local opts=
local pk
local type
for pk in "$@"; do
case "$pk" in
-* | --*) opts="$opts $pk";;
*-root-nodata.tgz | *-data-root.tgz | *-root.tgz) type=root;;
*-boot.tgz) type=boot;;
*-trk.tgz) type=track;;
*-bin.tgz) type=scripts;;
*) type=unknown;;
esac
if [ "$type" = "unknown" ]; then
echo "$cmd_name: sorry - don't recognize the package type of '$pk'"
rc=1
elif [ ! -z "$type" ]; then
# echo "sysipk $type $opts --install $pk">&2
PATH="$dir_bcm_scripts:$PATH" sysipk $type $opts --install "$pk"
rc=$?
fi
done
fi
return $rc
}
sysidk_setup()
{ local scripts="$default_dir_bin"
local pkgs="$default_dir_pkg"
local rc=0
local cmd_sudo=sudo
if [ "_$1" = "_-l" -o "_$1" = "_--local" ]; then
cmd_sudo=
scripts="$default_dir_localbin"
scripts="$default_dir_localpkg"
fi
[ "_$1" = "_-p" -o "_$1" = "_--package" ] && { pkgs="$1"; shift; }
[ "_$1" = "_-b" -o "_$1" = "_--bin" ] && { scripts="$1"; shift; }
if [ ! -z "$cmd_sudo" ] && \
! type $cmd_sudo >/dev/null 2>/dev/null; then
echo "$cmd_name: sorry - this scripts needs a '$cmd_sudo' command to execute as root" >&2
elif ! $cmd_sudo mkdir -p "$scripts"; then
echo "$cmd_name: can't create a place for your scripts - do you need root access?">&2
rc=1
elif ! $cmd_sudo mkdir -p "$pkgs"; then
echo "$cmd_name: can't create a place for your packages - $pkgs">&2
rc=2
else
$cmd_sudo chgrp $sysidk_users "$pkgs" "$scripts"
$cmd_sudo chmod g+rw "$pkgs" "$scripts"
echo "$cmd_name: creating $dir_sysidk/$env_sysidk">&2
content_env "$scripts" "$pkgs" > "$dir_sysidk/$env_sysidk"
echo "$cmd_name: creating $dir_sysidk/$rc_sysidk">&2
content_rc > "$dir_sysidk/$rc_sysidk"
rc=$?
fi
if [ $rc -eq 0 -a $# -gt 0 ]; then
local script_pkg="$1"; shift
local unpack=
case "$script_pkg" in
*-bin.tgz) unpack="-xzf";;
*) ;;
esac
if [ -z "$unpack" ]; then
echo "$cmd_name: scripts package name has unrecognized format">&2
rc=3
else
echo "$cmd_name: unpacking scripts into $scripts">&2
tar -C "$scripts" $unpack "$script_pkg"
rc=$?
fi
fi
if [ $rc -eq 0 -a $# -gt 0 ]; then
. "$dir_sysidk/$env_sysidk"
sysidk_add "$@"
rc=$?
fi
return $rc
}
sysidk_install()
{ # update the user's shell RC script to include the scripts on the cmd
local remove=false
[ "_$1" = "_-d" ] && { shift; remove=true; }
local rc_file=".bashrc"
local firstline="#=== sysidk set-up"
local lastline="#=== end of sysidk set-up"
local rc=0
local tmp="/tmp/${cmd_name}_instrc"
case "$SHELL" in
*/bash) rc_file=.bashrc;;
*/dash) rc_file=.profile;;
*/sh) rc_file=.profile;;
# *csh) rc_file=.cshrc;; # not a bourne shell
*) rc_file=.bashrc
echo "$cmd_name: sorry don't recongize your SHELL '$SHELL' - updating $rc_file">&2
;;
esac
rc_file="$HOME/$rc_file"
# rc_file=test.rc
echo "$cmd_name: updating $rc_file";
if [ ! -r "$rc_file" ]; then
echo "$cmd_name: you haven't defined a $rc_file shell initialization file!">&2
rc=1
elif $remove; then
if grep "^$firstline" "$rc_file" >/dev/null; then
sed -itmp -e "/$firstline/,/$lastline/ d" "$rc_file"
rm -f "$rc_file.tmp"
else
echo "$cmd_name: no installation found in $rc_file">&2
fi
else
{ echo "$firstline"
echo "# (please leave '#===' lines) last updated `date`"
echo "source \"$dir_sysidk/$rc_sysidk\""
echo "$lastline"
} > $tmp
if grep "^$firstline" "$rc_file" >/dev/null; then
sed -itmp -e "/$lastline/ r $tmp" \
-e "/$firstline/,/$lastline/ d" "$rc_file"
rm -f "$rc_file.tmp"
else
# echo "$cmd_name: appended"
{ echo; cat $tmp; } >> "$rc_file"
fi
rm -f "$tmp"
fi
return $rc
}
rc=0
if [ "_$1" = "_--help" -o "_$1" = "_-h" ]; then
shift
help "$@" >&2
exit 1
elif ! setup_dir_sysidk; then
exit 1
fi
if [ "_$1" = "_--install" -o \
"_$1" = "_--uninstall" -o \
"_$1" = "_--setup" ]; then
op="$1"
shift
if [ "_$op" = "_--install" ]; then
sysidk_setup "$@"
rc=$?
[ $rc -eq 0 ] && {
sysidk_install "$@"
rc=$?
}
elif [ "_$op" = "_--uninstall" ]; then
sysidk_install -d "$@"
rc=$?
else
sysidk_setup "$@"
rc=$?
fi
exit $rc
fi
. "$dir_sysidk/$env_sysidk"
if [ "_$1" = "_--add" -o "_$1" = "_-a" ]; then
shift
if check_have_scripts && check_have_packages; then
sysidk_add "$@"
rc=$?
else
rc=2
fi
elif [ "_$1" = "_--bindir" -o "_$1" = "_-b" ]; then
echo "$dir_bcm_scripts"
test ! -z "$dir_bcm_scripts"
rc=$?
elif [ "_$1" = "_--pkgdir" -o "_$1" = "_-p" ]; then
echo "$DIR_BCM_RELEASES"
test ! -z "$DIR_BCM_RELEASES"
rc=$?
else
# run the command without requiring previous installation
if check_have_scripts && check_have_packages; then
PATH="$dir_bcm_scripts:$PATH" "$@"
rc=$?
else
rc=2
fi
fi
exit $rc