#! /usr/bin/python3
# -*- coding: utf-8 -*-

# Called from buildpyb
# Arg: device (e.g. '/dev/pyboard')

import sys
import os, os.path
mp = os.getenv('MPDIR')
sys.path.append(''.join((mp, '/tools')))
import pyboard

errmsg = 'Usage pyb_check device'
d = {b'PYBv1.1' : 'PYBV11', b'PYBv1.0' : 'PYBV10', b'PYBLITEv1.0' : 'PYBLITEV10',
    b'PYBD-SF2W' : 'PYBD_SF2', b'PYBD-SF3W' : 'PYBD_SF3', b'PYBD-SF6W' : 'PYBD_SF6',
    b'PYBD_SF2W' : 'PYBD_SF2', b'PYBD_SF3W' : 'PYBD_SF3', b'PYBD_SF6W' : 'PYBD_SF6',}

def main():
    if len(sys.argv) < 2:
        print(errmsg, file=sys.stderr)
        sys.exit(1)
    device = sys.argv[1]
    if not os.path.exists(device):
        print('Device {} does not exist'.format(device), file=sys.stderr)
        sys.exit(1)
    pybd = pyboard.Pyboard(device)
    pybd.enter_raw_repl()
    hardware = pybd.exec('import os; print(os.uname()[4].split(' ')[0])').strip()
    pybd.exit_raw_repl()
    if hardware in d:
        print(d[hardware])

if __name__ == "__main__":
    main()

