#!/usr/bin/python3
# Copyright (C) 2022 The FUSS Project <info@fuss.bz.it>
# Author: Marco Marinello <contact-nohuman@marinello.bz.it>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

import requests
import subprocess
import socket

# Clean veyon db
subprocess.call(["/usr/bin/veyon-cli", "networkobjects", "clear"])

# Need to bypass proxy configuration
session = requests.Session()
session.trust_env = False

# Try to get list from server
server = session.get("http://proxy:13402/veyon/client_conf", params={"hostname": socket.gethostname()})
if server.status_code != 200:
    exit(0)

masterhost = socket.gethostname()

for cluster in server.json():
    subprocess.call(["/usr/bin/veyon-cli", "networkobjects", "add", "location", cluster["name"]])
    for pc in cluster["computers"]:
        if pc != masterhost:          
            subprocess.call(["/usr/bin/veyon-cli", "networkobjects", "add", "computer", pc, "", pc, cluster["name"]])
