#!/bin/bash

# Exit on any command failure
set -e

# Set variables
REPO_URL="https://harishgodishalantt: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@github.com/harishgodishalantt/sri9-ai.git/"
REPO_DIR=$(pwd)
BACKEND_DIR="$REPO_DIR/sri9-ai/sri9-ai/sri9-backend"
FRONTEND_DIR="$REPO_DIR/sri9-ai/sri9-ai/sri9-storefront"
B2B_FRONTEND_DIR="$REPO_DIR/sri9-ai/sri9-ai/sri9-b2b-storefront"
TESS_URL="https://github.com/tesseract-ocr/tesseract.git"
TESS_DATA_URL="https://github.com/tesseract-ocr/tessdata.git"
LIBRARIES="$REPO_DIR/libraries"
TESSARACT="$LIBRARIES/tessaract"
TESSARACT_DATA="$LIBRARIES/tessdata"
GOOGLE_APPLICATION_CREDENTIALS="$REPO_DIR/sri9-ai/sri9-ai/dep.json"

#Adding Google application credentials
#export GOOGLE_APPLICATION_CREDENTIALS=$GOOGLE_APPLICATION_CREDENTIALS

# Clean up the existing repository if it exists
rm -rf "$REPO_DIR/sri9-ai"

# Create the parent directory if it doesn't exist
mkdir -p "$LIBRARIES"

# Clone or update the main repository
if [ ! -d "$REPO_DIR/sri9-ai" ]; then
  echo "Cloning repository..."
  git clone "$REPO_URL" "$REPO_DIR/sri9-ai" || { echo "Failed to clone repository."; exit 1; }
else
  echo "Updating repository..."
  cd "$REPO_DIR/sri9-ai" || exit
  git pull || { echo "Failed to update repository."; exit 1; }
  cd ..
fi

# Clone or update Tesseract repository
if [ ! -d "$TESSARACT" ]; then
  echo "Cloning Tesseract repository..."
  git clone "$TESS_URL" "$TESSARACT" || { echo "Failed to clone Tesseract."; exit 1; }
fi

# Clone or update Tessdata repository
if [ ! -d "$TESSARACT_DATA" ]; then
  echo "Cloning Tessdata repository..."
  git clone "$TESS_DATA_URL" "$TESSARACT_DATA" || { echo "Failed to clone Tessdata."; exit 1; }
fi

export TESSDATA_PREFIX=$TESSARACT_DATA

# Copy configuration files
cp -f "$REPO_DIR/sri9-ai/sri9-ai/deploy/dev/storefront/application.properties" "$BACKEND_DIR/storefront/src/main/resources" || { echo "Failed to copy application.properties for storefront."; exit 1; }
cp -f "$REPO_DIR/sri9-ai/sri9-ai/deploy/dev/admin/application.properties" "$BACKEND_DIR/admin/src/main/resources" || { echo "Failed to copy application.properties for admin."; exit 1; }
cp -f "$REPO_DIR/sri9-ai/sri9-ai/deploy/dev/storefront/.env" "$FRONTEND_DIR" || { echo "Failed to copy .env file."; exit 1; }
cp -f "$REPO_DIR/sri9-ai/sri9-ai/deploy/dev/sri9-b2b-storefront/.env" "$B2B_FRONTEND_DIR" || { echo "Failed to copy .env file."; exit 1; }
cp -f "$REPO_DIR/config/sri9.p12" "$BACKEND_DIR/storefront/src/main/resources" || { echo "Failed to copy sri9.p12."; exit 1; }
cp -f "$REPO_DIR/config/sri9.p12" "$BACKEND_DIR/admin/src/main/resources" || { echo "Failed to copy sri9.p12."; exit 1; }

# Shutdown already running servers on ports 3001 and 8444
echo "Shutting down processes running on ports 3002 and 8444 8445..."
lsof -ti tcp:3000 | xargs -r kill -9 || echo "No process running on port 3000."
lsof -ti tcp:3001 | xargs -r kill -9 || echo "No process running on port 3001."
lsof -ti tcp:8444 | xargs -r kill -9 || echo "No process running on port 8444."
lsof -ti tcp:8445 | xargs -r kill -9 || echo "No process running on port 8445."

# Run the Spring Boot application in the backend
echo "Installing and starting backend applications (storefront and admin)..."
cd "$BACKEND_DIR" || exit
mvn clean install || { echo "Failed to install backend applications."; exit 1; }

# Starting storefront application
nohup mvn spring-boot:run -pl storefront > storefront.log 2>&1 &

# Starting admin application
nohup mvn spring-boot:run -pl admin > admin.log 2>&1 &


# Run the npm application in the frontend
echo "Starting npm application in the frontend..."
cd "$FRONTEND_DIR" || exit
npm install bootstrap antd || { echo "Failed to install npm dependencies."; exit 1; }

echo "Installing dependencies and building the application..."
nohup npm start > app.log 2>&1 &



echo "Applications started successfully."
