#! /usr/bin/env python

#################################################
# Convert each worldflags symbol to a PDF file. #
#                                               #
# Author: Scott Pakin <scott.clsl@pakin.org>    #
#################################################

import concurrent.futures
import glob
import os
import re
import subprocess
import sys


def kpsewhich(fname):
    'Find a filename in the TeX tree.'
    proc = subprocess.run(['kpsewhich', fname], capture_output=True,
                          check=True, encoding='utf-8')
    return proc.stdout.strip()


# Extract the package date from worldflags.sty.
wfdate_re = re.compile(r'(\d\d\d\d)-(\d\d)-(\d\d)')
sty = kpsewhich('worldflags.sty')
with open(sty) as r:
    for ln in r:
        match = wfdate_re.search(ln)
        if match is not None:
            wfdate = match[1] + '/' + match[2] + '/' + match[3]
            break

# Acquire a list of all defined symbols.
wfdir = os.path.dirname(kpsewhich('worldflag_US.tex'))
tex_files = glob.glob(os.path.join(wfdir, '*.tex'))
flags = [os.path.splitext(os.path.basename(fn))[0][10:] for fn in tex_files]
flags.sort()

# Create a fakeworldflags.sty file.
with open('fakeworldflags.sty', 'w') as w:
    w.write('''\
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This is a generated file.  DO NOT EDIT. %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

''')
    w.write(r'''
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{fakeworldflags}[%s v0.0 Flags of the world]
\RequirePackage{graphicx}

\newcommand*{\worldflag}[1]{\includegraphics{worldflags/flag_#1}}

\endinput
'''[1:] % wfdate)

# Create and switch to a worldflags subdirectory.
try:
    os.mkdir('worldflags')
except FileExistsError:
    pass
os.chdir('worldflags')


def run_and_return_output(cmd_line):
    'Run a command line and return its combined stdout + stderr.'
    stdout = 'RUN: ' + ' '.join(cmd_line) + '\n'
    proc = subprocess.run(cmd_line, check=True,
                          stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    stdout += proc.stdout.decode('utf-8')
    return stdout


# For speed, dump a LaTeX format file with worldflags preloaded.
with open('preloaded.tex', 'w') as w:
    w.write(r'''
\documentclass{minimal}
\usepackage{worldflags}
\begin{document}
\end{document}
'''[1:])
subprocess.run([
    'pdflatex',
    '--ini',
    '&pdflatex',
    'mylatex.ltx',
    'preloaded.tex'
],
    check=True)


def generate_graphic(flag):
    'Generate a PDF file for a given worldflags flag.'
    stdout = f'*** PROCESSING {flag} ***\n\n'

    # Create a LaTeX file.
    fbase = 'flag_' + flag
    stdout += f'CREATE: {fbase}.tex\n'
    with open(fbase + '.tex', 'w') as w:
        w.write(r'''
\documentclass{minimal}
\usepackage{worldflags}
\begin{document}
\worldflag[width=9pt]{%s}
\end{document}
''' % flag)

    # Compile the LaTeX file to PDF.
    stdout += run_and_return_output(['pdflatex', '&mylatex', fbase + '.tex'])

    # Crop the PDF file.
    stdout += run_and_return_output(['pdfcrop', fbase + '.pdf'])

    # Overwrite the original PDF file with the cropped version.
    stdout += f'RUN: mv {fbase}-crop.pdf {fbase}.pdf\n'
    os.rename(f'{fbase}-crop.pdf', f'{fbase}.pdf')

    # Output the buffered output.
    stdout += '\n'
    print(stdout)


# Concurrently process all symbols.
with concurrent.futures.ProcessPoolExecutor() as executor:
    executor.map(generate_graphic, flags)
