import sys
import json
import subprocess
import tempfile
import webbrowser
import time

def shell(cmdline, cwd='.'):
    print(f'$ {' '.join(cmdline)}')
    return subprocess.check_output(cmdline, cwd=cwd).decode()

feed = sys.argv[1]
adb = 'adb'

print(shell([adb,'shell','nohup /data/initialize.sh']))

board_config = shell([adb, 'shell', 'cat /usr/bin/boardconfig'])
board_config_dict = dict( c.strip().lower().split('=') for c in board_config.strip().split('\n') )
board_type=board_config_dict['board_type']
print(f'board type: {board_type}')

print(shell([adb, 'push', f'./build-{board_type}/libopencv_imgcodecs.so.4.2.0', '/data/lib/libopencv_imgcodecs.so.4.2.0']))
print(shell([adb, 'shell', 'chmod +x /data/lib/libopencv_imgcodecs.so.4.2.0']))
print(shell([adb, 'shell', 'ln -s /data/lib/libopencv_imgcodecs.so.4.2.0 /data/lib/libopencv_imgcodecs.so.4.2']))
print(shell([adb, 'shell', 'ln -s /data/lib/libopencv_imgcodecs.so.4.2 /data/lib/libopencv_imgcodecs.so']))

print(shell([adb, 'push', f'./build-{board_type}/libopencv_calib3d.so.4.2.0', '/data/lib/libopencv_calib3d.so.4.2.0']))
print(shell([adb, 'shell', 'chmod +x /data/lib/libopencv_calib3d.so.4.2.0']))
print(shell([adb, 'shell', 'ln -s /data/lib/libopencv_calib3d.so.4.2.0 /data/lib/libopencv_calib3d.so.4.2']))
print(shell([adb, 'shell', 'ln -s /data/lib/libopencv_calib3d.so.4.2 /data/lib/libopencv_calib3d.so']))

print(shell([adb, 'shell', 'mv /data/ava/lib/node_lidar_slam.so /data/ava/lib/node_lidar_slam.so.off']))

print(shell([adb, 'push', f'./build-{board_type}/node_previewer.so', '/data/ava/lib/node_previewer.so']))
print(shell([adb, 'shell', 'chmod +x /data/ava/lib/node_previewer.so']))

print(shell([adb, 'push', f'./preview_on.ogg', '/data/log/preview_on.ogg']))

os_release = shell([adb, 'shell', 'cat /etc/os-release'])
product = json.loads(os_release)['product']

conf_json = shell([adb, 'shell', f'cat /data/conf/{product}.conf'])
conf = json.loads(conf_json)

safe_nodes = []
for node in conf['nodes']:
    if node['ID'] == 'AvaNodeLdsSLAM':
        continue
    if node['ID'] == 'AvaNodePreviewer':
        continue
    safe_nodes.append(node)

conf['nodes'] = safe_nodes

if feed == "rgb":
    conf['nodes'].append({
        "ID": "AvaNodePreviewer",
        "name": "previewer",
        "feed": feed,
        "ruler": True,
    })
elif feed == "stereo":
    conf['nodes'].append({
        "ID": "AvaNodePreviewer",
        "name": "previewer",
        "feed": feed,
        "guide": False,
        "ruler": False,
    })
else:
    conf['nodes'].append({
        "ID": "AvaNodePreviewer",
        "name": "previewer",
        "feed": feed,
        "guide": True,
    })

with tempfile.NamedTemporaryFile(mode='w', delete=False) as temp:
    json.dump(conf, temp, indent=4)

print(shell([adb, 'push', temp.name, f'/data/conf/{product}.conf']))

print(shell([adb, 'shell', 'nohup /data/test/ava.sh']))

print(shell([adb, 'forward', 'tcp:8086', 'tcp:8086']))

serial = shell([adb, 'shell', 'cat /mnt/private/ULI/factory/sn.txt'])
print(serial)

time.sleep(7)

webbrowser.open('http://localhost:8086/preview/')

time.sleep(10)
