#!/bin/bash
# Copyright 2026 ANNE Media - https://anne.media
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# sync.sh -- mirror the pristine Slackware tree into the AM·OS build tree,
#           then mirror source-am/ into source/z/.
#
# sync.sh does not build or sign anything. It mirrors the upstream
# tree into $BUILD/slackware64 and $BUILD/source, and mirrors
# $BUILD/source-am into $BUILD/source/z.
#
# The one exception to "source-am/ is read-only" is
# source-am/annode/anne-node.zip: its inner anne-node.jar is
# pruned of non-Linux-x86-64 binaries in place before the mirror
# runs. See the "annode jar pruning" section below.
#
# Kernel source is taken from a single, explicitly named directory
# under $REPO/source (KERNEL_SRC_REL, default k72). Other k<digits>
# directories that may exist alongside it are ignored.

set -eu
BUILD_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
REPO="$(cd -- "$BUILD_DIR/../../../slackware-current/repo" && pwd -P)"
[[ -d "$REPO" ]] || { echo "REPO not found: $REPO (BUILD_DIR=$BUILD_DIR)" >&2; exit 1; }
BUILD="$(cd -- "$BUILD_DIR/.." && pwd -P)"
[[ -d "$BUILD" ]] || { echo "BUILD not found: $BUILD (BUILD_DIR=$BUILD_DIR)" >&2; exit 1; }
stale_count=0
# Kernel source directory under $REPO/source. Only this one is mirrored.
KERNEL_SRC_REL="${KERNEL_SRC_REL:-k72}"

PKG_LIST="$BUILD/packages.list"
SOURCE_Z="$BUILD/source-am"
PROD="$BUILD/slackware64"
SRCDST="$BUILD/source"

export REPO PROD

die() { echo "ERROR: $*" >&2; exit 1; }
log() { echo ">>> $*"; }

[ -d "$REPO" ]     || die "repo not found: $REPO"
[ -f "$PKG_LIST" ] || die "packages.list not found: $PKG_LIST"

# Shared blacklist: BLACKLIST_PATTERNS + is_blacklisted() live in
# blacklist.sh next to this script, so sync.sh (mirror side) and
# enter_chroot.sh (install/upgrade side) cannot drift apart. Edit only
# blacklist.sh. Semantics (freeze, not purge) are documented there.
BLACKLIST_SH="$BUILD_DIR/blacklist.sh"
[ -f "$BLACKLIST_SH" ] || die "blacklist.sh not found at $BLACKLIST_SH"
# shellcheck source=blacklist.sh
. "$BLACKLIST_SH"

pkgname() {
  basename "$1" | sed -E 's/-[0-9][^-]*-[^-]*-[^-]*\.t[gx]z$//'
}
is_kernel_pkg() {
  case "$1" in
    kernel-generic|kernel-headers|kernel-source) return 0 ;;
    *) return 1 ;;
  esac
}

# Copy the .txt description that sits next to a pristine-tree package
# into the production series dir. Never touches .asc (Slackware's sig).
copy_txt_for() {
  local repo_file="$1" dest="$2" repo_txt prod_txt
  repo_txt="${repo_file%.*}.txt"
  prod_txt="$dest/$(basename "${repo_file%.*}").txt"
  [ -f "$repo_txt" ] || return 0
  if [ ! -f "$prod_txt" ] || [ "$repo_txt" -nt "$prod_txt" ]; then
    cp -a "$repo_txt" "$dest/"
  fi
}

