# https://dev.to/aschmelyun/using-the-magic-of-mkcert-to-enable-valid-https-on-local-dev-sites-3a3c


import os
import shlex
import socket
import subprocess


OP_DEV_HOME = os.path.expanduser('~/.op-dev')
CERT_PATH = os.path.join(OP_DEV_HOME, 'localhost+4.pem')
KEY_PATH = os.path.join(OP_DEV_HOME, 'localhost+4-key.pem')


def verify():
  p = subprocess.run([ 'ipconfig', 'getifaddr', 'en0' ], capture_output=True, check=True)
  ip_addr = p.stdout.decode('UTF-8').strip()

  local_network_addr = socket.gethostname() + '.local'

  os.makedirs(OP_DEV_HOME, exist_ok=True)
  subprocess.run([ 'mkcert', '-install' ], check=True, cwd=OP_DEV_HOME)
  subprocess.run([ 'mkcert', 'localhost', '127.0.0.1', ip_addr, local_network_addr, '::1' ], check=True, cwd=OP_DEV_HOME)


def run(parser, args):
  verify()
  print('KEY=' + shlex.quote(KEY_PATH) + ' CERT=' + shlex.quote(shlex.quote(CERT_PATH)))
