#/bin/sh
# vim: set ts=3 sw=3 sts=3:
# File: /etc/installed/fdroid/fdroid-sync.sh
# Location: server3
# Author: bgstack15
# SPDX-License-Identifier: GPL-3.0-only
# Startdate: 2024-05-29-4 07:36
# Title: F-Droid Sync wrapper
# Purpose: Single command to mirror down only the F-droid packages I use
# History:
# Usage:
# Reference:
# Improve:
#    take a list of hardcoded filenames and prepend them with "P" in the rsync file, to avoid deletion.
# Documentation:
# Dependencies:
#    user fdroid, ~/venv1, custom pip packages installed
#    cd ~/git ; git clone https://gitlab.com/fdroid/fdroiddata
#    plecho from bgscripts

CONF_FILE="${CONF_FILE:-/etc/sysconfig/fdroid-sync}"
test -f "${CONF_FILE}" && . "${CONF_FILE}"
WORK_DIR="${WORK_DIR:-/etc/installed/fdroid}"
GET_FILE="${GET_FILE:-${WORK_DIR}/get}"
MIRROR_DIR="${MIRROR_DIR:-/mnt/mirror/fdroid/repo}"
GIT_DIR="${GIT_DIR:-/home/fdroid/git/fdroiddata}"
INSTALLED_FILES_DIR="${INSTALLED_FILES_DIR:-/mnt/public/Support/Systems/server3/fdroid}"

prepare_input_file() {
   echo "# BEGIN preparing list of packages I want."
   # installed.txt was the exact output shared from my fdroid instance(s)
   # I do not understand how this appears to limit to just our desired apps, within the icons/ path while also grabbing still the app files. But I think it does, so we will leave it at that.
   {
      #awk -F',' '$1!~/packageName/{print $1}' < "${WORK_DIR}/installed.txt" | awk '{print "+ **/"$0"**";}'
         #awk -F',' '$1!~/packageName/{print "+ **/"$1"*"; print "+ **/"$1"/en*/***"; print "+ icons*/"$1"***";}' | sort
      printf '%s\n' '- org.fdroid.fdroid.privileged*' ;
      printf '%s\n' '- com.termux.api*' ;
      printf '%s\n' '- com.termux.boot*' ;
      printf '%s\n' '- com.termux.gui*' ;
      printf '%s\n' '- com.termux.nix*' ;
      printf '%s\n' '- com.termux.styling*' ;
      printf '%s\n' '- com.termux.tasker*' ;
      printf '%s\n' '- com.termux.widget*' ;
      printf '%s\n' '- com.termux.window*' ;
      # Find all the metadata/*.protect files and use that filename as a deletion-protection filter for rsync
      # Does this, in effect: printf '%s\n' 'P org.qtproject.bibletimemini*' ;
      find "${MIRROR_DIR%%/}/../metadata/"*.protect | \
         xargs basename --multiple | sed -r -e 's/\.protect$/*/;' -e 's/^/P /;'
      cat "${INSTALLED_FILES_DIR%%/}"/installed*.txt | grep -vE 'com.google.android.marvin.talkback|org.fdroid.fdroid.privileged|\<packageName\>' | \
         awk -F',' '$1!~/packageName/{print "+ **/"$1"*"; print "+ **/"$1"/en*/***"; }' | sort
      printf '%s\n' '+ icons*/'
      printf '%s\n' '- *' ;
      #printf '%s\n' '+ **/index*'
      #awk -F',' '$1!~/packageName/{print $1}' < installed.txt | awk '{print "+ **"$0"*";print "+ icons*/"$0"*";}' ; printf '%s\n' '- *' ;
   } > "${GET_FILE}"
}

sync_files() {
   echo "# BEGIN syncing files"
   if test -n "${APPLY}" ;
   then
      dashn=""
   else
      dashn="-n "
   fi
   if test "${USER}" != "fdroid" ;
   then
      sudo -u fdroid -E /usr/bin/rsync --filter "merge ${GET_FILE}" ${dashn}-v -r -d -aHS --delete --delete-delay plug-mirror.rcac.purdue.edu::fdroid/repo/ "${MIRROR_DIR}"
   else
      /usr/bin/rsync --filter "merge ${GET_FILE}" ${dashn}-v -r -aHS --delete --delete-delay plug-mirror.rcac.purdue.edu::fdroid/repo/ "${MIRROR_DIR}"
   fi
}

fix_metadata() {
   echo "# BEGIN fixing metadata"
   _oldpwd="${PWD}"
   cd "${GIT_DIR}"
   git checkout master ; git pull
   cat "${INSTALLED_FILES_DIR%%/}"/installed*.txt | grep -vE 'com.google.android.marvin.talkback|org.fdroid.fdroid.privileged|\<packageName\>' | \
   awk -F',' '{print $1}' | while read app ;
   do
      appyml="${app}.yml"
      destfile="$( find "${MIRROR_DIR%%/}/../metadata/" -mindepth 1 -maxdepth 1 -name "${appyml}" -exec readlink -f {} \; )"
      test -z "${destfile}" && {
         destfile="${MIRROR_DIR%%/}/../metadata/${appyml}"
         echo "Making new file ${destfile}"
         touch "${destfile}"
      }
      descfile="${destfile%%.yml}.desc"
      srcfile="$( find "${GIT_DIR}/metadata" -name "${appyml}" -exec readlink -f {} \; )"
      echo "Need to munge ${srcfile} to ${destfile}"
      test -f "${descfile}" || { curl --silent -L "https://f-droid.org/en/packages/${app}/" | awk -F'"' '/meta name.*description/{print $4}' > "${descfile}" ; }
      if test -f "${srcfile}" ;
      then
         cp -pf "${srcfile}" "${destfile}"
         # too simple, and just one line. this should be rewritten in python bs4
         echo "Summary: '$( cat "${descfile}" )'" >> "${destfile}"
      fi
      sed -i -r -e "/ArchivePolicy:/d" -e "/AutoName:/s/^AutoName:/Name:/g;" "${destfile}"
   done
   cd "${_oldpwd}"
}

fdroid_update() {
   (
      source ~/venv1/bin/activate
      cd "${MIRROR_DIR}/.."
      fdroid update -c --use-date-from-apk
   )
}

generate_web() {
   /etc/installed/fdroid/fdroid_generate_web.py
}

main() {
   cd "${WORK_DIR}"
   test -z "${SKIP_INPUT}" && prepare_input_file
   test -z "${SKIP_METADATA}" && fix_metadata
   test -z "${SKIP_SYNC}" && sync_files
   test -z "${SKIP_UPDATE}" && fdroid_update
   test -z "${SKIP_WEB}" && generate_web
}

case "${0}" in
   "-bash")
      # dot-sourced
      :
      ;;
   *)
      main | plecho "fdroid_sync"
      ;;
esac
