#!/bin/bash -e SCRIPT_PATH="$(dirname "$0")" APK_FILE=/apks.txt function is_newer() { local url="$1" local remote_date local_date local headers; headers="$(curl -sSLI "$url")" local file; file="$(grep -Po '(?<=filename=).*\.apk' <<< "$headers")" if ! [ -f "$file" ]; then return 0 fi remote_date="$(sed -n 's/last-modified: //p' <<< "$headers")" local_date="$(stat "$file" | sed -n 's/^Modify: //p')" [ "$(date -d "$remote_date" '+%s')" -gt "$(date -d "$local_date" '+%s')" ] } function filter_links() { links=() for link in "$@"; do if is_newer "$link"; then links+=("$link") fi done echo "${links[@]}" } function get_links() { local file="$1" local url local tag local line while read -r line; do IFS=' ' read -r url tag <<< "$line" case "$url" in *github.com/*) "$SCRIPT_PATH/get_github_dl_links.sh" "$url" "$tag" ;; *gitlab.com/*) "$SCRIPT_PATH/get_gitlab_dl_links.sh" "$url" "$tag" ;; *) ;; esac done < <(awk 'NF && $1!~/^#/' "$file" | sort -u) } function main() { local links readarray -t links < <(get_links "$APK_FILE" | sort -u) read -ra links < <(filter_links "${links[@]}") (cd /repo/repo/ && xargs -n1 -P8 curl -sSLO <<< "${links[@]}") (cd /repo/ && fdroid update -c) if [ -n "$PUID" ]; then chown -R "$PUID:$PGID" /repo/ fi } main "$@"