-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkguse
262 lines (260 loc) · 6.46 KB
/
pkguse
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
#!/bin/sh
#
# @(#) pkguse V1.19 (C) 2019 by Roman Oreshnikov
#
URL=http://crux/CRUX-3.5
HLP="Synchronize ${URL##*/} packages with MasterHost
Usage: pkguse [options] [packages]
Options:
-d Remove specified packages
-f Force installation
-h Display this text
-i Ignore inverse dependencies
-k Install kernel
-l List of files that do not belong to installed packages
-r ROOT Specify alternative installation root
-s Synchronize packages, default: compare
-u URL MasterHost URL, default: $URL
Report bugs to <r.oreshnikov@gmail.com>"
Msg() { echo pkguse: $* >&2; }
Err() { Msg $*; exit 1; }
End() { trap 0; [ -z "$WRK" ] || /bin/rm -rf $WRK; [ $# = 0 ] || Err $*; exit; }
Add() { ADD=; for V do [ $V = $N ] && NEW=$NEW\ $V || ADD=$ADD\ $V; done; }
Del() { DEL=; for V do [ $V = $R ] || DEL=$DEL\ $V; done; }
Pkg() { PKG=; for V do V=${V%#*}; [ $V = $N ] || PKG=$PKG\ $V; done; }
Chk() {
Pkg $PKG
case " $DEL " in *\ $N\ *) return;; esac
CHK=$CHK\ $N
Add $ADD
for R in $R; do
case " $CHK $PKG " in *\ $R\ *);; *) PKG=$PKG\ $R;; esac
case " $DEL " in
*\ $R\ *) Msg Unable to remove $R, required for $N; Del $DEL
esac
done
}
Get() {
[ -z "$WRK" -o -z "$SRC" ] || /bin/rm $SRC
SRC=${WRK:-$URL}/$1${2:+#$2}
[ -z "$WRK" ] || /usr/bin/wget -qO "$SRC" "$URL/$1${2:+%23$2}" || SRC=
[ -n "$SRC" -a -r "$SRC" ] || Err Failure to get $1${2:+#$2}
}
Who() {
U=$1 u=$2; shift 2
case $1 in ''|/*) G=$U g=$u;; *) G=$1 g=$2; shift 2;; esac
[ $# != 0 ] && H=$1 && shift || H=/
[ -f "$DST/etc/passwd" ] &&
T=$(/bin/sed "/^$G:[^:]*:$g:/!d;s/.*/1/;q" "$DST/etc/group"
/bin/sed "/^$U:.:$u:$g:/!d;s/.*/2/;q" "$DST/etc/passwd"
/bin/sed "/^$U:/!d;s/.*/3/;q" "$DST/etc/shadow") || T=
case $T in
1*2*3) return;;
'') Msg Creating user $U;;
*) Msg User $U created with errors
esac
[ -z "$MOD" ] && return || [ -z "$T" ]
if [ -x /usr/sbin/useradd ]; then
/usr/sbin/groupadd -r ${DST:+-R "$DST"} -g $g $G
/usr/sbin/useradd -r ${DST:+-R "$DST"} -u $u -g $G -d $H \
${*:+-c "$*"} $U
else
echo "$G::$g:" >>"$DST/etc/group"
echo "$U:x:$u:$g:$*:$H:" >>"$DST/etc/passwd"
echo "$U:x:0::::::" >>"$DST/etc/shadow"
fi
}
Put() { /usr/bin/pkgadd $1 ${DST:+-r "$DST"} ${U:+-u} "$SRC"; }
Exe() {
echo $1
[ -z "$MOD" ] && return || shift
Get $N $V$EXT
Put $OPT && return || [ -z "$OPT" ]
Msg "Force install the package? [N/y/all] \c"
read A <&2
case $A in [Yy]);; [Aa]ll) OPT=-f;; *) exit 1;; esac
Put -f
}
Run() {
echo $1
[ -z "$MOD" ] && return || shift
Get $@
/bin/sh "$SRC" ${DST:+"$DST"} && return ||
Msg "$1 failed, continue? [Y/n] \c"
read A <&2
case $A in ""|[Yy]);; *) exit 1;; esac
}
Lst() {
WRK=
cd ${DST:-/}
/usr/bin/find ./ -mount 2>/dev/null | /usr/bin/awk '
/^\./ {
sub(/^\.\/*/, "")
if($0 in L) { delete L[$0]; next }
switch($0) {
case /^(boot|home|root)\//:
case /^etc\/ssh\/ssh_host_.*_key(\.pub)?$/:
case /^lib\/modules\//:
case /^lost\+found\/?/:
case /^usr\/(ports|src)\/?/:
case /^usr\/share\/fonts\/X11\/.+\/fonts\.(dir|scale)$/:
case /^var\/(cache|lib|log|run|spool)\//:
case /^(lib64)?$/: next
}
F["/" $0]
next
}
/\// { sub(/\/$/, ""); L[$0] }
END {
delete L["lib64"]
print "### List of files not included in packages:"
asorti(F); for(p in F) print F[p]
print "### List of missing files included in packages:"
asorti(L); for(p in L) print L[p]
}' $DBD -
End
}
ADD=y
ALL=
CHK=
DBD=/var/lib/pkg/db
DEL=
DST=
EXT=
IGN=
MOD=
NEW=
OPT=
PKG=
REL=
RUN=${URL##*/}
SRC=
USE=
WRK=/tmp/.pkguse$$
while getopts dfhiklr:su: V; do
case $V in
d) ADD=;;
f) OPT=-f;;
h) echo "$HLP"; exit 0;;
i) IGN=y;;
k) REL=$RUN;;
l) ALL=y;;
r) DST=$OPTARG;;
s) MOD=synchronized\ with;;
u) URL=${OPTARG:-.};;
?) Err '\rUsage: pkguse [-r root] [-u url] [-dfikls] [pkg ...]'
esac
done
shift $(($OPTIND - 1))
[ -n "$ADD" -o $# != 0 ] || Err Option -d requires a list of packages!
if [ -n "$DST" ]; then
[ -d "$DST" ] || Err $DST - no such directory!
case $DST in /*);; *) DST=$(pwd -P)/$DST;; esac
DBD=$DST$DBD
fi
case $URL in
http://[a-z0-9]*|https://[a-z0-9]*|ftp://[a-z0-9]*);;
*) [ -d "$URL" ] || Err $URL - no such directory!; WRK=
esac
for V do
case -$V- in
*--*|*[!a-zA-Z0-9_+-]*|-[0-9_+]*) Err Invalid package name $V!
esac
case " $PKG " in *\ $V\ *);; *) PKG=$PKG\ $V;; esac
done
set -e
trap "End Terminated due to error!" 0
trap "End Interrupted by signal!" 1 2 3 13 15
[ -z "$ALL" ] || Lst
Msg Packages are ${MOD:-compared to} $URL${DST:+ for $DST}
[ ! -f "$DBD" ] || USE=$(/bin/sed '${x;s/\n/ /g;q}
/\//d;/^$/d;/^lib64/d;N;s/\n/#/;H;d' "$DBD")
[ -n "$ADD" ] && ADD=$PKG ||
for P in $PKG; do
case " $USE " in
\ \ );;
*\ $P#*) DEL=$DEL\ $P;;
*) Msg Unable to remove uninstalled package $P
esac
done
[ -n "$USE$ADD" ] || End Empty list of packages to work!
[ -z "$WRK" ] || /bin/mkdir $WRK
Get PKG.use
PKG=$USE\ $ADD
while :; do
ALL= RUN= WHO=:
exec <$SRC
while read P R; do
case $P in
+) RUN=$R\ $RUN;;
@) WHO=:$R\ $WHO;;
.*) EXT=$P;;
*) ALL=$P\ $ALL N=${P%#*}
case " $PKG " in *\ $N[#\ ]*) Chk;; esac
esac
done
[ -n "$PKG" ] || break
for N in $PKG; do
case " $ALL " in *\ $N#*) continue;; esac
case " $ADD " in
*\ $N\ *) Msg Unable to install unknown package $N
esac
Pkg $PKG
done
ADD=
done
[ -n "$CHK" ] || End Nothing to do!
[ -z "$MOD" -o -n "$USE" ] || ! /bin/mkdir -p "${DBD%/*}" || echo >$DBD
for P in $USE; do
P=${P%#*}
case " $CHK " in
*\ $P\ *);;
*) echo Remove $P; [ -z "$MOD" ] || /usr/bin/pkgrm ${DST:+-r "$DST"} $P
esac
done
for P in $ALL; do
N=${P%#*}
case " $CHK " in *\ $N\ *);; *) continue;; esac
case " $USE " in *\ $P\ *) [ -n "$DST" ] || continue;; esac
case " $NEW " in *\ $N\ *);; *) NEW=$NEW\ $N;; esac
done
if [ -z "$IGN$DST" ]; then
exec <"$SRC"
while read P R; do
N=${P%#*}
case " $CHK " in
*\ $N\ *)
for P in $R; do
case " $NEW " in
*\ $N\ *) break;;
*\ $P\ *) NEW=$NEW\ $N; break
esac
done
esac
done
fi
for P in $ALL; do
case $P in [a-z]*) N=${P%#*};; *) continue;; esac
case " $NEW " in *\ $N\ *) V=${P#*#};; *) continue;; esac
case " $WHO " in *\ :$N\ *) U=${WHO#*:$N } U=${U%%:*}; Who $U;; esac
case " $USE " in
*\ $N#*) U=${USE#* $N#} U=${U%% *}; Exe "Upgrade $N $U to $V";;
*) U=; Exe "Install $N $V"
esac
case " $RUN " in
*\ $N\ *) Run "Post-install $N $V" $N $V.post-install
esac
done
if [ -n "$REL" ]; then
U=-kernel.run
if N=$(/bin/uname -n 2>/dev/null); then
N=$REL-$N P=$URL/$N$U
case $WRK in
"") [ -f "$P" ] || N=;;
*) /usr/bin/wget -q --spider "$P" || N=
esac
fi
Run "Install kernel ${N:-$REL}" "${N:-$REL}$U"
fi
Msg Done
End