File size: 3,120 Bytes
df15565
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/bash

# Function to check if config.json already exists in a subfolder and if an image file exists
check_exists() {
    local folder="$1"
    local config_file="$folder/config.json"
    local icon_file=$(find "$folder" -maxdepth 1 -type f \( -iname "*.jpg" -o -iname "*.gif" -o -iname "*.png" \) -print -quit)

    if [ -f "$config_file" ]; then
        if [ -n "$icon_file" ]; then
            icon=$(basename "$icon_file")
        fi
        return 0
    else
        return 1
    fi
}

# Function to update config.json if there are missing fields
update_config() {
    local folder="$1"
    local config_file="$folder/config.json"

    local model_file=$(find "$folder" -maxdepth 1 -type f -name "*.pth" -exec basename {} \; -quit)
    local feat_index_file=$(find "$folder" -maxdepth 1 -type f -name "*.index" -exec basename {} \; -quit)
    local feat_npy_file=$(find "$folder" -maxdepth 1 -type f -name "*.npy" -exec basename {} \; -quit)

    # Remove the .pth extension from the model file name
    local name=$(basename "$model_file" .pth)

    # Replace underscores with spaces, remove "RVC" or "(RVC)", "Epoch", square brackets, parentheses, and curly brackets from the folder name,
    # and replace any occurrences of consecutive spaces with a single space
    local note=$(basename "$folder" | sed 's/_/ /g; s/\(RVC\)//g; s/RVC//g; s/[(){}]//g; s/\[\]//g; s/Epoch//g; s/  */ /g; s/^ //; s/ $//')

    # Set other configuration values
    local speaker_id=0
    local author="Rubin"
    local source="Rubin"
    local icon=""
    
    if check_exists "$folder"; then
        if [ -n "$icon" ]; then
            icon=$(basename "$icon_file")
        fi

        # Update the fields for the existing config.json
        # Creating a new json string with the updated fields
        local new_config=$(cat <<EOF
{
    "model": "$model_file",
    "feat_index": "$feat_index_file",
    "feat_npy": "$feat_npy_file",
    "speaker_id": $speaker_id,
    "name": "$name",
    "author": "$author",
    "source": "$source",
    "note": "$note",
    "icon": "$icon"
}
EOF
        )
        # Write the new config to the file
        echo "$new_config" > "$config_file"
        echo "Updated config.json file in $folder"
    else
        # Create the config.json file
        cat > "$folder/config.json" <<EOF
{
    "model": "$model_file",
    "feat_index": "$feat_index_file",
    "feat_npy": "$feat_npy_file",
    "speaker_id": $speaker_id,
    "name": "$name",
    "author": "$author",
    "source": "$source",
    "note": "$note",
    "icon": "$icon"
}
EOF
        if [ -n "$icon" ]; then
            echo "config.json file created successfully in $folder with icon: $icon"
        else
             echo "config.json file created successfully in $folder (no icon)"
        fi
    fi

    # Process subfolders recursively
    while IFS= read -r -d '' subfolder; do
        update_config "$subfolder"
    done < <(find "$folder" -mindepth 1 -type d -print0)
}

# Set the folder path to the current directory
folder_path=$(pwd)

# Update config.json files recursively in subfolders
update_config "$folder_path"