# Locate the source dir for a binary package.
# Kernel packages all resolve to KERNEL_SRC_REL. Everything else is
# probed against the pristine source tree the usual way.
find_source_dir() {
  local pkg="$1" series="$2" d

  if is_kernel_pkg "$pkg"; then
    echo "$KERNEL_SRC_REL"
    return 0
  fi

  if [ -d "$REPO/source/$series/$pkg" ]; then
    echo "$series/$pkg"
    return 0
  fi

  d=$(find "$REPO/source" -type f -path "*/slack-desc/$pkg" -print -quit 2>/dev/null || true)
  if [ -n "$d" ]; then
    echo "$(dirname "$(dirname "${d#$REPO/source/}")")"
    return 0
  fi

  d=$(find "$REPO/source" -type f -name "slack-desc.${pkg}" -print -quit 2>/dev/null || true)
  if [ -n "$d" ]; then
    echo "$(dirname "${d#$REPO/source/}")"
    return 0
  fi

  return 1
}

# ================= packages =================
log "Syncing packages"
mkdir -p "$PROD"

mapfile -t WANTED < <(grep -vE '^[[:space:]]*(#|$)' "$PKG_LIST" | awk '{print $1}')

# --- Remove stale packages from slackware production/ ---
# Blacklisted packages are exempt: an existing blacklisted binary is
# frozen in place and must never be purged as "stale".
for f in "$PROD"/*/*.t[gx]z; do
  [ -f "$f" ] || continue
  name=$(pkgname "$f")

  if is_blacklisted "$name"; then
    log "  keeping blacklisted: ${f#$PROD/}"
    continue
  fi

  if ! printf '%s\n' "${WANTED[@]}" | grep -qx "$name"; then
    log "  removing stale: ${f#$PROD/}"
    rm -f "$f" "${f}.asc" "${f%.*}.txt"
  fi
done

# --- Add / replace / keep packages ---
missing=()
added=0
replaced=0
kept=0
frozen=0

for pkg in "${WANTED[@]}"; do
  # Blacklisted binaries are frozen: if one already exists in $PROD we
  # leave it alone (no upgrade, no removal); if it does not exist we do
  # not add it. Either way we never consult the pristine tree for it,
  # so a missing upstream blacklisted package cannot fail the sync.
  if is_blacklisted "$pkg"; then
    prod_file=$(find "$PROD" -maxdepth 2 -type f \
        \( -name "${pkg}-[0-9]*.txz" -o -name "${pkg}-[0-9]*.tgz" \) \
        | head -n1 || true)

    if [ -n "${prod_file:-}" ]; then
      log "  = keeping blacklisted: $(basename "$prod_file")"
      frozen=$((frozen + 1))
    else
      log "  - skipping blacklisted: $pkg"
    fi
    continue
  fi
  # search_paths=("$REPO/slackware64" "$REPO/extra")
  search_paths=("$REPO/slackware64")

  repo_file=$(find -L "${search_paths[@]}" -maxdepth 4 -type f \
      \( -name "${pkg}-[0-9]*.txz" -o -name "${pkg}-[0-9]*.tgz" \) \
      | sort -V | tail -n1 || true)

  if [ -z "${repo_file:-}" ]; then
    missing+=("$pkg")
    continue
  fi

  prod_file=$(find "$PROD" -maxdepth 2 -type f \
      \( -name "${pkg}-[0-9]*.txz" -o -name "${pkg}-[0-9]*.tgz" \) \
      | head -n1 || true)

  case "$repo_file" in
    "$REPO/z/"*)               dest="$PROD/z" ;;
    "$REPO/slackware64/"*)         dest="$PROD/$(basename "$(dirname "$repo_file")")" ;;
    *)                             dest="$PROD/unknown" ;;
  esac
  mkdir -p "$dest"

  if [ -z "${prod_file:-}" ]; then
    cp -a "$repo_file" "$dest/"
    copy_txt_for "$repo_file" "$dest"
    log "  + $(basename "$repo_file")"
    added=$((added + 1))
    continue
  fi

  if [ "$(basename "$repo_file")" != "$(basename "$prod_file")" ]; then
    log "  ^ $(basename "$prod_file") -> $(basename "$repo_file")"
    rm -f "$prod_file" "${prod_file}.asc" "${prod_file%.*}.txt"
    cp -a "$repo_file" "$dest/"
    copy_txt_for "$repo_file" "$dest"
    replaced=$((replaced + 1))
  else
    # Unchanged package -- still make sure the .txt is present/fresh.
    copy_txt_for "$repo_file" "$dest"
    kept=$((kept + 1))
  fi
done


# ================= sources =================
log "Syncing sources"
mkdir -p "$SRCDST"

WANTED_SRC=()
MISSING_SRC=()
# Blacklisted packages whose source dir could not be located upstream.
# These are frozen in place and are *not* an error -- they are reported
# separately from MISSING_SRC so a genuine upstream-source gap is not
# lost in a pile of expected blacklist entries.
BLACKLISTED_MISSING_SRC=()
# srcrel -> 1 for every source dir whose binary package is blacklisted.
# Populated via the binary -> source mapping (find_source_dir), which is
# the only reliable link between a package name and its source dir.
declare -A BLACKLISTED_SRC=()
seen=" "

for series_dir in "$PROD"/*/; do
  [ -d "$series_dir" ] || continue
  series=$(basename "$series_dir")
  case "$series" in
    branding|z) continue ;;
  esac

  for txz in "$series_dir"*.t[gx]z; do
    [ -f "$txz" ] || continue
    pkg=$(pkgname "$txz")

    srcrel=$(find_source_dir "$pkg" "$series") || {
      # A missing upstream source is only an error for non-blacklisted
      # packages. Blacklisted packages are frozen and never looked up,
      # so their absent upstream source is expected, not a failure --
      # but we still record it so the summary can show which frozen
      # package(s) have no source to freeze against.
      if is_blacklisted "$pkg"; then
        BLACKLISTED_MISSING_SRC+=("$series/$pkg")
      else
        MISSING_SRC+=("$series/$pkg")
      fi
      continue
    }

    # Record the freeze on the *source* side. The srcrel still goes
    # into WANTED_SRC below, so the stale-source sweep will treat it
    # as wanted (preserved) and the refresh loop will skip it.
    if is_blacklisted "$pkg"; then
      BLACKLISTED_SRC["$srcrel"]=1
    fi

    case "$seen" in
      *" $srcrel "*) continue ;;
    esac
    seen="$seen$srcrel "
    WANTED_SRC+=("$srcrel")
  done
