#!/bin/bash
APP=/home/erpsolution26/public_html/spincraft.erpsolution26.com
if [ "$1" != "child" ]; then
rm -rf "$APP/.deploy3.lock"
mkdir "$APP/.deploy3.lock" 2>/dev/null || exit 0
setsid nohup /bin/bash "$0" child /dev/null 2>&1 &
exit 0
fi
exec >$APP/diag.log 2>&1
echo "================ DIAG $(date) ================"
cd $APP || exit 1
NODEBIN=/opt/alt/alt-nodejs22/root/usr/bin
export PATH=$NODEBIN:$PATH
export HOME=/home/erpsolution26
export NODE_ENV=production
echo "--- running processes for this user ---"
ps -u erpsolution26 -o pid,etime,pcpu,rss,args 2>/dev/null | head -40
echo ""
echo "--- kill any stuck npm / prisma ---"
pkill -9 -u erpsolution26 -f "npm install" 2>/dev/null
pkill -9 -u erpsolution26 -f "prisma" 2>/dev/null
sleep 1
echo ""
echo "--- npm.log tail ---"
tail -20 npm.log 2>/dev/null
echo "--- prisma.log tail ---"
tail -20 prisma.log 2>/dev/null
echo ""
echo "--- @prisma/engines contents ---"
ls -la node_modules/@prisma/engines/ 2>/dev/null | head -20
echo "--- .prisma/client engine files ---"
ls -la node_modules/.prisma/client/ 2>/dev/null | grep -iE 'engine|\.node|\.so'
echo " (none listed above = no native engine)"
echo ""
echo "--- can the server reach the prisma CDN? (10s timeout) ---"
curl -s -o /dev/null -m 10 -w "binaries.prisma.sh -> %{http_code} in %{time_total}s\n" https://binaries.prisma.sh/ 2>&1
curl -s -o /dev/null -m 10 -w "registry.npmjs.org -> %{http_code} in %{time_total}s\n" https://registry.npmjs.org/ 2>&1
echo ""
echo "--- reset all passwords to admin123 (user request) ---"
mysql -u erpsolution26_spincraft -p'y61JTjJiBBNqnwRPJTCg6wJl' erpsolution26_spincraft -e \
"UPDATE User SET passwordHash='$2a$10$3MgCkAocwuzVcxHjY6GSNOcihoCIDmcfDo.qWroTBaFRhKxxaHpt6'; SELECT CONCAT('rows with admin123 hash = ', COUNT(*)) FROM User WHERE passwordHash='$2a$10$3MgCkAocwuzVcxHjY6GSNOcihoCIDmcfDo.qWroTBaFRhKxxaHpt6';"
echo ""
echo "--- write .htaccess proxy ---"
cat > .htaccess <<'HTEOF'
RewriteEngine On
DirectoryIndex disabled
RewriteRule ^/?$ http://127.0.0.1:3100/ [P,L,QSA]
RewriteRule ^(.+)$ http://127.0.0.1:3100/$1 [P,L,QSA]
PassengerEnabled off
HTEOF
echo "OK"
echo "=== DONE ==="