#!/usr/bin/env bash
# backup-run/apply — one backup run: dump DB containers to a staging dir, restic-backup the
# folders + staging dir to the rest-server repo, then check a 2% data subset.
#
# params: {"repo":"rest:http://10.13.13.2:8000/<user>/","user":"…","password":"…","repoPassword":"…",
#          "planId":"…","runId":"…","pieces":[
#            {"kind":"folder","name":"apt","paths":["/srv/apt"],"excludes":["…"]},
#            {"kind":"postgres-container","name":"sdp-db","container":"…","database":"…","user":"…"}]}
# outputs: snapshotId, totalFiles, filesNew, totalBytes, dataAdded, checkOk, pieces[{name,files,bytes}]
set -euo pipefail
. "$(dirname "$0")/../lib/backup-common.sh"
P=$(cat)
backup_init "$P"
backup_reachable
PLAN="$BACKUP_PLAN"; [ -n "$PLAN" ] || die "planId is required"
RUN=$(jq -r '.runId // empty' <<<"$P"); backup_valid_id "$RUN" || die "invalid runId"
STAGE="$BACKUP_STATE_DIR/stage/$PLAN"
W=$(mktemp -d); BACKUP_ON_EXIT='rm -rf "$W"'

rm -rf "$STAGE"; mkdir -p "$STAGE"; chmod 0700 "$STAGE"

echo "== staging"
paths=(); excludes=()
NAME_RE='^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$'
n=$(jq '.pieces | length' <<<"$P")
[ "$n" -gt 0 ] || die "no pieces"
for ((i=0;i<n;i++)); do
  piece=$(jq -c ".pieces[$i]" <<<"$P")
  kind=$(jq -r '.kind // empty' <<<"$piece"); name=$(jq -r '.name // empty' <<<"$piece")
  [[ "$name" =~ $NAME_RE ]] || die "invalid piece name"
  case "$kind" in
    folder)
      while IFS= read -r path; do
        [[ "$path" =~ ^/[A-Za-z0-9._/@+-]*$ ]] || die "piece $name: invalid path"
        [ -e "$path" ] || die "piece $name: path $path does not exist"
        paths+=("$path")
      done < <(jq -r '.paths[]?' <<<"$piece")
      while IFS= read -r ex; do if [ -n "$ex" ]; then excludes+=("$ex"); fi; done < <(jq -r '.excludes[]?' <<<"$piece")
      ;;
    postgres-container)
      c=$(jq -r '.container // empty' <<<"$piece"); db=$(jq -r '.database // empty' <<<"$piece"); u=$(jq -r '.user // empty' <<<"$piece")
      [[ "$c" =~ ^[A-Za-z0-9_.-]+$ && "$db" =~ ^[A-Za-z0-9_.-]+$ && "$u" =~ ^[A-Za-z0-9_.-]+$ ]] || die "piece $name: invalid container/database/user"
      [ "$(docker inspect -f '{{.State.Running}}' "$c" 2>/dev/null || true)" = true ] || die "piece $name: container $c not running"
      echo "dumping $name"
      docker exec "$c" pg_dump -U "$u" -Fc "$db" > "$STAGE/$name.dump" || die "piece $name: pg_dump failed"
      ;;
    *) die "piece $name: unknown kind '$kind'";;
  esac
done

echo "== repository"
rc=0; restic snapshots --json >/dev/null 2> "$W/snap.err" || rc=$?
if [ "$rc" -eq 10 ]; then
  restic init >&2
elif [ "$rc" -ne 0 ]; then
  echo "restic snapshots failed (exit $rc): $(backup_last_line "$W/snap.err")" >&2; exit "$rc"
fi

echo "== backup"
args=(backup --json --host "$(hostname)" --tag "plan:$PLAN" --tag "run:$RUN")
args+=("${paths[@]}" "$STAGE")
for e in "${excludes[@]}"; do args+=(--exclude "$e"); done
rc=0; restic "${args[@]}" > "$W/backup.out" 2> "$W/backup.err" || rc=$?
if [ "$rc" -ne 0 ]; then echo "restic backup failed (exit $rc): $(backup_last_line "$W/backup.err")" >&2; exit "$rc"; fi
SUMMARY=$(jq -c 'select(.message_type=="summary")' "$W/backup.out" | tail -n 1)
SNAP=$(jq -r '.snapshot_id // empty' <<<"$SUMMARY")
[ -n "$SNAP" ] || die "restic backup produced no snapshot summary"
echo "snapshot $SNAP"

echo "== check"
rc=0; restic check --read-data-subset=2% > "$W/check.out" 2> "$W/check.err" || rc=$?
if [ "$rc" -ne 0 ]; then echo "restic check failed (exit $rc): $(backup_last_line "$W/check.err")" >&2; exit "$rc"; fi

echo "== counting pieces"
# Counts regular files under the given paths in the snapshot. --recursive is required: without it
# restic lists only direct children of a directory. On failure writes the reason to $W/count.err.
count() { # path... -> {files,bytes}
  local p
  : > "$W/ls.all"
  for p in "$@"; do
    if ! restic ls --recursive --json "$SNAP" "$p" >> "$W/ls.all" 2> "$W/ls.err"; then
      echo "restic ls failed: $(backup_last_line "$W/ls.err")" > "$W/count.err"; return 1
    fi
  done
  jq -s 'map(select(.type=="file")) | {files:length, bytes:(map(.size // 0)|add // 0)}' "$W/ls.all"
}
COUNT_ERR=""
pieces_json='[]'
for ((i=0;i<n;i++)); do
  piece=$(jq -c ".pieces[$i]" <<<"$P"); name=$(jq -r '.name' <<<"$piece")
  if [ "$(jq -r '.kind' <<<"$piece")" = folder ]; then
    mapfile -t pp < <(jq -r '.paths[]' <<<"$piece"); c=$(count "${pp[@]}") || c=""
  else
    c=$(count "$STAGE/$name.dump") || c=""
  fi
  if [ -z "$c" ]; then
    c='{"files":0,"bytes":0}'; COUNT_ERR="piece $name: $(cat "$W/count.err" 2>/dev/null)"
    echo "WARNING: $COUNT_ERR" >&2
  fi
  pieces_json=$(jq -c --arg n "$name" --argjson c "$c" '. + [{name:$n} + $c]' <<<"$pieces_json")
done

jq -n --arg s "$SNAP" --argjson sum "$SUMMARY" --argjson pc "$pieces_json" --arg ce "$COUNT_ERR" \
  '{snapshotId:$s,totalFiles:($sum.total_files_processed//0),filesNew:($sum.files_new//0),
    totalBytes:($sum.total_bytes_processed//0),dataAdded:($sum.data_added//0),checkOk:true,pieces:$pc}
   + (if $ce != "" then {countError:$ce} else {} end)' > "$SSR_OUTPUTS"
rm -rf "$STAGE"
echo "backup run $RUN ok"
