# arg 1:  the new package version
# arg 2:  the old package version
post_upgrade() {
  if [ "$(vercmp $2 2:r12246.8b07dc845-2)" -le 0 ]; then
      cat << EOF
===> Upstream dropped support for MySQL. See
===> https://web.archive.org/web/20251004101616/https://community.tt-rss.org/t/support-for-mysql-is-no-more/6897
===> for more details and tips for migration to PostgreSQL.
EOF
  fi

  if [ "$(vercmp $2 2:r10820.4e81233ac-1)" -le 0 ]; then
    cat << EOF
===> tt-rss switched back to PHP 8.
===> Please reconfigure PHP-specific bits:
===>   * Enable required modules in /etc/php/php.ini
===>   * Start/enable php-fpm systemd service
===> See https://wiki.archlinux.org/index.php/TT-RSS for more information.
===> ********************************************************************
===> TT-RSS has restructured its config files, please consult
===>   https://tt-rss.org/docs/Global-Config.html
===> on the config file migration plan.
EOF
  fi

  if [ "$(vercmp $2 2:r11392.9a0dcdd6c)" -le 0 ]; then
      cat << EOF
===> Upstream changed the location for cached feed icons. Existing ones
===> will be moved to the new location after all feeds are updated in
===> the next batch update. See https://web.archive.org/web/20251004102641/https://community.tt-rss.org/t/favicons-cache-implementation-overhaul/5698 for
===> more details.
EOF
  fi

  if [ "$(vercmp $2 2:r11408.fa9c614ff)" -le 0 ]; then
      cat << EOF
===> Upstream split af_readability out as a standalone plugin. Please
===> follow https://web.archive.org/web/20251004102828/https://community.tt-rss.org/t/splitting-af-readability-into-a-separate-repo/5739 for how to add it back
===> if you need it.
EOF
  fi

  # Migrate local components to the new location
  if [ "$(vercmp $2 2:r12836.f058febba)" -le 0 ]; then
    echo "===> Migrating local plugins, templates, and themes to /var/lib/tt-rss/..."
    migrated=0
    for dir in plugins.local templates.local themes.local; do
      src="/usr/share/webapps/tt-rss/$dir"
      dst="/var/lib/tt-rss/$dir"
      if [ -d "$src" ]; then
        while IFS= read -r -d '' item; do
          dst_item="$dst/${item##*/}"
          # Don't overwrite existing files
          if [ ! -e "$dst_item" ]; then
            mv -v "$item" "$dst/" && migrated=1
          else
            echo "===> Skip: $item exists at $dst_item"
          fi
        done < <(find "$src" -mindepth 1 -maxdepth 1 -print0)
        rmdir "$src" 2>/dev/null || true
      fi
    done

    if [ "$migrated" -eq 1 ]; then
      cat << EOF
===> Local components (plugins, templates, and/or themes) have been migrated to /var/lib/tt-rss/.
===> Please verify that your custom components are working correctly.
EOF
    fi
  fi
}
