#!/bin/bash
### --- RUN SYSTEM DISPLAY MANAGER --- ###
i=0
cd "$(dirname "$0")" && source ./dsenv/bin/activate
#
echo "DISP:     Running Display Manager - 1 of 3"
python3 registrationManager.py || { echo "INFO:     Register    - Unregistered" ; exit 1; }
python3 statusManager.py &
if [ -f ./display.zip ]; then
    python3 fileManager.py --strict --contentonly
else
    python3 fileManager.py --strict --remote
fi
python3 -m uvicorn systemManager:app --host 0.0.0.0 --workers 1 1>/dev/null 2>&1 &
unclutter -idle 2 &
#
#
echo "DISP:     Verifying Display Manager Status - 2 of 3"
until curl -s -f -o /dev/null "http://localhost:8000/health"
do
    if [[ "$i" -eq 20 ]]; 
    then
        echo "DISP:     Fatal Error in Initilizing Data Manager ... " && killall python3 && exit 0
    elif [[ "$i" -lt 20 ]]; 
    then
        let "++i"
    fi
    sleep 3
done
#
### --- RUN SYSTEM DATA MANAGER --- ###
echo "DISP:     Running Data Manager - 3 of 3"
#python3 displayManager.py &
firefox --kiosk "http://localhost:8000/apps/index.html" 1>/dev/null 2>&1 &
python3 dataManager.py 1>/dev/null 2>&1