#!python
# Copies the given input file, removing the parts specific to the Web,
# so it can be converted to an email
import subprocess

import argparse
import re
from pathlib import Path

MAIL_GENERATOR = "/opt/homebrew/bin/mdtosendy"


parser = argparse.ArgumentParser()
parser.add_argument("source")
args = parser.parse_args()

root = Path.cwd()
print(f"Root: {root}")

source = Path(root / args.source)

if not source.is_file():
    print(f"ERROR: file not found {source}")
    exit(1)

print(f"Processing {source}")

input = source.read_text()

regex = re.compile(r"Title.*svg\">", re.S)
output = re.sub(regex, "", input)
destination = Path.cwd() / "mail" / source.name

print(f"Writing {destination}")
destination.write_text(output)

subprocess.run([MAIL_GENERATOR, "--preview", str(destination)])
