forked from slowpeek/apt-undo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapt-undo.sh
executable file
·184 lines (144 loc) · 3.77 KB
/
apt-undo.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
#!/bin/bash
# Walk backwards through "^Install: " lines in apt log and show
# numbered summaries of installed packages per action. With "-iN" show
# a full list of installed packages for the line N. Add "-q" to
# suppress total number of packages message. The message is written to
# stderr so it doesnt corrupt the package list anyways.
#
# Example: get list of packages to remove to undo last apt install
#
# apt-undo.sh -i1
#
bye () {
echo $@ >&2
exit 1
}
usage () {
cat <<EOF
${0##*/} [-iN] [-q] [-zN | -f <path>] [-h]
>> use -iN to show full list of packages for line N
>> use -f <path> to supply custom log. '-' states for stdin
>> use -zN to operate on N'th rotated log in default location
>> use -q with -iN to suppress total number of packages message
-z and -f are mutually exclusive.
EOF
}
is_int () {
test "$1" -eq "$1" 2>/dev/null
}
is_natural () {
is_int "$1" && [[ $1 -gt 0 ]]
}
smart_zcat() {
local cmd=cat
if [[ $1 == *.gz ]]; then
cmd=zcat
fi
$cmd "$1"
}
log_lines () {
tac |\
grep -oP '(?<=^Install: ).+' |\
sed -r 's/\([^)]+\)//g;s/ *, */ /g'
}
narg () {
echo $#
}
make_hint () {
local l=68 total=$#
local -a hint
while [[ $# -gt 0 ]]; do
((l -= ${#1} + 1))
if [[ $l -lt 0 ]]; then
hint+=("+$(( $total - ${#hint[@]} ))")
break
fi
hint+=($1)
shift
done
echo ${hint[@]}
}
main () {
local quiet index=0 prefix=/var/log/apt
# termux
if [[ -n $PREFIX ]]; then
prefix="$PREFIX"$prefix
fi
local log=$prefix/history.log
local arg opt_set=
while getopts ':i:z:f:qh' arg; do
opt_set=${opt_set}$arg
if [[ $opt_set == *z* && $opt_set == *f* ]]; then
bye -z and -f are mutually exclusive
fi
case $arg in
i)
index=$OPTARG
if ! is_natural "$index"; then
bye -i value must be natural
fi
;;
z)
local t=$OPTARG
if ! is_natural "$t"; then
bye -z value must be natural
fi
log=$prefix/history.log.$t.gz
if [[ ! -f $log ]]; then
bye Rotated log \#$t doesnt exist
fi
unset t
;;
f)
log=$OPTARG
if [[ -z $log ]]; then
bye Empty -f value
fi
if [[ $log != - ]]; then
local t=$(readlink -e "$log")
if [[ -z $t ]]; then
bye File not available: "$log"
fi
if [[ ! -f $t || ! -r $t ]]; then
bye Not a readable file: "$log"
fi
unset t
fi
;;
q)
quiet=y
;;
h)
usage
exit
;;
:)
bye Option requires value: -$OPTARG
;;
\?)
bye Unknown option: -$OPTARG
;;
esac
done
unset arg opt_set
smart_zcat "$log" | log_lines | {
if [[ $index -gt 0 ]]; then
line=$(sed -n ${index}p)
if [[ -z $line ]]; then
bye -i value is too big
exit
fi
if [[ -z $quiet ]]; then
echo Total number of packages: $(narg $line) >&2
fi
echo $line
else
n=0
while read line; do
printf "%-4d" $((++n))
make_hint $line
done
fi
}
}
main "$@"