@echo off

if exist "./ready.flag" (
    del "./ready.flag"
)

start /min "" powershell -WindowStyle Hidden -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command ^
"Add-Type -AssemblyName System.Windows.Forms; ^
Add-Type -AssemblyName System.Drawing; ^
$form = New-Object Windows.Forms.Form; ^
$form.FormBorderStyle = 'None'; ^
$form.StartPosition = 'CenterScreen'; ^
$image = [System.Drawing.Image]::FromFile('../Assets/MudaeAvatar.ico'); ^
$form.ClientSize = $image.Size; ^
$picture = New-Object Windows.Forms.PictureBox; ^
$picture.Image = $image; ^
$picture.Dock = 'Fill'; ^
$form.Controls.Add($picture); ^
$form.TopMost = $true; ^
$form.Show(); ^
$flag = './ready.flag'; ^
while ((-not (Test-Path $flag))) { Start-Sleep -Milliseconds 200 }"

if exist "Dashboard.py" (
    del "Dashboard.py"
)

if exist "requirements.txt" (
    del "requirements.txt"
)

if exist ".env" (
    del ".env"
)

set SCRIPT_URL=https://files.yoki.cx/apps/DashBoard/.Code/requirements.txt
set OUTPUT_FILE=requirements.txt

powershell -Command "Invoke-WebRequest -Uri '%SCRIPT_URL%' -OutFile '%OUTPUT_FILE%' -UseBasicParsing"

set SCRIPT_URL=https://files.yoki.cx/apps/DashBoard/.Code/Dashboard.py
set OUTPUT_FILE=Dashboard.py

powershell -Command "Invoke-WebRequest -Uri '%SCRIPT_URL%' -OutFile '%OUTPUT_FILE%' -UseBasicParsing"

set SCRIPT_URL=https://files.yoki.cx/apps/DashBoard/.Code/.env
set OUTPUT_FILE=.env

powershell -Command "Invoke-WebRequest -Uri '%SCRIPT_URL%' -OutFile '%OUTPUT_FILE%' -UseBasicParsing"

for /f "tokens=1,2 delims==" %%a in (.env) do (
    set %%a=%%b
)
if not exist "%cd%/venv" (
    call "%cd%/Python/python" -m venv "%cd%/venv"
    echo created a new venv
) else (
    echo venv already exists
)
call "%cd%/venv/Scripts/activate"
"%cd%/Python/python" -m pip install --upgrade pip
"%cd%/Python/python" Dashboard.py

if exist "./ready.flag" (
    del "./ready.flag"
)