#!/usr/bin/env python3
"""Regenerates 58k_files.js: the list of 58k_*.js files on disk, so the 58k pages load only files that exist
(a missing kit or lane file would otherwise log a 'Failed to load resource' console error on file://).
Run it after adding any 58k_*.js file:  python3 _tools/58k_kits.py"""
import os, json, time
here = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
files = sorted(f for f in os.listdir(here) if f.startswith('58k_') and f.endswith('.js') and f != '58k_files.js')
sfx = os.path.isdir(os.path.join(here, 'sfx'))
out = '/* generated by _tools/58k_kits.py at %s: the 58k_*.js files on disk (the loader skips anything not listed). */\nwindow.K58_FILES = %s;\nwindow.K58_SFX = %s;\n' % (time.strftime('%d %b %H:%M'), json.dumps(files), 'true' if sfx else 'false')
open(os.path.join(here, '58k_files.js'), 'w').write(out)
print(len(files), 'files listed in 58k_files.js')
