#!/bin/sh
set -eu

APP_ROOT="${APP_ROOT:-/app}"
DATA_ROOT="${MOBILEMODELS_DATA_ROOT:-/data}"

mkdir -p "$DATA_ROOT" "$DATA_ROOT/state"

sync_missing_dir_entries() {
  src_dir="$1"
  dst_dir="$2"

  mkdir -p "$dst_dir"

  for src_entry in "$src_dir"/*; do
    [ -e "$src_entry" ] || continue
    name="$(basename "$src_entry")"
    dst_entry="$dst_dir/$name"

    if [ -d "$src_entry" ]; then
      sync_missing_dir_entries "$src_entry" "$dst_entry"
      continue
    fi

    if [ ! -e "$dst_entry" ] && [ ! -L "$dst_entry" ]; then
      mkdir -p "$(dirname "$dst_entry")"
      cp -a "$src_entry" "$dst_entry"
    fi
  done
}

refresh_workspace_builtin_entries() {
  src_dir="$1"
  dst_dir="$2"

  mkdir -p "$dst_dir"

  for rel_path in \
    brands \
    misc \
    CHANGELOG.md \
    CHANGELOG_en.md \
    LICENSE.txt
  do
    src_entry="$src_dir/$rel_path"
    dst_entry="$dst_dir/$rel_path"

    [ -e "$src_entry" ] || continue

    rm -rf "$dst_entry"
    mkdir -p "$(dirname "$dst_entry")"
    cp -a "$src_entry" "$dst_entry"
  done

  if [ -d "$src_dir/local" ]; then
    sync_missing_dir_entries "$src_dir/local" "$dst_dir/local"
  fi
}

migrate_legacy_manual_catalog() {
  legacy_dir="$DATA_ROOT/workspace/brands/local"
  target_dir="$DATA_ROOT/workspace/local"
  legacy_file="$legacy_dir/manual_catalog.json"
  target_file="$target_dir/manual_catalog.json"

  [ -d "$legacy_dir" ] || return

  mkdir -p "$target_dir"
  if [ -f "$legacy_file" ] && [ ! -f "$target_file" ]; then
    cp -a "$legacy_file" "$target_file"
  fi

  # Older builds accidentally copied local overlay files under brands/local.
  # Once the real target exists, drop the misplaced directory to avoid confusion.
  rm -rf "$legacy_dir"
}

init_path() {
  rel_path="$1"
  src_path="$APP_ROOT/$rel_path"
  dst_path="$DATA_ROOT/$rel_path"

  # Bind-mounted paths like /app/dist cannot be removed and replaced with symlinks.
  # Keep the mount in place and only ensure the runtime copy exists.
  if [ "$rel_path" = "dist" ]; then
    if [ -d "$src_path" ]; then
      if [ ! -e "$dst_path" ] && [ ! -L "$dst_path" ]; then
        mkdir -p "$(dirname "$dst_path")"
        cp -a "$src_path" "$dst_path"
      else
        sync_missing_dir_entries "$src_path" "$dst_path"
      fi
    elif [ ! -e "$dst_path" ] && [ ! -L "$dst_path" ]; then
      mkdir -p "$(dirname "$dst_path")"
      cp -a "$src_path" "$dst_path"
    fi
    return
  fi

  if [ "$rel_path" = "workspace" ] && [ -d "$src_path" ] && [ ! -L "$src_path" ]; then
    refresh_workspace_builtin_entries "$src_path" "$dst_path"
  elif [ -d "$src_path" ]; then
    if [ ! -e "$dst_path" ] && [ ! -L "$dst_path" ]; then
      mkdir -p "$(dirname "$dst_path")"
      cp -a "$src_path" "$dst_path"
    else
      sync_missing_dir_entries "$src_path" "$dst_path"
    fi
  elif [ ! -e "$dst_path" ] && [ ! -L "$dst_path" ]; then
    mkdir -p "$(dirname "$dst_path")"
    cp -a "$src_path" "$dst_path"
  fi

  if [ -L "$src_path" ]; then
    current_target="$(readlink "$src_path" || true)"
    if [ "$current_target" = "$dst_path" ]; then
      return
    fi
    rm -f "$src_path"
  else
    rm -rf "$src_path"
  fi

  ln -s "$dst_path" "$src_path"
}

for rel_path in \
  workspace \
  dist
do
  init_path "$rel_path"
done

migrate_legacy_manual_catalog
