#!/bin/bash

# 设置搜索的起始目录
start_dir="./package/Applications"
feeds_dir="./feeds"

# 搜索所有以luci-app-开头的文件夹
echo "Searching for folders starting with 'luci-app-' in $start_dir..."
luci_app_folders=$(find $start_dir -type d -name "luci-app-*")

# 为了方便比较，将结果存储到数组中
declare -a luci_app_names

# 提取并存储每个找到的文件夹名称
for folder in $luci_app_folders; do
    folder_name=$(basename $folder)
    luci_app_names+=("$folder_name")
done

# 如果没有找到任何文件夹，提前退出
if [ ${#luci_app_names[@]} -eq 0 ]; then
    echo "No 'luci-app-' folders found in $start_dir."
    exit 0
fi

# 在feeds目录下搜索所有文件夹，查找与luci_app_names数组中名称相同的文件夹，并删除它们
echo "Searching for matching folders in $feeds_dir..."
for app_name in "${luci_app_names[@]}"; do
    # 使用find命令在feeds目录下搜索同名文件夹，并删除找到的文件夹
    find $feeds_dir -type d -name "$app_name" -exec echo Deleting {} \; -exec rm -rf {} \;
done

echo "Operation completed."
