#!/bin/bash

OPTS="--no-first-run --disable-infobars --noerrdialogs"

# No GPU on Pi 3
cat /proc/cpuinfo | grep "Pi 3 Model" >/dev/null
if [ $? -eq 0 ]; then
  OPTS=$OPTS --disable-3d-apis
fi

while true; do
    tvservice -s 2>&1 | grep "state 0x40001" >/dev/null
    if [ $? -ne 0 ]; then
        # Clear browser errors
        PREFS='/home/pi/.config/chromium/Default/Preferences'
        sed -i 's/"exited_cleanly":false/"exited_cleanly":true/' $PREFS
        sed -i 's/"exit_type":"Crashed"/"exit_type":"Normal"/' $PREFS

        # Start browser
        /usr/local/bin/browser $OPTS http://localhost/
    fi

    sleep 1
done
