-
Notifications
You must be signed in to change notification settings - Fork 0
/
logbook-installer
executable file
·204 lines (186 loc) · 5.22 KB
/
logbook-installer
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
#!/bin/bash
#
# Copyright (C) 2022 by Benedikt Brinkmann
# Authors:
# Benedikt Brinkmann <benedikt@thinkbot.de>
#
# Based on the logbuch-tools from Intevation GmbH
# https://hg.intevation.de/adminton
#
# This program is free software under the GPL (>=v3)
# Read the file COPYING coming with the software for details.
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
LOGBOOK="${LOGBOOK:-/etc/logbook.txt}"
TMPFILE1=""
TMPFILE2=""
init() {
if [ ! -f "$LOGBOOK" ] ; then
{
echo "Logbook for ${LOGBOOK_HOST:-`hostname`}"
echo
echo -e "`date +'%Y.%m.%d'`\t${LOGBOOK_USER:-${EMAIL:-${LOGNAME:-?}}}"
echo "* $LOGBOOK created"
echo "* Installed Packages:"
xbps-query -l | awk '$1 ~ /^ii$/ { print " ", $2 }'
} >> "$LOGBOOK" || exit $?
fi
VI="`type -p vim`"
test "$VI" || VI="`type -p vi`"
EMACS="`type -p emacs`"
if [ -z "$VI" -a -z "$EMACS" ] ; then
echo "Please install 'vim', another vi-like editor or the emacs." >&2
exit 1
fi
if [ ! -w "$LOGBOOK" ] ; then
echo "Error: no write access to $LOGBOOK" >&2
exit 1
fi
}
# format_diff pre-install-pkg-list post-install-pkg-list
format_diff() {
diff --unchanged-group-format='' \
--changed-group-format='@UPDATE:
%<@---
%>@END UPDATE
' \
--new-group-format='@NEW:
%>@END NEW
' \
--old-group-format='@REMOVED:
%<@END REMOVED
' \
--old-line-format='- %l
' \
--new-line-format='+ %l
' \
"$1" "$2" | \
awk 'function reset()
{
oi=0; delete opkgs;
ni=0; delete npkgs;
}
/^@UPDATE:/{ prefix=" Updated"; reset() }
/^@---/{ }
/^@END UPDATE/{ for (i in npkgs) { print prefix, npkgs[i], "<= (" opkgs[i] ")" }}
/^@NEW:/{ prefix=" Installed"; reset() }
/^@END NEW/{ for (i in npkgs) { print prefix, npkgs[i]}}
/^@REMOVED:/{ prefix=" Removed"; reset() }
/^@END REMOVED/{ for (i in opkgs) { print prefix, opkgs[i]}}
/^-/{opkgs[++oi]=$2;}
/^\+/{npkgs[++ni]=$2;}
'
}
# do_n_log options
#
# Invoke "xbps-" with $options and put changes to the package list in
# the logbook.
do_n_log() {
TMPFILE2=`tempfile "$TMPFILE2"` || exit 1
TMPFILE3=`tempfile "$TMPFILE3"` || exit 1
xbps-query -l | awk '$1 ~ /^ii$/ { print $2 }' > "$TMPFILE2"
xbps-"$@"
xbps-query -l | awk '$1 ~ /^ii$/ { print $2 }' > "$TMPFILE3"
cmp -s "$TMPFILE2" "$TMPFILE3" && return 1
{
echo -e "`date +'%Y.%m.%d'`\t${LOGBOOK_USER:-${EMAIL:-${LOGNAME:-?}}}"
echo -n "* xbps-$@"
if ! cmp -s "$TMPFILE2" "$TMPFILE3" ; then
echo ":"
format_diff "$TMPFILE2" "$TMPFILE3"
else
echo
fi
echo
} > "$TMPFILE1"
}
edit_logbook() {
unset DISPLAY
DATEPATTERN='^[0-9]*\.[0-9]*\.[0-9]*'
if [ "$EMACS" -a \( -z "$VI" -o "$EDITOR" = "emacs" \) ] ; then
$EMACS -eval "(progn (find-file \"$LOGBOOK\")
(search-forward-regexp \"^[0-9]+\\\\.[0-9]+\\\\.[0-9]+\")
(move-beginning-of-line 1)
(insert-file-contents \"$TMPFILE1\"))"
else
$VI -c "1;/$DATEPATTERN/-1;r $TMPFILE1" "$LOGBOOK"
fi
}
delete_tmpfile() {
if [ -n "$TMPFILE1" ] ; then
rm -f "$TMPFILE1" && TMPFILE1=""
fi
if [ -n "$TMPFILE2" ] ; then
rm -f "$TMPFILE2" && TMPFILE2=""
fi
}
cleanup() {
echo "Aborted."
delete_tmpfile
exit 0
}
tempfile() {
if [ -n "$1" ] ; then
echo "$1"
else
mktemp "/tmp/logbook.XXXXXXXXXX"
fi
}
init "$@"
TMPFILE1=`tempfile "$TMPFILE1"` && {
trap "cleanup" 1 2 3 7 10 13 15
case "`basename \"$0\"`" in
logbook)
{
echo -e "`date +'%Y.%m.%d'`\t${LOGBOOK_USER:-${EMAIL:-${LOGNAME:-?}}}"
echo "* $@"
echo
} > "$TMPFILE1"
edit_logbook
;;
xbpsupg)
do_n_log install -Su && \
edit_logbook
;;
xbpsins)
do_n_log install -S "$@" && \
edit_logbook
;;
xbpsrm)
do_n_log remove "$@" && \
edit_logbook
;;
xbpsrdp)
do_n_log remove -R "$@" && \
edit_logbook
;;
xbpsrop)
do_n_log remove -o && \
edit_logbook
;;
logbook-installer)
INSTALL_PATH="${1:-/usr/local/bin}"
REMOVE_PATH="/usr/local/sbin"
TOOLS="xbpsupg xbpsins xbpsrm xbpsrdp xbpsrop"
echo "Installing logbook tools to $INSTALL_PATH"
cp "$0" "$INSTALL_PATH/logbook"
chmod 755 "$INSTALL_PATH/logbook"
for tool in $TOOLS; do
[ $# -eq 0 -a -e "$REMOVE_PATH/$tool" ] && rm "$REMOVE_PATH/$tool"
ln -sf logbook "$INSTALL_PATH/$tool"
done
[ $# -eq 0 -a -e "$REMOVE_PATH/logbook" ] && rm "$REMOVE_PATH/logbook"
{
echo -e "`date +'%Y.%m.%d'`\t${LOGBOOK_USER:-${EMAIL:-${LOGNAME:-?}}}"
echo "* Logbook tools created in $INSTALL_PATH"
echo
} > "$TMPFILE1"
edit_logbook
;;
*)
echo "Not implemented: $0" >&2
exit 1
;;
esac
delete_tmpfile
}
exit 0