-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
update.am
138 lines (125 loc) · 4.85 KB
/
update.am
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
#!/usr/bin/env bash
# Function to update GitHub API key in AM-updater files
function update_github_api() {
local ghapikey_file="$AMPATH/ghapikey.txt"
if [[ -f "$ghapikey_file" ]]; then
ghapikey=$(<"$ghapikey_file")
local updater_files=("$APPSPATH"/*/AM-updater) # Assuming AM-updater is one level deeper
for f in "${updater_files[@]}"; do
if [[ -f "$f" ]] && grep -q "https://api.github.com" "$f"; then
# Check if the file already contains a valid API key
if ! grep -qE "(gh[ps]_[a-zA-Z0-9]{36}|github_pat_[a-zA-Z0-9]{22}_[a-zA-Z0-9]{59})" "$f"; then
# Insert HeaderAuthWithGITPAT before the GitHub API URL
sed -i "s#https://api.github.com#$HeaderAuthWithGITPAT https://api.github.com#g" "$f"
else
# Replace existing API key with the one from ghapikey.txt
sed -i "s#\(gh[ps]_[a-zA-Z0-9]\{36\}\|github_pat_[a-zA-Z0-9]\{22\}_[a-zA-Z0-9]\{59\}\)#$ghapikey#g" "$f"
fi
fi
done
fi
}
function _list_updatable_apps() {
_check_version
cd "$APPSPATH" && find -name 'AM-updater' -printf " %h\n" 2>/dev/null | sort -u | xargs -n 1 basename 2>/dev/null > "$AMPATH"/.cache/updatable-args
ARGS=$(cat "$AMPATH"/.cache/updatable-args)
for arg in $ARGS; do
app_version=$(cat "$AMPATH/.cache/version-args" | grep -w " ◆ $arg |" | sed 's:.*| ::')
echo " ◆ $arg $app_version" >> $AMPATH/.cache/updatable-args-list
done
}
case "$1" in
'-U'|'-u'|'update')
# Call update_github_api function
update_github_api
while [ -n "$1" ]; do
case $2 in
''|'--apps')
rm -R -f "$AMPATH"/.cache/*
_list_updatable_apps
echo '-----------------------------------------------------------------------------'
echo -e ' "'"$(echo "$AMCLI" | tr a-z A-Z)"'" CAN MANAGE UPDATES FOR THE FOLLOWING PROGRAMS:\n'
if test -f "$AMPATH/.cache/updatable-args-list"; then
cat "$AMPATH/.cache/updatable-args-list"
else
echo " None"
fi
echo -e '\n All self-updatable programs are excluded'; sleep 0.1
echo '-----------------------------------------------------------------------------'
echo ' >> START OF ALL PROCESSES <<'
echo '-----------------------------------------------------------------------------'
for f in "$APPSPATH"/*/; do
cd $f 2>/dev/null &&
if test -f ./AM-updater; then
APPNAME=$(echo "$(printf '%s\n' "${PWD##*/}")")
start=$(date +%s) && sh -x ./AM-updater > /dev/null 2>&1 | echo -ne ' Updating "'"$APPNAME"'"...\r' && end=$(date +%s) &&
echo -e " ◆ $(echo "$APPNAME" | tr a-z A-Z) is updated, $(($end-$start)) seconds elapsed!" &
else
echo "" > /dev/null 2>&1
fi
done
wait
echo '-----------------------------------------------------------------------------'
if test -f "$AMPATH"/.cache/updatable-args-list; then
mv "$AMPATH"/.cache/updatable-args-list "$AMPATH"/.cache/updatable-args-list-old
_list_updatable_apps
OLDVER="$AMPATH/.cache/updatable-args-list-old"
NEWVER="$AMPATH/.cache/updatable-args-list"
if cmp --silent -- "$NEWVER" "$OLDVER"; then
echo ' Nothing to do here!'
else
echo -e " The following apps have been updated:\n"
diff --new-line-format="" --unchanged-line-format="" "$NEWVER" "$OLDVER"
echo ""
fi
else
echo ' No apps to update here!'
fi
rm -R -f $APPSPATH/*/tmp
if [ -z "$2" ]; then
echo "-----------------------------------------------------------------------------"
$AMCLIPATH -s
fi
echo '-----------------------------------------------------------------------------'
echo ' >> END OF ALL PROCESSES << '
echo '-----------------------------------------------------------------------------'
sleep 0.2
exit;;
*)
if test -f "$APPSPATH"/$2/AM-updater; then
start=$(date +%s); "$APPSPATH"/$2/AM-updater > /dev/null 2>&1 | echo -ne " UPDATING $(echo $2 | tr a-z A-Z)\r"; end=$(date +%s)
echo -e " ◆ $(echo $2 | tr a-z A-Z) is updated, $(($end-$start)) seconds elapsed!" && break
else
echo ' "'"$(echo "$AMCLI" | tr a-z A-Z)"'" CANNOT MANAGE UPDATES FOR "'"$(echo $2 | tr a-z A-Z)"'"'
UPDATERS=$(cd "$APPSPATH"/$2 2>/dev/null && find . -name "*update*" -print 2>/dev/null)
if [ -n "$UPDATERS" ]; then
echo ' This program probably includes its own update system!'
fi
exit
fi
esac
done
shift
;;
'--force-latest')
case $2 in
'')
echo " USAGE: $AMCLI $1 [ARGUMENT]"; exit
;;
*)
if ! test -d "$APPSPATH"/$2; then
echo ' ERROR: "'$2'" is not installed, see "-f"'
elif ! test -f "$APPSPATH"/$2/AM-updater; then
echo ' ERROR: "'$AMCLI'" cannot manage updates for "'$2'"'
elif ! grep -q "api.github.com" "$APPSPATH"/$2/AM-updater; then
echo ' ERROR: "'$2'" source is not on Github'
elif ! grep -q "/releases | jq " "$APPSPATH"/$2/AM-updater; then
echo ' ERROR: "'$2'" does not redirect to a generic "releases"'
else
sed -i 's#/releases | jq #/releases/latest | jq #g' "$APPSPATH"/$2/AM-updater
$AMCLIPATH -u $2
fi
;;
esac
;;
esac