#!/bin/bash

# 定义配置文件路径
CONFIG_FILE="/etc/containers/registries.conf"

# 检查是否具有足够的权限
if [ "$EUID" -ne 0 ]; then
  echo "请使用 root 权限运行此脚本"
  exit 1
fi

# 创建或备份现有的 registries.conf 文件
if [ -f "$CONFIG_FILE" ]; then
    echo "备份现有的 $CONFIG_FILE 到 ${CONFIG_FILE}.bak"
    cp "$CONFIG_FILE" "${CONFIG_FILE}.bak"
fi


cat <<EOF > "$CONFIG_FILE"
# registries.conf - v2 格式
# 支持国内镜像加速（如阿里云）

unqualified-search-registries = ["docker.mirrors.ustc.edu.cn"]

[[registry]]
prefix = "docker.io"
insecure = false
blocked = false
location = "docker.io"
[[registry.mirror]]
location = "docker.mirrors.ustc.edu.cn"
insecure = false
EOF


echo "已更新 $CONFIG_FILE，请重启 Podman 相关服务或重新运行 Podman 命令使更改生效。"

# 可选：验证配置是否正确加载
read -p "是否现在验证配置是否正确？(y/n) " answer
if [ "$answer" == "y" ]; then
    podman info | grep registries
fi