#!/bin/bash



# Exit on any command failure

set -e



# Set variables

REPO_URL="https://Niraimathy12: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@github.com/Niraimathy12/Translator-ai.git"

REPO_DIR=$(pwd)

CLONED_DIR="$REPO_DIR/translate-ai"

PROJECT_ROOT_DIR="$CLONED_DIR"

#Adding Google application credentials

export GOOGLE_APPLICATION_CREDENTIALS=$GOOGLE_APPLICATION_CREDENTIALS



# Clean up the existing repository if it exists

rm -rf "$CLONED_DIR"



# Create the parent directory if it doesn't exist

mkdir -p "$CLONED_DIR"





# Clone or update the main repository

if [ ! -d "$REPO_DIR/sri9-ai" ]; then

  echo "Cloning repository..."

  git clone "$REPO_URL" "$CLONED_DIR" || { echo "Failed to clone repository."; exit 1; }

else

  echo "Updating repository..."

  cd "$CLONED_DIR" || exit

  git pull || { echo "Failed to update repository."; exit 1; }

  cd ..

fi





# Shutdown already running servers on ports 3001 and 8444

echo "Shutting down processes running on ports 3002 and 8444 8445..."

lsof -ti tcp:4200 | xargs -r kill -9 || echo "No process running on port 4200."







# Run the npm application in the sri 9 b2b frontend

echo "Starting npm application in the translate-ai Storefront..."

cd "$PROJECT_ROOT_DIR" || exit
npm install @angular/animations@20.0.5  || { echo "Failed to install npm dependencies."; exit 1; }

npm install  || { echo "Failed to install npm dependencies."; exit 1; }



echo "Installing dependencies and building the application..."

nohup npm run startHttps > app2.log 2>&1 &



echo "Applications started successfully."
