printf  "\nsetting up NGINX\n"

rm /etc/nginx/sites-available/default
echo "
map \$http_upgrade \$connection_upgrade {
  default upgrade;
  '' close;
}

upstream grafana {
server localhost:3000;
}

server {
    listen 1234;
    server_name  localhost;
    location /grafana {
        proxy_pass         http://localhost:3000;
        rewrite  ^/grafana/(.*)  /\$1 break;
        proxy_set_header   Host \$host;
    }
    location /grafana/api/live/ {
        proxy_http_version 1.1;
        proxy_set_header Upgrade \$http_upgrade;
        proxy_set_header Connection \$connection_upgrade;
        proxy_set_header Host \$http_host;
        proxy_pass http://grafana;
        rewrite  ^/grafana/(.*)  /\$1 break;
    }

    location /novnc/ {
      proxy_pass http://127.0.0.1:6080/;
    }
    
    location /novnc/websockify {
      proxy_pass http://127.0.0.1:6080/;
      proxy_http_version 1.1;
      proxy_set_header Upgrade \$http_upgrade;
      proxy_set_header Connection "Upgrade";
      proxy_set_header Host \$host;
    }

    location / {
        proxy_set_header   X-Forwarded-For \$remote_addr;
        proxy_set_header   Host \$http_host;
        proxy_pass         "http://127.0.0.1:6666";
    }
    
}
" >> /etc/nginx/sites-available/default
printf  "\nNGINX configuration successfull\n"

printf  "\ncreating sqlite databases\n"
sqlite3 /app/MsRewards-Reborn/MsRewards.db "CREATE TABLE daily (id INTEGER PRIMARY KEY,compte TEXT,points int,date TEXT);"
sqlite3 /app/MsRewards-Reborn/MsRewards.db "CREATE TABLE comptes (id INTEGER PRIMARY KEY,compte TEXT,last_pts int, banned int);"

printf  "\nconfigurating grafana\n"

cp /app/MsRewards-Reborn/config/grafana.ini /etc/grafana/
grafana-cli plugins install frser-sqlite-datasource

printf  "setting up default dashboard"
cp /app/MsRewards-Reborn/config/Stats-dashbord.json /usr/share/grafana/public/dashboards/home.json