import re

with open("placeholder_small.b64", "r") as f:
    b64_data = f.read().strip()

with open("webrtc.html", "r") as f:
    html = f.read()

replacement = f"""
      const img = new Image();
      img.onload = () => {{
        setInterval(() => {{
          ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
        }}, 500);
      }};
      img.src = 'data:image/jpeg;base64,{b64_data}';
"""

# The target we want to replace
target = """
      let dot = 0;
      setInterval(() => {
        ctx.fillStyle = '#333344';
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        ctx.fillStyle = '#ffffff';
        ctx.font = '40px sans-serif';
        ctx.textAlign = 'center';
        ctx.fillText('No Camera Available' + '.'.repeat(dot % 4), canvas.width / 2, canvas.height / 2);
        dot++;
      }, 500);
"""

html = html.replace(target.strip(), replacement.strip())

with open("webrtc.html", "w") as f:
    f.write(html)
