#!/bin/sh
# Ver: 1.3 by Endial Fang (endial@126.com)
# 
# 此脚本用于根据传入的参数选择对应的 apk 源配置文件并复制到指定目录
# shell 执行参数，分别为 -e(命令执行错误则退出脚本) -u(变量未定义则报错) -x(打印实际待执行的命令行)
set -eu

# 检查是否有足够的权限
if [ "$(id -u)" -ne 0 ]; then
    echo "Error: This script must be run as root."
    exit 1
fi

# 获取用户传入的参数，若未传入则使用默认值 "default"
source_name=${1:-default}

# 定义源文件路径
source_file="/etc/apk/repositories.${source_name}"

# 检查源文件是否存在
if [ ! -f "$source_file" ]; then
    echo "Error: Source file $source_file does not exist."
    exit 1
fi

# 定义目标文件路径
target_file="/etc/apk/repositories"

# 复制源文件到目标文件
cp "$source_file" "$target_file"
echo "Successfully selected apt source: $source_name"
