2020-07-05 11:39:37 +00:00
#!/bin/sh
2020-05-26 18:00:32 +00:00
2019-11-12 18:41:19 +00:00
# Update all assets
2020-03-16 07:24:31 +00:00
#
# Copyright 2018-2020 FriendlyNeighborhoodShane
# Distributed under the terms of the GNU GPL v3
2019-09-30 08:35:55 +00:00
2019-11-12 18:41:19 +00:00
workdir = " $( pwd ) " ;
2020-02-01 11:14:38 +00:00
cd " $workdir " || { echo " " ; echo " FATAL: Can't cd to $workdir " ; return 1; } ;
2019-11-12 18:41:19 +00:00
confdir = " $workdir /conf " ;
2020-05-27 08:18:56 +00:00
resdir = " $workdir /res " ;
2019-11-12 18:41:19 +00:00
resdldir = " $workdir /resdl " ;
2020-07-07 15:52:18 +00:00
tmpdir = " $( mktemp -d) " ;
2020-05-27 08:18:56 +00:00
reldir = " $workdir /releases " ;
2019-11-24 07:29:12 +00:00
updatetime = " $( date -u +%Y%m%d%H%M%S) " ;
2019-12-19 09:42:00 +00:00
updatelog = " $reldir /update- $updatetime .log " ;
2019-09-30 08:35:55 +00:00
2020-05-27 03:51:46 +00:00
select_word( ) {
select_term = " $1 " ;
cat | while read -r select_line; do
select_current = 0;
select_found = "" ;
for select_each in $select_line ; do
2020-07-05 11:40:41 +00:00
select_current = " $(( select_current + 1 )) " ;
2020-05-27 03:51:46 +00:00
[ " $select_current " = " $select_term " ] && { select_found = "yes" ; break; }
done ;
[ " $select_found " = "yes" ] && echo " $select_each " ;
done ;
}
2019-11-12 18:41:19 +00:00
echo " " ;
echo "-- Minimal MicroG Update Script --" ;
echo "-- The Essentials Only MicroG Pack --" ;
echo "-- From The MicroG Telegram group --" ;
echo "-- No, not the Official one --" ;
# Bin check
2020-07-07 16:34:25 +00:00
for bin in chmod cp curl grep head jq mv rm sort unzip; do
2020-01-27 17:05:10 +00:00
[ " $( which $bin ) " ] || { echo " " >& 2; echo " FATAL: No $bin found " >& 2; return 1; }
2019-11-12 18:41:19 +00:00
done ;
2019-11-24 07:29:12 +00:00
echo " " ;
echo " - Working from $workdir " ;
echo " " ;
echo " - Update started at $updatetime " ;
2019-11-12 18:41:19 +00:00
echo " " ;
echo " - Cleaning..." ;
2020-02-13 17:47:28 +00:00
2019-11-12 18:41:19 +00:00
rm -Rf " $tmpdir " ;
2019-11-24 09:38:39 +00:00
mkdir -p " $tmpdir " " $tmpdir /repos " " $( dirname " $updatelog " ) " ;
2019-11-12 18:41:19 +00:00
# Config
2019-11-24 05:52:16 +00:00
[ -f " $confdir /resdl-download.txt " ] || { echo " " ; echo "F: No resdl-download.txt found" ; return 1; }
2020-03-16 06:36:00 +00:00
cp -f " $confdir /resdl-download.txt " " $tmpdir /resdlconf " ;
2020-07-05 05:46:42 +00:00
chmod +x " $tmpdir /resdlconf " ;
2020-03-16 06:36:00 +00:00
. " $tmpdir /resdlconf " || { echo " " ; echo "FATAL: resdl-download.txt cannot be executed" >& 2; return 1; } ;
2019-11-12 18:41:19 +00:00
2019-12-19 09:42:00 +00:00
if [ " $* " ] ; then
2019-11-12 18:41:19 +00:00
echo " " ;
echo " - Building update list..." ;
stuff_download_new = "" ;
stuff_repo_new = "" ;
2020-02-01 11:14:38 +00:00
for include in " $@ " ; do
2019-11-12 18:41:19 +00:00
echo " -- CONFIG: Including $include " ;
stuff_download_new = " $stuff_download_new
$( echo " $stuff_download " | grep -Pi " ^[ \t]*[^ \t]* $include [^ \t]*[ \t]+ " )
" ;
done ;
stuff_download = " $( echo " $stuff_download_new " | sort -u) " ;
repo_apps = " $( echo " $stuff_download " | grep -P "^[ \t]*[^ \t]+[ \t]+repo[ \t]+" ) " ;
2020-05-27 03:51:46 +00:00
for repo in $( echo " $repo_apps " | select_word 3) ; do
2019-11-12 18:41:19 +00:00
stuff_repo_new = " $stuff_repo_new
$( echo " $stuff_repo " | grep -P " ^[ \t]* $( dirname " $repo " ) [ \t]+ " | head -n1)
" ;
done ;
stuff_repo = " $( echo " $stuff_repo_new " | sort -u) " ;
2020-02-13 17:47:28 +00:00
fi ;
2019-11-12 18:41:19 +00:00
2019-12-18 16:44:15 +00:00
# Pre update actions
pre_update_actions;
2019-11-12 18:41:19 +00:00
# Download repos
echo " " ;
echo " - Downloading repos..." ;
2020-05-27 03:51:46 +00:00
for repo in $( echo " $stuff_repo " | select_word 1) ; do
2019-11-12 18:41:19 +00:00
line = " $( echo " $stuff_repo " | grep -P " ^[ \t]* $repo [ \t]+ " | head -n1) " ;
2020-05-27 03:51:46 +00:00
repourl = " $( echo " $line " | select_word 2) " ;
2020-01-27 17:05:10 +00:00
[ " $repourl " ] || { echo " ERROR: Repo $repo has no URL " >& 2; continue ; }
2019-11-12 20:18:04 +00:00
echo " -- REPO: Downloading repo $repo " ;
2020-07-07 16:34:25 +00:00
curl --progress-bar " $repourl /index-v1.jar " -o " $tmpdir /repos/ $repo .jar " ;
2020-01-27 17:05:10 +00:00
[ -f " $tmpdir /repos/ $repo .jar " ] || { echo " ERROR: Repo $repo failed to download " >& 2; continue ; }
2019-11-12 18:41:19 +00:00
unzip -oq " $tmpdir /repos/ $repo .jar " "index-v1.json" -d " $tmpdir /repos/ " ;
2020-01-27 17:05:10 +00:00
[ -f " $tmpdir /repos/index-v1.json " ] || { echo " ERROR: Repo $repo failed to unzip " >& 2; continue ; }
2019-11-12 18:41:19 +00:00
mv -f " $tmpdir /repos/index-v1.json " " $tmpdir /repos/ $repo .json " ;
2020-01-27 17:05:10 +00:00
[ -f " $tmpdir /repos/ $repo .json " ] || { echo " ERROR: Repo $repo failed to rename " >& 2; continue ; }
2019-11-12 18:41:19 +00:00
done ;
# Download assets
echo " " ;
echo " - Downloading assets..." ;
2020-05-27 03:51:46 +00:00
for object in $( echo " $stuff_download " | select_word 1) ; do
2019-11-12 18:41:19 +00:00
line = " $( echo " $stuff_download " | grep -P " ^[ \t]* $object [ \t]+ " | head -n1) " ;
2020-05-27 03:51:46 +00:00
source = " $( echo " $line " | select_word 2) " ;
objectpath = " $( echo " $line " | select_word 3) " ;
objectarg = " $( echo " $line " | select_word 4) " ;
2020-01-27 17:05:10 +00:00
[ " $objectpath " ] || { echo " ERROR: $object has no source arguments " >& 2; continue ; }
2019-11-12 18:41:19 +00:00
echo " -- ASSET: Downloading object $object from source $source " ;
case " $source " in
local )
objectfile = " $objectpath " ;
; ;
*)
case " $source " in
direct)
objecturl = " $objectpath " ;
; ;
github)
echo " ---- Getting GitHub URL for $object " ;
2019-11-24 07:29:12 +00:00
objecturl = " $( curl -sN " https://api.github.com/repos/ $objectpath /releases " | jq -r '.[].assets[].browser_download_url' | grep -P " $objectarg $" | head -n1) " ;
2019-11-12 18:41:19 +00:00
; ;
gitlab)
echo " ---- Getting GitLab project ID for $object " ;
2020-05-27 03:51:46 +00:00
objectid = " $( curl -sN " https://gitlab.com/ $objectpath " | grep "Project ID" | head -n1 | select_word 3) " ;
2020-01-27 17:05:10 +00:00
[ " $objectid " ] || { echo " ERROR: $object gitlab project ID not found " >& 2; continue ; }
2019-11-12 18:41:19 +00:00
echo " ---- Getting GitLab URL for $object " ;
2019-11-24 07:29:12 +00:00
objectupload = " $( curl -sN " https://gitlab.com/api/v4/projects/ $objectid /repository/tags " | jq -r '.[].release.description' | grep -Po " (/uploads/[^()]* $objectarg ) " | head -n1 | tr -d "()" ) " ;
2020-01-27 17:05:10 +00:00
[ " $objectupload " ] || { echo " ERROR: $object gitlab project upload not found " >& 2; continue ; }
2019-11-24 07:29:12 +00:00
objecturl = " https://gitlab.com/ $objectpath $objectupload " ;
2019-11-12 18:41:19 +00:00
; ;
repo)
objectrepo = " $( dirname " $objectpath " ) " ;
objectpackage = " $( basename " $objectpath " ) " ;
2020-05-02 14:54:09 +00:00
[ " $objectarg " ] && {
2020-05-27 03:51:46 +00:00
objectarch = " $( echo " $objectarg " | sed "s|:| |g" | select_word 1) " ;
objectsdk = " $( echo " $objectarg " | sed "s|:| |g" | select_word 2) " ;
2020-05-02 14:54:09 +00:00
}
2020-02-01 11:14:38 +00:00
[ " $objectrepo " ] && [ " $objectpackage " ] || { echo " ERROR: $object has no valid repo arguments " >& 2; continue ; }
2020-01-27 17:05:10 +00:00
[ -f " $tmpdir /repos/ $objectrepo .json " ] || { echo " ERROR: $object repo $objectrepo does not exist " >& 2; continue ; }
2020-03-15 07:20:12 +00:00
echo " ---- Getting repo URL for $object from repo $objectrepo " ;
2019-11-12 18:41:19 +00:00
objectserver = " $( jq -r '.repo.address' " $tmpdir /repos/ $objectrepo .json " ) " ;
if [ " $objectarg " ] ; then
2020-05-02 14:54:09 +00:00
echo " ---- Getting object for args $objectarg [ $objectarch ] [ $objectsdk ] " ;
objectserverfile = " $( jq -r --arg pkg " $objectpackage " --arg arch " $objectarch " --arg sdk " $objectsdk " '.packages[$pkg][] | if ( $arch | length ) == 0 then . elif has ( "nativecode" ) then select ( .nativecode[]? == $arch ) else . end | if ( $sdk | length ) == 0 then . else select ( ( .minSdkVersion | tonumber ) <= ( $sdk | tonumber ) ) end | .apkName' " $tmpdir /repos/ $objectrepo .json " | head -n1) " ;
2019-11-12 18:41:19 +00:00
else
objectserverfile = " $( jq -r --arg pkg " $objectpackage " '.packages[$pkg][].apkName' " $tmpdir /repos/ $objectrepo .json " | head -n1) " ;
fi ;
2020-05-02 09:00:45 +00:00
[ " $objectserver " ] && [ " $objectserver " != "null" ] && [ " $objectserverfile " ] && [ " $objectserverfile " != "null" ] || { echo " ERROR: $object has no URL available " >& 2; continue ; }
2019-11-12 18:41:19 +00:00
objecturl = " $objectserver / $objectserverfile " ;
; ;
*)
2020-01-27 17:05:10 +00:00
echo " ERROR: Source $source for $object unknown " >& 2;
2019-11-12 18:41:19 +00:00
; ;
esac ;
2020-02-13 17:47:28 +00:00
[ " $objecturl " ] || { echo " ERROR: $object has no URL available " >& 2; continue ; }
2019-11-12 18:41:19 +00:00
objectname = " $( basename " $objecturl " ) " ;
objectfile = " $tmpdir / $objectname " ;
2019-11-12 20:18:04 +00:00
echo " ---- Downloading $objecturl " ;
2020-07-07 16:34:25 +00:00
curl --progress-bar " $objecturl " -o " $objectfile " || { echo " ERROR: $object failed to download " >& 2; continue ; }
2020-01-27 17:05:10 +00:00
[ -f " $objectfile " ] || { echo " ERROR: $object failed to download " >& 2; continue ; }
2019-11-24 10:24:49 +00:00
echo " NAME: $objectname , FILE: $object , URL: $objecturl ; " >> " $updatelog " ;
2019-11-12 18:41:19 +00:00
; ;
esac ;
mkdir -p " $resdldir / $( dirname " $object " ) " ;
mv -f " $objectfile " " $resdldir / $object " ;
2020-02-13 17:47:28 +00:00
[ -f " $resdldir / $object " ] || { echo " ERROR: $object failed to copy " >& 2; continue ; }
2019-11-12 18:41:19 +00:00
done ;
2019-12-18 16:44:15 +00:00
# Post update actions
post_update_actions;
2019-11-12 18:41:19 +00:00
# Done
echo " " ;
echo " - Done!" ;
rm -Rf " $tmpdir " ;
echo " " ;