#!/usr/bin/env python3

import argparse
import os
import subprocess
import tempfile
import importlib.util

cli = argparse.ArgumentParser(description='deploy a thing')
cli.add_argument('depo', type=str,
                 help='the depo roun to deploy')

args = cli.parse_args()

cwd = os.getcwd()
bild_dir = f"{cwd}/bild"

src = f"{cwd}/depo/{args.depo}"
out = f"{bild_dir}/{args.depo}"


# bild
subprocess.run(["nix-build", "-A", f"depo.{args.depo}",
                "--out-link", out])
print("bilt")

# get roun
p = subprocess.run(["chip/roun", args.depo, "-i"],
                   stdout=subprocess.PIPE)
ip = p.stdout.decode()

# push
subprocess.run(["nix", "copy", "--to", f"ssh://root@{ip}", f"{out}"])
print("sent")

# switch
subprocess.run(["ssh", f"root@{ip}", "sudo",
                f"{os.readlink(out)}/bin/switch-to-configuration",
                "switch"])

print(f"pushed {args.depo} -> {ip}")