done

log "  ${#WANTED_SRC[@]} source dirs derived from slackware64 production/"
if [ "${#BLACKLISTED_SRC[@]}" -gt 0 ]; then
  log "  ${#BLACKLISTED_SRC[@]} source dir(s) frozen by blacklist:"
  for s in "${!BLACKLISTED_SRC[@]}"; do log "    $s"; done
fi
if [ "${#BLACKLISTED_MISSING_SRC[@]}" -gt 0 ]; then
  log "  ${#BLACKLISTED_MISSING_SRC[@]} blacklisted package(s) with no upstream source (frozen in place, not an error):"
  for m in "${BLACKLISTED_MISSING_SRC[@]}"; do log "    $m"; done
fi
if [ "${#MISSING_SRC[@]}" -gt 0 ]; then
  log "  ${#MISSING_SRC[@]} package(s) still have no upstream source:"
  for m in "${MISSING_SRC[@]}"; do log "    $m"; done
fi

is_wanted_or_ancestor() {
  local rel="$1" want
  for want in "${WANTED_SRC[@]}"; do
    case "$want" in
      "$rel"|"$rel"/*) return 0 ;;
    esac
  done
  return 1
}


for d in "$SRCDST"/*/ "$SRCDST"/*/*/; do
  [ -d "$d" ] || continue
  rel="${d#$SRCDST/}"; rel="${rel%/}"
  [ -z "$rel" ] && continue
  case "$rel" in
    z|z/*) continue ;;
  esac

  # A safety net: a source dir whose name matches a blacklist
  # pattern is frozen by name even when no binary currently maps to it
  # (e.g. the binary was dropped from packages.list but the source is
  # meant to stay). The authoritative mapping is BLACKLISTED_SRC, which
  # is checked via is_wanted_or_ancestor below; this catches the
  # case where a dir literally happens to be named after a pattern.
  base=$(basename "$rel")
  if is_blacklisted "$base"; then
    log "  keeping blacklisted source: $rel"
    continue
  fi

  if ! is_wanted_or_ancestor "$rel"; then
    log "  removing stale: $rel"
    rm -rf "$d"
    stale_count=$((stale_count + 1))
  fi
done

log "  refreshing source/ (${#WANTED_SRC[@]} dirs)"
src_refreshed=0
for srcrel in "${WANTED_SRC[@]}"; do
  # Blacklisted source dirs are frozen: keep whatever version is
  # already in $SRCDST, never rm -rf + cp -a over it from $REPO/source.
  if [ -n "${BLACKLISTED_SRC[$srcrel]+x}" ]; then
    log "  keeping blacklisted source: $srcrel"
    continue
  fi

  src="$REPO/source/$srcrel"
  dst="$SRCDST/$srcrel"
  [ -d "$src" ] || continue
  rm -rf "$dst"
  mkdir -p "$(dirname "$dst")"
  cp -a "$src" "$dst"
  src_refreshed=$((src_refreshed + 1))
done

# ================= annode jar pruning =================
# source-am/annode/anne-node.zip is the AM·OS Node runtime bundle.
# Inside it lives anne-node.jar, which ships native binaries for many
# platforms. AM·OS targets linux-x86-64 only, so everything else is
# pruned here, in source-am, before the source-am mirror below
# copies the zip into source/z/. The mirror then carries the
# pruned jar.
#
# This is the one and only place where this script writes into
# source-am/. Everything else treats source-am/ as read-only.
#
# Removed from anne-node.jar:
#   tor/mac/            (tor/ itself and tor/linux/ are kept)
#   tor/win/
#   win32-x86-64/       (jar root; holds shabal.dll)
#   yt-dlp/             (jar root)
#   darwin/             (jar root; holds libshabal.dylib)
#   lib/JOCL_2_0_1-windows-x86.dll
#   lib/JOCL_2_0_1-windows-x86_64.dll
#   lib/libJOCL_2_0_1-apple-x86_64.dylib
#   lib/libJOCL_2_0_1-linux-x86_64.so
#                       (JOCL unused by anne-node; all builds removed)
#   com/formdev/flatlaf/natives/*
#                       (FlatLaf is GUI-only; unused in headless mode)
#   com/sun/jna/<every platform subdir except linux-x86-64>
#                       (JNA bootstrap: keep only libjnidispatch.so
#                        for the one platform we ship)
#
#   linux-x86-64/libshabal.so
#   com/sun/jna/linux-x86-64/libjnidispatch.so
#
# After pruning, the jar is scanned for any remaining .so / .a / .la
# entries, and every match is echoed so we can see which native
# libraries survive. If the zip or the jar is missing we skip
# cleanly (the later source-am section still hard-fails if
# $SOURCE_Z itself is gone).

prune_annode_jar() {
  local zip_path="$1"
  local tmpdir jar_path entry d f found subdir

  command -v unzip >/dev/null 2>&1 \
    || die "unzip not found (required to prune $zip_path)"
  command -v zip >/dev/null 2>&1 \
    || die "zip not found (required to prune $zip_path)"

  tmpdir=$(mktemp -d)
  # shellcheck disable=SC2064
  trap "rm -rf '$tmpdir'" EXIT INT TERM

  # Pull only the inner jar out of the outer container. -o overwrites
  # any stale copy from a previous interrupted run.
  unzip -q -o "$zip_path" anne-node.jar -d "$tmpdir"
  jar_path="$tmpdir/anne-node.jar"
  [ -f "$jar_path" ] || die "anne-node.jar not found inside $zip_path"

  # --- delete directory subtrees inside the jar ---
  # Info-ZIP 3.0's "dir/*" also removes the "dir/" entry itself, so a
  # second call for "dir/" is redundant and errors with "Nothing to
  # do!" (which Info-ZIP prints to stdout, not stderr -- that is why
  # the earlier 2>/dev/null guard did not silence it). One call per
  # dir is correct and sufficient.
  for d in tor/mac tor/win win32-x86-64 yt-dlp darwin; do
    zip -q -d "$jar_path" "$d/*" >/dev/null 2>&1 || true
  done

  # --- JOCL: unused by anne-node. Delete all builds, including the
  #     linux-x86-64 one. ---
  for f in \
      lib/JOCL_2_0_1-windows-x86.dll \
      lib/JOCL_2_0_1-windows-x86_64.dll \
      lib/libJOCL_2_0_1-apple-x86_64.dylib \
      lib/libJOCL_2_0_1-linux-x86_64.so; do
    zip -q -d "$jar_path" "$f" >/dev/null 2>&1 || true
  done

  # --- FlatLaf: desktop integration natives only (GTK theme colors,
  #     HiDPI hints, window decorations). Useless in headless mode;
  #     anne-node does not run a Swing UI. Delete the entire natives
  #     directory in one shot so no platform variant can leak through
  #     if upstream ever ships Windows/macOS builds alongside the
  #     Linux one. FlatLaf's Java classes live at
  #     com/formdev/flatlaf/*.class and are untouched by this. ---
  zip -q -d "$jar_path" 'com/formdev/flatlaf/natives/*' >/dev/null 2>&1 || true

  # shabal
  zip -q -d "$jar_path" 'linux-x86-64/libshabal.so' >/dev/null 2>&1 || true
  # --- JNA: keep only the linux-x86-64 bootstrap (libjnidispatch.so).
  #     Every other subdirectory under com/sun/jna/ holds a native for
  #     an OS/arch we do not ship. The subdirectory set is derived
  #     from the jar itself, so upstream additions (linux-riscv128,
  #     haiku, ...) are handled automatically with no list to
  #     maintain. Classes live flat at com/sun/jna/*.class and are
  #     never matched -- only one-level subdirectories are considered.
  #
  #     NOTE: JNA is *not* a GUI library. Headless mode does not make
  #     it unnecessary. libjnidispatch.so is required if any code in
  #     the jar (or a dependency) calls Native.load(...). This
  #     whitelist is therefore deliberately conservative: it keeps
  #     the platform we actually ship and strips everything else. If
  #     a source audit later proves JNA is unused, replace this block
  #     with a hard delete of the whole tree:
  #         zip -q -d "$jar_path" 'com/sun/jna/*' >/dev/null 2>&1 || true
  while IFS= read -r subdir; do
    # [ "$subdir" = "com/sun/jna/linux-x86-64" ] && continue
    zip -q -d "$jar_path" "$subdir/*" >/dev/null 2>&1 || true
  done < <(unzip -Z1 "$jar_path" 2>/dev/null \
           | sed -n 's:^\(com/sun/jna/[^/]*\)/.*:\1:p' \
           | sort -u)

  # --- scan and echo any surviving native libraries ---
  log "  native libs left in anne-node.jar:"
  found=0
  while IFS= read -r entry; do
    [ -n "$entry" ] || continue
    case "$entry" in
      *.so|*.so.*|*.a|*.la)
        echo "    $entry"
        found=$((found + 1))
        ;;
    esac
  done < <(unzip -Z1 "$jar_path" 2>/dev/null || true)
  [ "$found" -gt 0 ] || echo "    (none)"

  # --- put the pruned jar back into the outer zip, in place ---
  # No `rm` of the original, no `zip -r .` recompress: zip updates the
  # member in place and renames atomically on success, so ^C here
  # leaves $zip_path intact rather than truncated.
  ( cd "$tmpdir" && zip -q "$zip_path" anne-node.jar )

  rm -rf "$tmpdir"
  trap - EXIT INT TERM
}

ANNODE_ZIP="$SOURCE_Z/annode/anne-node.zip"
if [ -f "$ANNODE_ZIP" ]; then
  log "Pruning annode jar in $ANNODE_ZIP"
  prune_annode_jar "$ANNODE_ZIP"
else
  log "Pruning annode jar: $ANNODE_ZIP not found, skipping"
fi

# ================= source-am =================
# source-am/ holds AM·OS's out-of-tree source contributions. It is
# mirrored wholesale into source/z/ so the production source tree
# is self-contained: make_world can build everything it needs from
# $SRCDST alone, with no dependency on source-am/ existing at all.
#
# Apart from the annode jar pruning step above (which edits
# source-am/annode/anne-node.zip in place), source-am/ is
# read-only here: it is only ever the source side of a cp, nothing
# else in this script writes to it, renames in it, or deletes from it.
#
# source/z/ is wiped and re-created each run, so removing a recipe
# from source-am/ removes it from source/z/ on the next sync.
# This step runs last, after the stale-source cleanup above, so that
# cleanup can never see source/z/ and never touches it.
#
# If $SOURCE_Z does not exist we DO NOT wipe $SRCDST/z. A
# missing source-am is a hard error -- we refuse to run rather
# than destroy state we cannot restore.

[ -d "$SOURCE_Z" ] \
  || die "source-am not found at $SOURCE_Z (refusing to wipe $SRCDST/z)"

log "Mirroring source-am into source/z"
rm -rf "$SRCDST/z"
mkdir -p "$SRCDST/z"
cp -a "$SOURCE_Z/." "$SRCDST/z/"

# count files and dirs, not only dirs, so the log tells the truth
# about what actually landed in source/z.
z_count=$(find "$SRCDST/z" -mindepth 1 -maxdepth 1 | wc -l)
log "  $z_count entr(y|ies) mirrored"

# ================= summary =================
total_pkgs=$(find "$PROD" -name '*.t[gx]z' | wc -l)
total_txts=$(find "$PROD" -name '*.txt' | wc -l)
total_src=${#WANTED_SRC[@]}


if [ "${#missing[@]}" -gt 0 ]; then
  echo
  echo "ERROR: ${#missing[@]} package(s) not found in pristine tree:"
  printf '  %s\n' "${missing[@]}"
  exit 1
fi

echo
log "Sync complete"
echo "  packages:      ${total_pkgs} total (added: ${added}, replaced: ${replaced}, unchanged: ${kept}, frozen: ${frozen})"
echo "  descriptions:  ${total_txts} .txt file(s) present"
echo "  sources:       ${total_src} dirs mirrored (refreshed: ${src_refreshed}, frozen: ${#BLACKLISTED_SRC[@]})"
echo "  source-am:  ${z_count} entr(y|ies) mirrored to source/z"
echo "  stale removed: $stale_count source dir(s)"
echo "  kernel source: $KERNEL_SRC_REL"
echo "  Slackware64 production:    $PROD"
echo "  source:        $SRCDST"