#!/bin/bash

NOVNC=""

XVNC=Xvnc
XVNC_HOST=localhost
XVNC_PORT=5901

DISPLAY_WIDTH=$1
DISPLAY_HEIGHT=$2

XVNC_DISPLAY=:69

print_usage() {
    echo "Usage: run_vnc.sh <width> <height>"
}

#if command -v novnc_proxy &> /dev/null; then
#    NOVNC=novnc_proxy
#elif command -v novnc &> /dev/null; then
#    NOVNC=novnc
#else
#    echo "noVNC (novnc_proxy || novnc) not found! Aborting..."
#    exit 1
#fi

if ! command -v Xvnc &> /dev/null; then
    echo "Xvnc not found! Aborting..."
    exit 1
fi

if [[ "$DISPLAY_WIDTH" = "" || "$DISPLAY_HEIGHT" = "" ]] ; then
    print_usage
    exit 1
fi

$XVNC ${DISPLAY} \
    -geometry ${DISPLAY_WIDTH}x${DISPLAY_HEIGHT} \
    -depth 24 \
    -SecurityTypes None \
    -interface $XVNC_HOST \
    -rfbport $XVNC_PORT &

sleep 2

websockify --web /usr/share/novnc 0.0.0.0:7080 localhost:${XVNC_PORT} &

sleep 2

export DISPLAY=${XVNC_DISPLAY}
exec ./out/miniRT
