#!/usr/bin/env python

import logging
import re
import sys


log_level = logging.DEBUG if "-v" in sys.argv else logging.INFO
logging.basicConfig(level=log_level)
log = logging.getLogger(__name__)

def load(path):
    with open(path, 'rb') as f:
        return [line.strip() for line in f.readlines()]

images = load('images.txt')
backgrounds = load('backgrounds.txt')

def replace_id(match):
    attr = match.group(1).lower()
    if attr == 'bg':
        attr = 'background'
        db = backgrounds
    else:
        db = images

    group = match.group(2)
    quote_char = group[0]
    asset_id = int(group[1:-1])

    if asset_id >= len(db):
        log.debug("{} #{} not found".format(attr, asset_id))
        return match.group(0)

    url = db[asset_id]
    log.debug("{}={} -> {}".format(attr, asset_id, url))

    return '{}={}{}{}'.format(attr, quote_char, url, quote_char)

html = re.sub(
    r'\b(src|bg)\s*=\s*("\d+"|\'\d+\')',
    replace_id,
    sys.stdin.read(),
    flags=re.IGNORECASE,
)

print(html)
