#!/bin/bash
echo "downloading Auth Server";
git clone https://SadTechInc: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@github.com/SadTechInc/Auth-Server.git
echo "downloading Chez-Moi";
git clone https://SadTechInc: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@github.com/SadTechInc/aloethere.git
echo "opening aloethere";
cd aloethere;
pwd;
echo "installing nodejs packages";
pnpm --force i;
echo "building next";
pnpm next build

# Restore assets into the public folder if backup exists
if [ -d "/var/www/aloethereinc.com/assets" ]; then
  echo "Restoring assets to public folder...";
  sudo mv /var/www/aloethereinc.com/assets /var/www/aloethereinc.com/aloethere/public/;
else
  echo "No assets backup found. Skipping restore.";
fi

echo "flushing pm2 logs";
sudo pm2 flush;

echo "launching aloethere with pm2";
sudo pm2 start pnpm --name "AloeThere app" -- start;
echo "changing directories";
cd ../Auth-Server;
pwd;
echo "installing nodejs packages";
pnpm --force i;
echo "launching Auth Server with pm2";
sudo pm2 start server.js --name "Auth Server" --node-args="--env-file=.env.production"
echo "returning to home dir";
cd ..;
pwd;
echo "opening pm2 log";
sudo pm2 logs;

