2023-03-22 18:28:43 -05:00
#!/usr/bin/env bash
2022-10-09 17:37:34 -05:00
2022-10-11 22:52:34 -05:00
# Run only one instance of this script at one time
[ " ${ BKLOCKER } " != "running" ] && exec env BKLOCKER = "running" flock -en "/tmp/revanced-builder.lock" " $0 " " $@ " || :
2022-10-11 20:14:42 -05:00
2022-10-11 18:29:50 -05:00
# Get timestamp
timestamp = $( date '+%s' )
2023-04-23 18:28:58 -05:00
# Log everything to a logfile inside logs/
2023-07-01 22:30:41 -05:00
log_file = " $1 /logs/ $timestamp .log "
2023-04-23 18:28:58 -05:00
[ -d " $1 " ] && mkdir -p " $1 /logs " && exec > >( tee " $log_file " ) 2>& 1
2022-10-11 18:29:50 -05:00
# Set working directory and current directory
2022-10-11 20:25:44 -05:00
if [ -d " $1 " ] ; then
WDIR = " $1 "
else
echo "Working directory not provided"
2023-01-28 12:59:25 -06:00
exit 1
2022-10-11 20:25:44 -05:00
fi
2022-10-26 00:13:26 -05:00
# File containing all patches
2023-03-22 18:28:43 -05:00
patch_file = " $WDIR /chosen_patches.txt "
2022-10-26 00:13:26 -05:00
2022-10-13 01:27:01 -05:00
# Returns if $1 is less than $2
ver_less_than( ) {
2022-11-23 14:52:46 -06:00
# Strip letters from version name
ver1 = $( echo $1 | sed 's/[a-zA-Z]*//g' )
ver2 = $( echo $2 | sed 's/[a-zA-Z]*//g' )
[ $( echo $ver1 $'\n' $ver2 | sort -V | tail -n1) != $ver1 ] && echo true || echo false
2022-10-13 01:27:01 -05:00
}
2022-10-12 18:28:44 -05:00
# Make sure to work in the script directory
2023-03-20 12:19:02 -05:00
SDIR = " $( dirname -- " $( readlink -f -- " $0 " ) " ) "
2022-10-12 18:28:44 -05:00
cd " $SDIR "
2022-10-09 17:37:34 -05:00
2023-03-22 18:28:43 -05:00
# Read the settings
if [ -f " $WDIR /build_settings " ] ; then
source " $WDIR /build_settings "
else
if [ -f "./build_settings" ] ; then
cp ./build_settings " $WDIR /build_settings "
source ./build_settings
else
echo "Could not find the build_settings file!"
fi
fi
2023-03-20 12:19:02 -05:00
# Get line numbers where included & excluded patches start from.
2022-10-09 17:37:34 -05:00
# We rely on the hardcoded messages to get the line numbers using grep
excluded_start = " $( grep -n -m1 'EXCLUDE PATCHES' " $patch_file " | cut -d':' -f1) "
included_start = " $( grep -n -m1 'INCLUDE PATCHES' " $patch_file " | cut -d':' -f1) "
# Get everything but hashes from between the EXCLUDE PATCH & INCLUDE PATCH line
# Note: '^[^#[:blank:]]' ignores starting hashes and/or blank characters i.e, whitespace & tab excluding newline
2023-03-20 12:19:02 -05:00
excluded_patches = " $( tail -n +$excluded_start $patch_file | head -n " $(( included_start - excluded_start)) " | grep '^[^#[:blank:]]' ) "
2022-10-09 17:37:34 -05:00
# Get everything but hashes starting from INCLUDE PATCH line until EOF
included_patches = " $( tail -n +$included_start $patch_file | grep '^[^#[:blank:]]' ) "
# Array for storing patches
declare -a patches
2022-10-12 23:29:22 -05:00
# Required artifacts in the format repository-name_filename
2023-03-20 12:19:02 -05:00
artifacts = "revanced/revanced-cli:revanced-cli.jar revanced/revanced-integrations:revanced-integrations.apk revanced/revanced-patches:revanced-patches.jar inotia00/VancedMicroG:microg.apk"
2022-10-09 17:37:34 -05:00
## Functions
# Function for populating patches array, using a function here reduces redundancy & satisfies DRY principals
populate_patches( ) {
# Note: <<< defines a 'here-string'. Meaning, it allows reading from variables just like from a file
while read -r patch; do
patches += ( " $1 $patch " )
2023-03-20 12:19:02 -05:00
done <<< " $2 "
2022-10-09 17:37:34 -05:00
}
## Main
# cleanup to fetch new revanced on next run
2022-10-11 20:25:44 -05:00
if [ [ " $2 " = = "clean" ] ] ; then
2022-10-09 22:10:11 -05:00
rm -f revanced-cli.jar revanced-integrations.apk revanced-patches.jar
2022-10-09 17:37:34 -05:00
exit
fi
2022-10-11 20:25:44 -05:00
if [ [ " $2 " = = "experimental" ] ] ; then
2022-10-09 17:37:34 -05:00
EXPERIMENTAL = "--experimental"
fi
2022-10-11 18:29:50 -05:00
# Set flag to determine if a build should happen or not
flag = false
2022-10-13 23:34:05 -05:00
check_flag = false
2022-10-11 18:29:50 -05:00
2022-10-11 19:42:28 -05:00
# Get inside the working directory
cd " $WDIR "
2022-10-18 19:55:25 -05:00
echo " $( date) | Starting check... "
2023-07-01 22:43:52 -05:00
cp verions.json versions.json.old
2022-10-11 19:42:28 -05:00
2022-10-09 17:37:34 -05:00
# Fetch all the dependencies
2023-01-28 12:59:25 -06:00
try = 0
2023-03-20 12:19:02 -05:00
while :; do
try = $(( $try + 1 ))
[ $try -gt 10 ] && echo "API error!" && exit 2
curl -X 'GET' 'https://releases.revanced.app/tools' -H 'accept: application/json' -o latest_versions.json
cat latest_versions.json | jq -e '.error' >/dev/null || break
echo " API failure, trying again. $(( 10 - $try )) tries left... "
2023-01-28 12:59:25 -06:00
sleep 10
done
2022-10-12 23:29:22 -05:00
for artifact in $artifacts ; do
2022-10-11 18:29:50 -05:00
#Check for updates
2022-10-14 13:59:43 -05:00
repo = $( echo $artifact | cut -d ':' -f1)
name = $( echo $artifact | cut -d ':' -f2)
basename = $( echo $repo | cut -d '/' -f2)
2022-10-18 19:55:25 -05:00
echo " Checking $basename "
2022-10-12 23:29:22 -05:00
version_present = $( jq -r " .\" $basename \" " versions.json)
2022-10-14 13:59:43 -05:00
data = " $( jq -r " .tools[] | select((.repository == \" $repo \") and (.content_type | contains(\"archive\"))) " latest_versions.json) "
2023-03-20 12:19:02 -05:00
[ [ $name = = microg.apk ] ] && version = $( curl -s " https://api.github.com/repos/ $repo /releases/latest " | jq -r '.tag_name' ) || version = $( echo " $data " | jq -r '.version' )
2022-10-13 01:27:01 -05:00
if [ [ $( ver_less_than $version_present $version ) = = true || ! -f $name || $2 = = force ] ] ; then
2022-10-13 23:19:22 -05:00
if [ [ $2 = = checkonly ] ] ; then
2022-10-18 19:55:25 -05:00
echo " [checkonly] $basename has an update ( $version_present -> $version ) "
2022-10-13 23:34:05 -05:00
check_flag = true
2022-10-13 23:19:22 -05:00
continue
fi
2022-10-18 19:55:25 -05:00
echo " Downloading $name "
2022-10-13 01:27:01 -05:00
[ [ $name = = microg.apk && -f $name && $2 != force ] ] && microg_updated = true
2022-10-09 17:37:34 -05:00
# shellcheck disable=SC2086,SC2046
2023-03-20 12:19:02 -05:00
[ [ $name = = microg.apk ] ] && download_link = " https://github.com/ $repo /releases/latest/download/ $name " || download_link = " $( echo " $data " | jq -r '.browser_download_url' ) "
curl -sLo " $name " " $download_link "
jq " .\" $basename \" = \" $version \" " versions.json >versions.json.tmp && mv versions.json.tmp versions.json
2022-10-18 20:50:18 -05:00
echo " Upgraded $basename from $version_present to $version "
2022-10-11 18:29:50 -05:00
flag = true
2022-10-09 17:37:34 -05:00
fi
done
2022-10-13 01:36:54 -05:00
[ [ ! -f com.google.android.youtube.apk || ! -f com.google.android.apps.youtube.music.apk ] ] && flag = true
2022-10-11 18:29:50 -05:00
# Exit if no updates happened
2022-10-13 23:19:22 -05:00
if [ [ $flag = = false && $2 != force ] ] ; then
2022-10-13 23:34:05 -05:00
if [ [ $check_flag = = false ] ] ; then
2022-10-18 19:55:25 -05:00
echo "Nothing to update"
2022-10-13 23:19:22 -05:00
else
2022-10-13 23:34:05 -05:00
" $SDIR /download_apkmirror.sh " " $WDIR " checkonly
2022-10-13 23:19:22 -05:00
fi
2022-10-18 19:55:25 -05:00
echo "--------------------" $'\n' "--------------------"
2022-10-11 18:29:50 -05:00
exit
fi
# Download required apk files
2023-07-01 22:50:16 -05:00
[ [ $2 != buildonly ] ] && " $SDIR /download_apkmirror.sh " " $WDIR "
2022-10-11 18:29:50 -05:00
2022-10-09 17:37:34 -05:00
# If the variables are NOT empty, call populate_patches with proper arguments
[ [ ! -z " $excluded_patches " ] ] && populate_patches "-e" " $excluded_patches "
[ [ ! -z " $included_patches " ] ] && populate_patches "-i" " $included_patches "
2023-04-23 18:52:32 -05:00
# Variable to flag errors
error = 0
2023-03-22 18:28:43 -05:00
# Functions for building the APKs
build_yt_nonroot( ) {
echo "************************************"
echo "Building YouTube APK"
echo "************************************"
if [ -f "com.google.android.youtube.apk" ] ; then
echo "Building Non-root APK"
java -jar revanced-cli.jar -m revanced-integrations.apk -b revanced-patches.jar \
${ patches [@] } \
$EXPERIMENTAL \
-a com.google.android.youtube.apk -o revanced-yt-nonroot.apk
else
echo "Cannot find YouTube APK, skipping build"
fi
echo ""
echo "************************************"
# Rename files
2023-04-23 18:52:32 -05:00
mv revanced-yt-nonroot.apk YouTube_ReVanced_nonroot_$timestamp .apk || error = 1
2023-03-22 18:28:43 -05:00
}
build_yt_root( ) {
echo "************************************"
echo "Building YouTube APK"
echo "************************************"
if [ -f "com.google.android.youtube.apk" ] ; then
echo "Building Root APK"
java -jar revanced-cli.jar -m revanced-integrations.apk -b revanced-patches.jar --mount \
-e microg-support ${ patches [@] } \
$EXPERIMENTAL \
-a com.google.android.youtube.apk -o revanced-yt-root.apk
else
echo "Cannot find YouTube APK, skipping build"
fi
echo ""
echo "************************************"
# Rename files
2023-04-23 18:52:32 -05:00
mv revanced-yt-root.apk YouTube_ReVanced_root_$timestamp .apk || error = 1
2023-03-22 18:28:43 -05:00
}
build_ytm_nonroot( ) {
echo "Building YouTube Music APK"
echo "************************************"
if [ -f "com.google.android.apps.youtube.music.apk" ] ; then
echo "Building Non-root APK"
java -jar revanced-cli.jar -m revanced-integrations.apk -b revanced-patches.jar \
${ patches [@] } \
$EXPERIMENTAL \
-a com.google.android.apps.youtube.music.apk -o revanced-ytm-nonroot.apk
else
echo "Cannot find YouTube Music APK, skipping build"
fi
2022-10-11 18:29:50 -05:00
2023-03-22 18:28:43 -05:00
# Rename files
2023-04-23 18:52:32 -05:00
mv revanced-ytm-nonroot.apk YouTube_Music_ReVanced_nonroot_$timestamp .apk || error = 1
2023-03-22 18:28:43 -05:00
}
build_ytm_root( ) {
echo "Building YouTube Music APK"
echo "************************************"
if [ -f "com.google.android.apps.youtube.music.apk" ] ; then
echo "Building Root APK"
java -jar revanced-cli.jar -m revanced-integrations.apk -b revanced-patches.jar --mount \
-e microg-support ${ patches [@] } \
$EXPERIMENTAL \
-a com.google.android.apps.youtube.music.apk -o revanced-ytm-root.apk
else
echo "Cannot find YouTube Music APK, skipping build"
fi
# Rename files
2023-04-23 18:52:32 -05:00
mv revanced-ytm-root.apk YouTube_Music_ReVanced_root_$timestamp .apk || error = 1
2023-03-22 18:28:43 -05:00
}
# Check the config and build accordingly
$YT_NONROOT && build_yt_nonroot
$YT_ROOT && build_yt_root
$YTM_NONROOT && build_ytm_nonroot
$YTM_ROOT && build_ytm_root
2022-10-11 20:14:42 -05:00
2022-10-11 18:29:50 -05:00
# Send telegram message about the new build
2022-10-11 21:08:02 -05:00
2023-04-23 18:52:32 -05:00
if [ $error = = 1 ] ; then
echo "There was an error while building!"
2023-07-01 22:30:41 -05:00
msg = "There was an error during the build process! Please take a look at the logs." $'\n' " Timestamp: $timestamp "
2023-04-23 18:52:32 -05:00
if $TG_NOTIFICATIONS ; then
2023-07-01 22:30:41 -05:00
./telegram.sh -t " $TELEGRAM_TOKEN " -c " $TELEGRAM_CHAT " -T "❗❗❗ Build Error ❗❗❗" -M " $msg " $'\n' "⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯"
fi
if $GOTIFY_NOTIFICATIONS ; then
curl -X POST " $GOTIFY_URL /message?token= $GOTIFY_TOKEN " \
-F "title=⚙⚙⚙ Build Details ⚙⚙⚙" -F " message= $msg " -F "priority=5"
fi
if $NTFY_NOTIFICATIONS ; then
curl -H "Icon: https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Revanced-logo-round.svg/240px-Revanced-logo-round.svg.png" \
-H "Title: ⚙⚙⚙ ReVanced Build ⚙⚙⚙" \
-d " $MESSAGE " \
" $NTFY_URL / $NTFY_TOPIC "
2023-04-23 18:52:32 -05:00
fi
2023-07-01 22:43:52 -05:00
mv versions.json versions.json.fail
mv versions.json.old versions.json
2023-04-23 18:52:32 -05:00
exit 4
fi
2023-03-22 18:28:43 -05:00
if $TG_UPLOAD ; then
2023-04-02 00:15:37 -05:00
echo "Uploading to telegram"
2023-03-22 18:28:43 -05:00
# telegram-upload uses personal account, hence bypassing 50 MB max upload limit of bots
[ " $CHANNEL_ADDRESS " = = "" ] && echo "Please provide valid channel address in the settings!"
/home/sintan/.local/bin/telegram-upload YouTube_ReVanced_nonroot_$timestamp .apk YouTube_Music_ReVanced_nonroot_$timestamp .apk --to " $CHANNEL_ADDRESS " --caption "" && sent = true
fi
2022-10-11 21:08:02 -05:00
2023-06-22 00:59:32 -05:00
# Create the message to be sent
msg = $( cat versions.json | tail -n+2 | head -n-1 | cut -c3- | sed "s/\"//g" | sed "s/,//g" | sed "s/com.google.android.apps.youtube.music/YouTube Music/" |
sed "s/com.google.android.youtube/YouTube/" | sed "s/VancedMicroG/Vanced microG/" | sed "s/revanced-/ReVanced /g" | sed "s/patches/Patches/" |
sed "s/cli/CLI/" | sed "s/integrations/Integrations/" | awk 1 ORS = $'\n' ) # I know, it's a hacky solution
2023-03-22 18:28:43 -05:00
if $TG_NOTIFICATIONS ; then
2023-04-02 00:15:37 -05:00
echo "Sending messages to telegram"
2023-03-22 18:28:43 -05:00
# telegram.sh uses bot account, but it supports formatted messages
[ [ " $TELEGRAM_TOKEN " = = "" || " $TELEGRAM_CHAT " = = "" ] ] && echo "Please provide valid channel address in the settings!"
2023-04-02 00:15:37 -05:00
./telegram.sh -t " $TELEGRAM_TOKEN " -c " $TELEGRAM_CHAT " -T "⚙⚙⚙ Build Details ⚙⚙⚙" -M " $msg " $'\n' " Timestamp: $timestamp " $'\n' "⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯"
[ $microg_updated ] && ./telegram.sh -t " $TELEGRAM_TOKEN " -c " $TELEGRAM_CHAT " -M "_An update of microg was published._"
2023-03-22 18:28:43 -05:00
fi
2022-10-11 18:29:50 -05:00
2023-06-22 00:59:32 -05:00
if $GOTIFY_NOTIFICATIONS ; then
echo "Sending messages to Gotify"
MESSAGE = " $msg " $'\n' " Timestamp: $timestamp "
curl -X POST " $GOTIFY_URL /message?token= $GOTIFY_TOKEN " \
-F "title=⚙⚙⚙ Build Details ⚙⚙⚙" -F " message= $MESSAGE " -F "priority=5"
MESSAGE = "An update of microg was published."
[ $microg_updated ] && curl -X POST " $GOTIFY_URL /message?token= $GOTIFY_TOKEN " \
-F "title=⚙⚙⚙ Build Details ⚙⚙⚙" -F " message= $MESSAGE " -F "priority=5"
fi
2023-06-23 19:51:02 -05:00
if $NTFY_NOTIFICATIONS ; then
echo "Sending messages to ntfy.sh"
MESSAGE = " $msg " $'\n' " Timestamp: $timestamp "
curl -H "Icon: https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Revanced-logo-round.svg/240px-Revanced-logo-round.svg.png" \
-H "Title: ⚙⚙⚙ ReVanced Build ⚙⚙⚙" \
-d " $MESSAGE " \
" $NTFY_URL / $NTFY_TOPIC "
MESSAGE = "An update of microg was published."
[ $microg_updated ] && curl -H "Icon: https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Revanced-logo-round.svg/240px-Revanced-logo-round.svg.png" \
-H "Title: ⚙⚙⚙ ReVanced Build ⚙⚙⚙" \
-d " $MESSAGE " \
" $NTFY_URL / $NTFY_TOPIC "
fi
2022-10-18 20:50:18 -05:00
# Do some cleanup, keep only the last 3 build's worth of files and a week worth of logs
2022-10-11 18:29:50 -05:00
mkdir -p archive
2023-03-22 18:28:43 -05:00
mv *ReVanced_*_$timestamp .apk archive/
2023-03-20 12:19:02 -05:00
find ./archive -maxdepth 1 -type f -printf '%Ts\t%P\n' |
sort -rn |
tail -n +7 |
cut -f2- |
xargs -r -I { } rm "./archive/{}"
2022-10-18 20:50:18 -05:00
find ./logs -mtime +7 -exec rm { } \;
2022-10-20 21:03:54 -05:00
2023-01-14 14:58:15 -06:00
# Run a custom post script, if available
[ -f post_script.sh ] && ./post_script.sh $timestamp
2023-03-20 12:19:02 -05:00
echo "Done!" $'\n' "************************************"