#!/usr/bin/env bash
set -euo pipefail

if [ "$#" -ne 2 ]; then
  echo "用法: $0 <source-image> <target-filename>" >&2
  exit 2
fi

SRC="$1"
NAME="$2"
IMG_DIR="/home/ittoview/src/data/image"
BASE_URL="https://itto.topwind.top/learning-images/"

if [ ! -f "$SRC" ]; then
  echo "源文件不存在: $SRC" >&2
  exit 1
fi

mkdir -p "$IMG_DIR"
cp "$SRC" "$IMG_DIR/$NAME"

echo "--- 本地目录确认 ---"
ls -lh "$IMG_DIR/$NAME"

echo "--- 公网线上挂载目录确认 ---"
HTML="$(curl -sS --max-time 8 "$BASE_URL")"
NUMBER_PREFIX="${NAME%%-*}-"
if printf '%s\n' "$HTML" | grep -F "$NAME" >/dev/null || printf '%s\n' "$HTML" | grep -F "$NUMBER_PREFIX" >/dev/null; then
  printf '%s\n' "$HTML" | grep -F "$NAME" || printf '%s\n' "$HTML" | grep -F "$NUMBER_PREFIX"
  echo "线上可见: $NAME"
else
  echo "线上未确认可见: $NAME" >&2
  exit 1
fi
