Repository navigation
Expand file tree
/
Copy pathfdroidcli.sh
More file actions
210 lines (194 loc) · 4.78 KB
/
Copy pathfdroidcli.sh
File metadata and controls
210 lines (194 loc) · 4.78 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
## TODO
. adb_wrapper.sh
parse_versions() {
ver_file=".versions.json"
unset -v ver_code
unset -v ver_name
unset -v suggested
curl -s "https://f-droid.org/api/v1/packages/$1" > "$ver_file"
suggested="$(jq 'try .suggestedVersionCode catch fromjson?' "$ver_file")"
if [ "$suggested" == "null" ]; then
return 1
fi
no=0
until [ "$ver_code" == "$suggested" ]; do
ver_code="$(jq ".packages[$no].versionCode" "$ver_file")"
ver_name="$(jq ".packages[$no].versionName" "$ver_file" | sed 's/\"//g')"
no=$((no+1))
if [ "$ver_code" == "null" ] || [ "$ver_code" == "" ]; then
return 1
fi
done
rm "$ver_file"
}
list_installed() {
for pkg in $(adb shell pm list packages -f -3 -i | cut -d"/" -f6 | cut -d'=' -f2- | grep -v com.android.vending | cut -d' ' -f1); do
ver="$(adb shell dumpsys package "$pkg" | grep versionName | cut -d= -f2)"
echo "$pkg/$ver"
done > .installed.list
}
install_pkg() {
if curl -# -o "$apk_file" "$dl_url"; then
if [ "$TERMUX_VERSION" != "" ]; then
if adb install -r "$apk_file" >/dev/null; then
echo "Successfully installed $1"
# rm "$apk_file"
else
echo "Failed to install $1"
fi
else
adb install -r "$apk_file"
fi
rm *.apk >/dev/null
fi
}
download_pkg() {
if ! parse_versions "$1"; then
echo "We couldnt find $1"
return 1
fi
url="https://f-droid.org/en/packages/$1/"
dl_url="$(curl -s "$url/" | grep ".apk" | sed '/class/d;/.apk.asc/d;s/<a href=\"//g;s/\">//g' | grep "$1_$ver_code" |xargs)"
apk_file="$(echo "$dl_url" | cut -d"/" -f5)"
echo "APK: $apk_file Version: $ver_name"
if [ ! -f "$apk_file" ]; then
if [ "$2" == "" ]; then
echo "$1 uses these permissions."
echo
curl -s "$url/" | grep "permission-label" | sed -r '/></d;s/<\/div>//g;s/<div class="permission-label">//g;s/[[:blank:]]+/ /g' | sort -u
echo
echo -n "Do you wish to proceed? [y/N]:"
read -r -n1 confirm
echo
if [ "$confirm" != "y" ]; then
echo "$1 was NOT installed"
exit
else
install_pkg "$1"
exit 0
fi
fi
if [ "$2" == "-y" ]; then
install_pkg "$1"
fi
if [ "$2" == "-d" ]; then
curl -# -o "$apk_file" "$dl_url"
fi
fi
}
pkg_search() {
app_count=0
n=0
new_ver=""
pkg_name=()
pkg_ver=()
app_name=()
app_desc=()
app_url=()
curl -s "https://search.f-droid.org/api/search_apps?q=$1" | jq '.apps[] | "\(.name)", "\(.summary)", "\(.url)"' > .fdroid_search_results
while IFS="" read -r line; do
line="${line//\"/}"
if [ "$n" == "0" ]; then
app_name+=("$line")
app_count=$((app_count+1))
elif [ "$n" == "1" ]; then
app_desc+=("$line")
else
app_url+=("$line")
tmp_pkg_name="$(echo "$line" | cut -d"/" -f6)"
pkg_name+=("$tmp_pkg_name")
parse_versions "$tmp_pkg_name"
pkg_ver+=("$ver_name")
fi
if [ "$n" == "2" ]; then
n=0
else
n=$((n+1))
fi
done < .fdroid_search_results
}
display_search() {
echo
list_installed
pkg_search "$1"
for ((i=0;i<=((app_count-1));i++)); do
if grep -q "${pkg_name[$i]}/" .installed.list ; then
installed_ver="$(grep "${pkg_name[$i]}/" .installed.list | cut -d"/" -f2-)"
if [ "$installed_ver" == "${pkg_ver[$i]}" ]; then
echo "${pkg_name[$i]}/${pkg_ver[$i]} [installed]"
else
echo "${pkg_name[$i]}/${pkg_ver[$i]} [upgradable from $installed_ver]"
fi
else
echo "${pkg_name[$i]}/${pkg_ver[$i]}"
fi
echo "${app_name[$i]}: ${app_desc[$i]}"
echo
done
}
mass_install() {
file="$1"
if [ ! -f "$file" ]; then
echo "mass takes a file argument"
exit 1
fi
if [ "$TERMUX_VERSION" != "" ]; then
sed -i '/com.termux /d' "$file"
fi
while read -r pkg ver; do
download_pkg "$pkg" "-y"
done < "$file"
}
check_update() {
list_installed
sed -i 's/\// /g' .installed.list
while read -r pkg ver; do
if ! parse_versions "$pkg"; then
continue
fi
if [[ "$ver_name" != "$ver" ]] && [ "$ver_name" != "" ] && [ "$ver_name" != "null" ]; then
echo "$pkg \"$ver\" -> \"$ver_name\""
download_pkg "$pkg" "-y"
fi
done < .installed.list
}
MY_VERSION="1.0"
show_usage() {
echo
echo "$0: $MY_VERSION"
echo
echo " list lists installed packages"
echo " and versions"
echo " install installs a package"
echo " search searches for an app"
echo " mass installs mutiple apps"
echo " listed in a file"
echo " update updates all apps"
echo
echo
}
case $1 in
search)
shift
display_search "$1";;
install)
shift
download_pkg "$1" "$2";;
list)
list_installed
sed 's/\// /g' .installed.list;;
mass)
shift
mass_install "$1";;
update)
check_update;;
*)
show_usage;;
esac
# cleanup
if [ -f .installed.list ]; then
rm .installed.list
fi
if [ -f .fdroid_search_results ]; then
rm .fdroid_search_results
fi