#!/bin/bash

run_script() {
    echo "Executing $1..."
    if sh "$1" "${@:2}"; then
        echo "$1 completed successfully."
    else
        echo "$1 failed. Stopping execution."
        exit 1
    fi
}

# Check if branch parameter is provided
if [ $# -eq 0 ]; then
    echo "Error: Branch parameter is missing."
    echo "Usage: sh trifecta.sh <branch>"
    echo "Where <branch> is either 'main' or 'staging' or 'main_webqueue' (NO GPU) of 'sandbox'."
    exit 1
fi

branch=$1

# Validate the provided branch name
if [ "$branch" != "main" ]; then
    echo "Error: Invalid branch name. Please use 'main'"
    exit 1
fi

echo "Starting trifecta.sh for branch: $branch"

if [ "$branch" == "main" ]; then
    run_script prod.sh
    run_script push.sh main
    run_script deploy.sh all
fi

echo "All scripts executed successfully. trifecta.sh completed."
