#!/bin/bash

replaceVAPIDKeys() 
{
    local file="$1"

    # Only scan for VAPID placeholders that actually exist in the file
    local existing_placeholders=$(runInstallOp grep -oE 'RANDOMIZEDVAPID[0-9]*' "$file" 2>/dev/null | sort -u)
    
    if [[ -n "$existing_placeholders" ]]; then
        while IFS= read -r placeholder; do
            if [[ -n "$placeholder" ]]; then
                local vapid_key
                vapid_key=$(openssl rand -base64 32 | tr -d '+/=' | tr -cd '[:alnum:]')
                
                runInstallOp sed -i "s/${placeholder}/${vapid_key}/g" "$file"
                checkSuccess "Updated ${placeholder} in $(basename "$file") with a new VAPID key."
            fi
        done <<< "$existing_placeholders"
    fi
}
