# verify if port 3000 is busy
# if not, run thumbnail.ts
# if yes, send error

import socket
import subprocess
import os

def is_port_busy(port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    result = sock.connect_ex(('localhost', port))
    sock.close()
    return result == 0

if is_port_busy(3000):
    print("Port 3000 is busy")
else:
    path = os.path.dirname(os.path.abspath(__file__))
    # Execute bun index.ts in another shell in background
    subprocess.Popen(f"bun {path}/index.ts", shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
    print("Thumbgen running in background")


