-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaur.sh
More file actions
executable file
·271 lines (215 loc) · 8.99 KB
/
saur.sh
File metadata and controls
executable file
·271 lines (215 loc) · 8.99 KB
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
263
264
265
266
267
268
269
270
271
#!/bin/bash
# --- Constants ---
RED='\033[0;31m'
BLUE='\033[0;94m'
NC='\033[0m'
SAUR_DIR="$HOME/.config/saur"
INSTALLED_LIST="$SAUR_DIR/installed.list"
CACHE_DIR="$HOME/.cache/saur/pkgb"
mkdir -p "$SAUR_DIR" "$CACHE_DIR"
touch "$INSTALLED_LIST"
# --- Helper Functions ---
confirm() {
local prompt="$1"
local response
read -rp "$prompt [y/N]: " response
if [[ "$response" != [yY] ]]; then
echo "Aborting ..."
exit 1
fi
}
run_in_container() {
# Mount current directory inside container as /src
podman run --rm -it \
-v "$(pwd)":/src:z \
archlinux:latest /bin/bash -c "
pacman -Sy --noconfirm base base-devel linux linux-firmware &&
cd /src &&
./saur --inside-container $*"
}
check_maintainer_change() {
pkg="$1"
cache_file="$SAUR_DIR/maintainers.list"
current_maintainer=$(curl -fsSL "https://aur.archlinux.org/rpc/v5/info/$pkg" | jq -r '.results[0].Maintainer')
if grep -q "^$pkg:" "$cache_file" 2>/dev/null; then
old_maintainer=$(grep "^$pkg:" "$cache_file" | cut -d':' -f2-)
if [[ "$old_maintainer" != "$current_maintainer" ]]; then
echo -e "${RED}WARNING: Maintainer has changed from $old_maintainer to $current_maintainer!${NC}"
confirm "Do you wish to proceed despite the maintainer change?"
fi
else
echo "First time seeing $pkg, caching maintainer info."
fi
# Update cache
grep -v "^$pkg:" "$cache_file" 2>/dev/null > "$cache_file.tmp"
echo "$pkg:$current_maintainer" >> "$cache_file.tmp"
mv "$cache_file.tmp" "$cache_file"
}
fetch_safety_card() {
pkg="$1"
echo -e "${RED}Fetching package metadata from AUR...${NC}"
json=$(curl -fsSL "https://aur.archlinux.org/rpc/v5/info/$pkg")
if [[ -z "$json" || "$json" == "null" ]]; then
echo "Failed to fetch package info."
return
fi
maintainer=$(echo "$json" | jq -r '.results[0].Maintainer')
submitted=$(echo "$json" | jq -r '.results[0].FirstSubmitted' | xargs -I{} date -d @{} "+%Y-%m-%d" 2>/dev/null || date -r {} "+%Y-%m-%d")
updated=$(echo "$json" | jq -r '.results[0].LastModified' | xargs -I{} date -d @{} "+%Y-%m-%d" 2>/dev/null || date -r {} "+%Y-%m-%d")
votes=$(echo "$json" | jq -r '.results[0].NumVotes')
popularity=$(echo "$json" | jq -r '.results[0].Popularity')
echo -e "\n${RED}==== SAFETY CARD ====${NC}"
echo -e "${BLUE}Package:${NC} $pkg"
echo -e "${BLUE}Maintainer:${NC} $maintainer"
echo -e "${BLUE}Submitted:${NC} $submitted"
echo -e "${BLUE}Last Update:${NC}$updated"
echo -e "${BLUE}Votes:${NC} $votes"
echo -e "${BLUE}Popularity:${NC} $popularity"
echo -e "${RED}=====================${NC}"
confirm "Do you trust this?"
}
show_pkgbuild_diff() {
local pkg="$1"
local latest_pkgbuild_file="$2"
local cached_pkgbuild="$CACHE_DIR/$pkg.PKGBUILD"
if [[ ! -f "$cached_pkgbuild" ]]; then
echo "No cached PKGBUILD found for $pkg, treating as new install."
return 0
fi
diff -u "$cached_pkgbuild" "$latest_pkgbuild_file" | less
}
cache_current_pkgbuild() {
cp "$2" "$CACHE_DIR/$1.PKGBUILD"
}
fetch_aur_deps() {
local missing_aur_deps=()
mapfile -t raw_deps < <(makepkg -o --syncdeps --nobuild 2>&1 | awk '/^ ->/ { print $2 }')
missing_aur_deps=()
# Check if package is in official repos
for pkg in "${raw_deps[@]}"; do
if ! pacman -Si "$pkg" &>/dev/null; then
missing_aur_deps+=("$pkg")
else
echo "Skipping $pkg because it is in the official repos"
fi
done
if [[ ${#missing_aur_deps[@]} -gt 0 ]]; then
maintainer_changed=()
cache_file="$SAUR_DIR/maintainers.list"
printf "\n${RED}==== AUR Dependency Summary ====${NC}\n"
printf "%-20s | %-15s | %-10s | %-6s | %-10s | %-10s\n" "Package" "Maintainer" "Updated" "Votes" "Popularity" "Submitted"
printf -- "---------------------------------------------------------------\n"
for dep in "${missing_aur_deps[@]}"; do
json=$(curl -fsSL "https://aur.archlinux.org/rpc/v5/info/$dep")
maintainer=$(echo "$json" | jq -r '.results[0].Maintainer')
submitted_epoch=$(echo "$json" | jq -r '.results[0].FirstSubmitted')
submitted=$(date -d "@$submitted_epoch" +"%Y-%m-%d" 2>/dev/null || date -r "$submitted_epoch" +"%Y-%m-%d")
last_update_epoch=$(echo "$json" | jq -r '.results[0].LastModified')
last_update_date=$(date -d "@$last_update_epoch" +"%Y-%m-%d" 2>/dev/null || date -r "$last_update_epoch" +"%Y-%m-%d")
votes=$(echo "$json" | jq -r '.results[0].NumVotes')
popularity=$(echo "$json" | jq -r '.results[0].Popularity')
printf "%-20s | %-15s | %-10s | %-6s | %-10s | %-10s\n" "$dep" "$maintainer" "$last_update_date" "$votes" "$popularity" "$submitted"
# Now check maintainer change for this dep:
old_maintainer=""
if grep -q "^$dep:" "$cache_file" 2>/dev/null; then
old_maintainer=$(grep "^$dep:" "$cache_file" | cut -d':' -f2-)
fi
if [[ "$old_maintainer" != "" && "$old_maintainer" != "$maintainer" ]]; then
maintainer_changed+=("$dep: $old_maintainer -> $maintainer")
fi
# Update cache with current maintainer for this dep
grep -v "^$dep:" "$cache_file" 2>/dev/null > "$cache_file.tmp"
echo "$dep:$maintainer" >> "$cache_file.tmp"
mv "$cache_file.tmp" "$cache_file"
done
printf "${RED}=================================${NC}\n"
if [[ ${#maintainer_changed[@]} -gt 0 ]]; then
echo -e "\n${RED}WARNING: The following packages have changed maintainers:${NC}"
for entry in "${maintainer_changed[@]}"; do
echo " - $entry"
done
confirm "Do you wish to proceed despite these maintainer changes?"
fi
confirm "Do you wish to review and install these AUR dependencies?"
read -rp "Do you want to skip viewing PKGBUILDs for dependencies? [y/N]: " skip_view
for dep in "${missing_aur_deps[@]}"; do
echo
echo -e "${RED}Handling AUR dependency: $dep${NC}"
if [[ "$skip_view" != [yY] ]]; then
echo -e "${RED}Viewing PKGBUILD for $dep${NC}\n"
read -n 1 -s -r -p "Press any key to continue..."
curl -fsSL "https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD?h=$dep" | bat --language=bash
echo
confirm "Have you verified that the PKGBUILD is safe and wish to continue?"
fi
tmpdir=$(mktemp -d)
git clone "https://aur.archlinux.org/$dep.git" "$tmpdir/$dep"
cd "$tmpdir/$dep"
makepkg -si
sed -i "/^$dep /d" "$INSTALLED_LIST"
version=$(pacman -Q "$dep" | awk '{print $2}')
echo "$dep $version" >> "$INSTALLED_LIST"
cp PKGBUILD "$CACHE_DIR/$dep.PKGBUILD"
cd - >/dev/null
rm -rf "$tmpdir"
done
fi
}
update() {
echo -e "${RED}Checking for AUR updates...${NC}"
while read -r pkg current_version; do
echo "Checking $pkg..."
pkginfo=$(curl -fsSL "https://aur.archlinux.org/rpc/v5/info/$pkg")
upstream_version=$(echo "$pkginfo" | jq -r '.results[0].Version')
maintainer=$(echo "$pkginfo" | jq -r '.results[0].Maintainer')
last_update_epoch=$(echo "$pkginfo" | jq -r '.results[0].LastModified')
last_update_date=$(date -d "@$last_update_epoch" +"%Y-%m-%d" 2>/dev/null || date -r "$last_update_epoch" +"%Y-%m-%d")
if [[ "$upstream_version" != "$current_version" ]]; then
echo
echo -e "Update available for $pkg: $current_version -> $upstream_version"
echo "Maintainer: $maintainer"
echo "Last Updated: $last_update_date"
tmpdir=$(mktemp -d)
git clone --depth=1 "https://aur.archlinux.org/$pkg.git" "$tmpdir/$pkg"
echo "Showing PKGBUILD diff..."
show_pkgbuild_diff "$pkg" "$tmpdir/$pkg/PKGBUILD"
confirm "Do you wish to build and install $pkg $upstream_version after reviewing the diff?"
cd "$tmpdir/$pkg" || continue
makepkg -si || { echo "Failed to build $pkg"; continue; }
sed -i "/^$pkg /d" "$INSTALLED_LIST"
echo "$pkg $upstream_version" >> "$INSTALLED_LIST"
cache_current_pkgbuild "$pkg" "$tmpdir/$pkg/PKGBUILD"
rm -rf "$tmpdir"
fi
done < "$INSTALLED_LIST"
echo "Update check complete."
}
# --- Main Logic ---
if [[ "${BASH_ARGV[0]}" == "--container" ]]; then
# Run inside container mode
set -- "${@:1:$(($#-1))}" # remove --container from args
run_in_container "$@"
exit $?
fi
if [[ "$1" == "-S" ]]; then
fetch_safety_card "$2"
check_maintainer_change "$2"
echo -e "${RED}Since this is an AUR package, saur will enforce viewing the PKGBUILD, it will be displayed using bat, you can press Q to exit bat and will be asked for confirmation before continuing.${NC}\n"
read -n 1 -s -r -p "Press any key to continue..."
curl -fsSL "https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD?h=$2" | bat --language=bash
echo
confirm "Have you verified that the PKGBUILD is safe and wish to continue?"
cd ~/$install_dir
git clone "https://aur.archlinux.org/$2.git"
cd "$2"
fetch_aur_deps
makepkg -si
sed -i "/^$2 /d" "$INSTALLED_LIST"
version=$(pacman -Q "$2" | awk '{print $2}')
echo "$2 $version" >> "$INSTALLED_LIST"
cp PKGBUILD "$CACHE_DIR/$2.PKGBUILD"
elif [[ "$1" == "-Su" ]]; then
update
else
echo "Usage: $0 -S <package> | -Su"
fi