-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathehooks_check.sh
executable file
·153 lines (123 loc) · 4.08 KB
/
ehooks_check.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
#!/bin/bash
color_norm=$(tput sgr0)
color_bold=$(tput bold)
color_blue=$(tput setaf 4)
color_cyan=$(tput setaf 6)
[[ -z $1 || $# -gt 1 ]] && set -- "-h"
## Manual.
case $1 in
-c|--check)
true
;;
-r|--reset)
reset="yes"
;;
*)
echo "${color_blue}${color_bold}NAME${color_norm}"
echo " ehooks - generate emerge command needed to apply ebuild hooks changes"
echo
echo "${color_blue}${color_bold}SYNOPSIS${color_norm}"
echo " ${color_blue}${color_bold}ehooks${color_norm} [${color_cyan}OPTION${color_norm}]"
echo
echo "${color_blue}${color_bold}DESCRIPTION${color_norm}"
echo " It looks for ebuild hooks changes and generates emerge command needed to apply the changes."
echo " /usr/bin/${color_blue}${color_bold}ehooks${color_norm} is a symlink to /var/lib/layman/unity-gentoo/ehooks_check.sh script."
echo
echo "${color_blue}${color_bold}OPTIONS${color_norm}"
echo " ${color_blue}${color_bold}-c${color_norm}, ${color_blue}${color_bold}--check${color_norm}"
echo " Generate emerge command when changes found."
echo
echo " ${color_blue}${color_bold}-r${color_norm}, ${color_blue}${color_bold}--reset${color_norm}"
echo " Set changes as applied (reset modification time)."
echo
exit 1
esac
count=0
## Progress indicator.
indicator() {
local -a arr=( "|" "/" "-" "\\" )
[[ ${count} -eq 4 ]] && count=0
printf "\b\b %s" "${arr[${count}]}"
count=$((count + 1))
}
printf "%s " "Looking for ebuild hooks changes"
indicator
repo_root="$(/usr/bin/portageq get_repo_path / unity-gentoo)"
wcard="*/*/"
arr=( "nzero" )
## Get all ebuild hooks (sub)directories.
while [[ -n ${arr[@]} ]]; do
indicator
prev_shopt=$(shopt -p nullglob)
shopt -s nullglob
arr=( "${repo_root}"/profiles/ehooks/${wcard} )
${prev_shopt}
ehk+=( "${arr[@]}" )
wcard+="*/"
done
sys_db="/var/db/pkg/"
EHOOK_UPDATE=()
for x in "${ehk[@]}"; do
indicator
## Get ${CATEGORY}/{${P}-${PR},${P},${P%.*},${P%.*.*},${PN}} from ebuild hook's path.
m=${x%%/files/*}
m=${m%/}
m=${m#*/ehooks/}
## Get ${SLOT}.
[[ ${m} == *":"+([0-9.]) ]] && slot="${m#*:}" || slot=""
m="${m%:*}"
## Get installed packages affected by the ebuild hook.
prev_shopt=$(shopt -p nullglob) ## don't use extglob
shopt -s nullglob
[[ -d ${sys_db}${m} ]] && pkg=( "${sys_db}${m}" ) || pkg=( "${sys_db}${m}"{-[0-9],.[0-9],-r[0-9]}*/ )
${prev_shopt}
for n in "${pkg[@]}"; do
## Try another package if modification time is newer or equal than the ebuild hook's (sub)directory time.
sys_date=$(date -r "${n}" "+%s")
[[ ${sys_date} -ge $(date -r "${x}" "+%s") ]] && continue
## Try another package if slots differ.
[[ -z ${slot} ]] || fgrep -qsx "${slot}" "${n}/SLOT" || continue
## Try another package if ehook_require USE-flag is not declared.
req=$(egrep -hos -m 1 "ehook_require\s[A-Za-z0-9+_@-]+" "${x%%/files/*}/"*.ehook | head -1)
[[ -z ${req} ]] || portageq has_version / unity-extra/ehooks["${req/ehook_require }"] || continue
## Set ebuild hook's modification time equal to package's time when --reset option given.
[[ -n ${reset} ]] && touch -m -t $(date -d @"${sys_date}" +%Y%m%d%H%M.%S) "${x}" 2>/dev/null && reset="applied" && continue
## Get ownership of file when 'touch: cannot touch "${x}": Permission denied' and quit.
[[ -n ${reset} ]] && reset=$(stat -c "%U:%G" "${x}") && break 2
## Get =${CATEGORY}/${PF} from package's ${sys_db} path.
n="${n%/}"
n="${n#${sys_db}}"
EHOOK_UPDATE+=( "=${n}" )
done
done
printf "\b\b%s\n\n" "... done!"
einfo() {
local color_green=$(tput setaf 2)
echo " ${color_green}${color_bold}*${color_norm} $*"
}
ewarn() {
local color_yellow=$(tput setaf 3)
echo " ${color_yellow}${color_bold}*${color_norm} $*"
}
if [[ -n ${EHOOK_UPDATE[@]} ]]; then
# Remove duplicates.
EHOOK_UPDATE=( $(printf "%s\n" "${EHOOK_UPDATE[@]}" | sort -u) )
ewarn "Rebuild the packages affected by the ebuild hooks changes:"
ewarn "emerge -1 ${EHOOK_UPDATE[@]}"
else
case ${reset} in
"")
einfo "No rebuild needed"
;;
applied)
einfo "Reset applied"
;;
yes)
einfo "Reset not needed"
;;
*)
ewarn "Permission denied to apply reset (ownership ${reset})"
;;
esac
fi
echo