@echo off
chcp 65001
setlocal enabledelayedexpansion

echo ➡️ Delete old DIST folder
rmdir /s /q "dist"
echo ✅ Old DIST deleted

echo ➡️ Build Backend
cd backend
call composer run build
cd ..
echo ✅ Backend built

echo ➡️ Build frontend
cd frontend
call npm run build
cd ..
echo ✅ Frontend built

echo ➡️ Make new DIST folder structure
mkdir "dist"
mkdir "dist\data"
mkdir "dist\data\attachments"
mkdir "dist\app"
echo ✅ New DIST structur created

echo ➡️ Copy APP files
robocopy backend dist\app /S /XD tests scripts /XF composer.*
robocopy frontend\dist dist\app\public /S
echo ✅ APP files copied

echo ➡️ Packaging build
for /f "tokens=2 delims=:" %%a in ('findstr /c:"\"version\"" frontend\package.json') do (
	set ver=%%a
	set ver=!ver: =!
	set ver=!ver:"=!
	set ver=!ver:,=!
)
echo 👉 Version: %ver%
cd dist
tar -a -c -f ..\build\BugReport.%ver%.zip *
cd ..
echo ✅ Build packaged

echo 🚀 Done.
