r"""The site's pictures: the frames of `app.php demo` drawn as a terminal (tools/site/demo.txt, refreshed with
`bin/jumphp run caramail-server demo | sed 's/\x1b\[[0-9;]*m//g' > tools/site/demo.txt`)."""
import sys
from pathlib import Path
from PIL import Image, ImageDraw, ImageFont

app = Path(sys.argv[1]) if len(sys.argv) > 1 else Path(__file__).resolve().parents[2]
font = ImageFont.truetype(sys.argv[2] if len(sys.argv) > 2 else '/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf', 18)
lines = (app / 'tools/site/demo.txt').read_text().splitlines()[:26]

def terminal(w, h, path):
    img = Image.new('RGB', (w, h), '#101418')
    d = ImageDraw.Draw(img)
    d.rectangle((0, 0, w, 34), fill='#2a2f36')
    for i, c in enumerate(['#ff5f56', '#ffbd2e', '#27c93f']):
        d.ellipse((14 + i * 22, 11, 26 + i * 22, 23), fill=c)
    d.text((90, 8), 'caramail-server demo', font=font, fill='#c0c0c0')
    y = 46
    for line in lines:
        who, _, rest = line.partition(' ')
        rest = rest.strip()
        d.text((16, y), f'{who:<6}', font=font, fill='#ffbd2e')
        colour = '#7ee787' if rest.startswith('→') else '#79c0ff'
        d.text((90, y), rest.replace('⇥', '·'), font=font, fill=colour)
        y += 24
        if y > h - 24:
            break
    img.save(path, optimize=True)

terminal(1280, 720, app / 'assets/thumbnail.png')
terminal(1200, 630, app / 'site/images/og.png')
terminal(1280, 680, app / 'site/images/demo.png')
icon = Image.new('RGB', (64, 64), '#101418')
ImageDraw.Draw(icon).text((10, 18), '>_', font=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf', 28), fill='#7ee787')
icon.save(app / 'site/images/favicon.png')
print('ok')
