import sys
import ctypes
import os
import pathlib
import subprocess
from platform import python_version

# arg0 -- script path
# arg1 -- dll path
# arg2 -- shared memory name
# arg3 -- shared memory size
# arg4 -- python version

def validate_python_version(expectedVersion):
    pythonVersionTokenized = python_version().split('.')
    expectedpythonVersionTokenized = expectedVersion.split('.')
    if pythonVersionTokenized[0] != expectedpythonVersionTokenized[0] or pythonVersionTokenized[1] != expectedpythonVersionTokenized[1]:
        raise Exception("Version of python doesn't match the expected version") 

def start_python_host():
    validate_python_version(sys.argv[4])
    
    ni_python_interface_dll = ctypes.CDLL(sys.argv[1])

    n = 3
    byte_ptr_type = ctypes.POINTER(ctypes.c_byte)
    byte_array_type = byte_ptr_type * n

    # arg1 -- dll path
    # arg2 -- shared memory name
    # arg3 -- shared memory size
    byte_array = [
        bytes(sys.argv[1], 'utf-8'),
        bytes(sys.argv[2], 'utf-8'),
        bytes(sys.argv[3], 'utf-8'),
        ]
    arg_array = byte_array_type(
        ctypes.cast(byte_array[0], byte_ptr_type),
        ctypes.cast(byte_array[1], byte_ptr_type),
        ctypes.cast(byte_array[2], byte_ptr_type),
        )
    ret_val = ni_python_interface_dll.StartPythonHost(ctypes.c_int(n), arg_array)


start_python_host()