#!/bin/bash

# Check for the existence of environment files
env_file=".env"
prod_file=".env.production"
test_file=".env.testing"

env_exists=false
prod_exists=false
test_exists=false

if [ -f "$env_file" ]; then
    env_exists=true
fi

if [ -f "$prod_file" ]; then
    prod_exists=true
fi

if [ -f "$test_file" ]; then
    test_exists=true
fi

# Switch environments based on the current state
if $prod_exists && $env_exists && ! $test_exists; then
    echo "Currently in testing environment. Switching to production..."
    mv "$env_file" "$test_file" && mv "$prod_file" "$env_file" && echo "Switched to production environment."
elif $test_exists && $env_exists && ! $prod_exists; then
    echo "Currently in production environment. Switching to testing..."
    mv "$env_file" "$prod_file" && mv "$test_file" "$env_file" && echo "Switched to testing environment."
else
    echo "Environment files are missing or not properly configured."
    echo "Ensure both .env and either .env.production or .env.testing files are present."
    exit 1
fi
