#!/bin/bash

find . -type f -name "app.json" -print0 | while IFS= read -r -d $'\0' file; do
    temp_file=$(mktemp)
    
    jq '
    if (has("Tags") and (.Tags | type == "string")) then
      .Tags |= (
         split(";") as $tags |
         if ($tags == [""]) then
             "debian"
         else
             if (
                  ($tags | index("ubuntu")) != null and 
                  ($tags | index("deepin")) != null and 
                  ($tags | index("debian")) == null
                ) then
                 ($tags + ["debian"]) | unique | join(";")
             else
                 $tags | join(";")
             end
         end
      )
    else
      if (has("Tags")) then .
      else . + {Tags: "debian"} end
    end
    ' "$file" > "$temp_file"
    
    if ! cmp -s "$file" "$temp_file"; then
        mv "$temp_file" "$file"
        echo "[修改成功] 已更新文件：$file"
    else
        rm "$temp_file"
        echo "[无需修改] 跳过文件：$file"
    fi
done
