#!/usr/bin/env python3
import os
import json
import subprocess
import sys
import urllib.request

from gzuav.GazeboPaths import generate_model_path, generate_plugin_path

server_ip = sys.argv[1]
server_port = int(sys.argv[2])

THISDIR = os.path.abspath(os.path.dirname(__file__))

info_dict = json.loads(urllib.request.urlopen('http://{}:{}/info'.format(server_ip, server_port)).read().decode())

gzenv = dict(os.environ)
gzenv['GAZEBO_MASTER_URI'] = 'http://{}:{}'.format(server_ip, info_dict['network_info']['gazebo_port'])
gzenv['GAZEBO_MODEL_PATH'] = generate_model_path(os.path.join(THISDIR, 'gazebo/models'))
gzenv['GAZEBO_PLUGIN_PATH'] = generate_plugin_path(os.path.join(THISDIR, 'gazebo/plugins'))

gzcmd = \
[
    'gzclient',
    '--verbose'
]

subprocess.run(gzcmd, env=gzenv)
