-
Notifications
You must be signed in to change notification settings - Fork 5
/
eos-pacdiff
executable file
·213 lines (186 loc) · 6.69 KB
/
eos-pacdiff
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
#!/bin/bash
# Pacdiff for EndeavourOS.
UserWants() {
case "$user_wants" in
cli | both) [ "$1" = "cli" ] && return 0 ;;
esac
which yad &>/dev/null || return 1
case "$user_wants" in
gui | both) [ "$1" = "gui" ] && return 0 ;;
esac
return 1
}
SafetyWarning() {
if [ "$EOS_PACDIFF_WARNING" = "yes" ] ; then
local txt=""
txt+="NOTE: <b>$progname</b> is a powerful tool, so use it carefully!\n\n"
txt+="For example, please do <i>not</i> modify files like:\n"
txt+="<tt> /etc/passwd</tt>\n"
txt+="<tt> /etc/group</tt>\n"
txt+="<tt> /etc/shadow</tt>\n"
txt+="and related files unless you know <i>exactly</i> what you are doing.\n\n"
txt+="You can remove this warning popup by changing the value of\n"
txt+="variable <tt>EOS_PACDIFF_WARNING</tt> in file <tt>/etc/eos-script-lib-yad.conf</tt>.\n"
txt+="(Note that you may need to merge the changes from\nfile /etc/eos-script-lib-yad.conf.pacnew first!)\n"
if UserWants cli ; then
txt2="$(echo "$txt" | sed -e 's|<[/bit]*>||g' -e 's|\\n|\n|g')"
printf "%s\n\n" "$txt2" >&2
fi
if UserWants gui ; then
eos_yad --form --image=$ICO_WARNING --title="Warning" --text="$txt" --button="yad-ok!!I understand":1
fi
fi
}
HasPacdiffs() { test -n "$(echo q | DIFFPROG=diff /usr/bin/pacdiff)" ; }
DoesSudo() { groups | grep -Pw 'root|wheel' >/dev/null ; }
HasBackupInView() {
[ "$BACKUP_VIEW" = "yes" ] || return
cat <<EOF >> $cmdfile
/usr/bin/sed -i $pacdiff -e "s|\(v\|V)\)|\1 test \\\$BACKUP -ne 0 \&\& \\\$SUDO cp -v \"\\\$file\" \"\\\$file.bak\"|"
EOF
}
HasBackupInMerge() {
[ "$BACKUP_MERGE" = "yes" ] || return
cat <<EOF >> $cmdfile
/usr/bin/sed -i $pacdiff -e "s|\(m\|M)\)|\1 test \\\$BACKUP -ne 0 \&\& \\\$SUDO cp -v \"\\\$file\" \"\\\$file.bak\"|"
EOF
}
PacdiffCmd() {
local differ="$1"
local prompt=""
local msg=""
local -r pacdiff="/usr/local/bin/pacdiff-eos" # will backup original file on view mode too (with option -b)
local bopt=""
if HasPacdiffs ; then
case "$differ" in # some differs may need options
vim) differ+=" -d" ;;
esac
msg="Starting pacdiff & $differ as root ..."
prompt="echo '$msg'"
local cmdfile=$(mktemp "$HOME/.tmp.$progname.tmpXXX") # don't mount $HOME with noexec!
local doas="/usr/bin/su-c_wrapper"
cat <<EOF > $cmdfile
#!/bin/bash
/usr/bin/cp /usr/bin/pacdiff $pacdiff
EOF
[ "$BACKUP_OVERWRITE" = "yes" ] && bopt="--backup" # may backup before overwrite
HasBackupInView # may backup before view too
HasBackupInMerge # may backup before merge too
if DoesSudo ; then
doas="/usr/bin/sudo"
echo "DIFFPROG=\"/usr/bin/$differ\" $pacdiff $bopt 2>/dev/null" >> $cmdfile
else
echo "DIFFPROG=\"/usr/bin/$differ\" $doas -p $pacdiff $bopt 2>/dev/null" >> $cmdfile
fi
echo "rm -f $pacdiff" >> $cmdfile
chmod u+x,go-rwx $cmdfile
if [ $start_new_terminal = yes ] ; then
RunInTerminal "$prompt ; $doas $cmdfile"
else
bash -c "$prompt ; $doas $cmdfile"
fi
rm -f $cmdfile
return $diffs_yes
else
msg="$progname: nothing to do."
if UserWants cli ; then
echo "$indent$msg" >&2
fi
if UserWants gui ; then
eos_yad_nothing_todo "<tt>$msg</tt>" 5
fi
[ $start_new_terminal = yes ] && return $diffs_no_nt || return $diffs_no
fi
}
Usage() {
cat <<EOF
Usage: $progname [options]
Options: -h, --help This help.
-b, --backup Save the old files (in view, merge, and overwrite modes) with .bak.
--nt Starts a new terminal for the output (used by the Welcome app).
--quiet Do not display messages.
--msg-types=WHAT Selects how informational messages will be displayed.
Supported values for WHAT:
"cli" Only using terminal CLI.
"gui" Only using popup windows.
"both" "cli" + "gui".
"" Do not display messages (same as --quiet).
Default: "both".
EOF
case "$1" in
OK) exit 0 ;;
FAIL) exit 1 ;;
esac
}
DIE() {
local linenr="$1"
local msg="$2"
case "$linenr" in
[1-9]*) linenr=" (line $linenr)" ;;
*) linenr="" ;;
esac
echo -e "\n==> $progname$linenr: error: $msg\n" >&2
Usage FAIL
}
Main()
{
local progname=${0##*/}
# exit codes
local diffs_yes=0
local diffs_error=1
local diffs_no=2
local diffs_no_nt=3
local start_new_terminal=no
local indent=""
local user_wants=both
local BACKUP_OVERWRITE=no
local BACKUP_VIEW=no
local BACKUP_MERGE=no
local -r extra_conf=/etc/$progname.conf
local arg
# pre-handling of certain options
for arg in "$@" ; do
case "$arg" in
--quiet) user_wants="" ;;
--msg-types=*) user_wants="${1#*=}" ;; # cli gui both ""
esac
done
source /usr/share/endeavouros/scripts/eos-script-lib-yad || return $diffs_error
export -f eos_yad
export -f eos_yad_nothing_todo
AssignIconVariables_in_eos_bash_shared
# read config file if it exists; command line options will override config file values
if [ -e $extra_conf ] ; then
case "$user_wants" in
cli | both) echo "==> reading $extra_conf" >&2 ;;
esac
source $extra_conf
else
echo "==> info: file $extra_conf is not available." >&2
fi
# handle rest of the options
while [ "$1" ] ; do
case "$1" in
-h | --help) Usage OK ;;
-b | --backup) BACKUP_OVERWRITE=yes
BACKUP_VIEW=yes
BACKUP_MERGE=yes
;;
--indent=*) indent="${1#*=}" ;;
--nt) start_new_terminal=yes ;;
--quiet) ;; # user_wants="" ;;
--msg-types=*) ;; # user_wants="${1#*=}" ;; # cli gui both ""
-*) DIE $LINENO "option '$1' not supported." ;;
esac
shift
done
SafetyWarning
# see /etc/eos-script-lib-yad.conf about EOS_WELCOME_PACDIFFERS
for differ in "${EOS_WELCOME_PACDIFFERS[@]}" ; do
if [ -x /usr/bin/$differ ] ; then
PacdiffCmd "$differ"
return $?
fi
done
}
Main "$@"