#!/usr/bin/env /usr/bin/python3.7
#
#  Create configs/driver.yaml using lirc-lsplugins and
#  configs/confs_by_driver using irdb-get. Runs as part of the
#  build unless cross-compiling in which case it needs to be
#  run manually after installation

import sys
import os
import subprocess

here = os.path.dirname(os.path.realpath(__file__))
if os.path.exists(os.path.join(here, '..', 'lirc_options.conf')):
    # building
    tooldir = here
    configs = os.path.join('..', 'configs')
else:
    sys.path.insert(0, here);
    import config
    tooldir = BINDIR
    configs = os.path.join(DATADIR, 'lirc', 'configs')

cmd = [os.path.join(here, 'lirc-lsplugins'),
       '-U', '../plugins/.libs', '--yaml']
data = subprocess.check_output(cmd)
with open(os.path.join(configs, 'drivers.yaml'), 'w') as f:
    f.write(data.decode('utf8̈́'))

cmd = [os.path.join(here, 'irdb-get'), 'yaml-config']
data = subprocess.check_output(cmd)
with open(os.path.join(configs, 'confs_by_driver.yaml'), 'w') as f:
    f.write(data.decode('utf8̈́'))
