


# Check if Miniconda is already installed
conda --version &> /dev/null
if [[ $? -ne 0 ]]; then
    echo "Miniconda is not installed. Proceeding with installation..."
else
    echo "Miniconda is already installed. Please uninstall it before proceeding."
    exit 0
fi


# Detect the machine type if MACHINE is not set
if [[ -z "$MACHINE" ]]; then
    uname_output=$(uname -m)
    case "$uname_output" in
        arm64)
            if [[ "$(uname)" == "Darwin" ]]; then
                MACHINE="apple_silicon"
            else
                MACHINE="linux_arm"
            fi
            ;;
        x86_64)
            if [[ "$(uname)" == "Darwin" ]]; then
                MACHINE="intel_mac"
            else
                MACHINE="linux_x86"
            fi
            ;;
        *)
            echo "Unsupported architecture: $uname_output"
            exit 1
            ;;
    esac
fi

# Proceed with installation
if [[ "$MACHINE" == "apple_silicon" ]]; then
    echo "Autoinstalling Miniconda for Apple Silicon..."
    wget -q https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh -O Miniconda3-MacOSX-arm64.sh
    chmod +x Miniconda3-MacOSX-arm64.sh
    ./Miniconda3-MacOSX-arm64.sh -b -p ~/miniconda3
    rm Miniconda3-MacOSX-arm64.sh
elif [[ "$MACHINE" == "intel_mac" ]]; then
    echo "Autoinstalling Miniconda for Intel Mac..."
    wget -q https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh -O Miniconda3-MacOSX-x86_64.sh
    chmod +x Miniconda3-MacOSX-x86_64.sh
    ./Miniconda3-MacOSX-x86_64.sh -b -p ~/miniconda3
    rm Miniconda3-MacOSX-x86_64.sh
elif [[ "$MACHINE" == "linux_x86" ]]; then
    echo "Autoinstalling Miniconda for Linux x86..."
    wget -q https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O Miniconda3-Linux-x86_64.sh
    chmod +x Miniconda3-Linux-x86_64.sh
    ./Miniconda3-Linux-x86_64.sh -b -p ~/miniconda3
    rm Miniconda3-Linux-x86_64.sh
elif [[ "$MACHINE" == "linux_arm" ]]; then
    echo "Autoinstalling Miniconda for Linux ARM..."
    wget -q https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-aarch64.sh -O Miniconda3-Linux-aarch64.sh
    chmod +x Miniconda3-Linux-aarch64.sh
    ./Miniconda3-Linux-aarch64.sh -b -p ~/miniconda3
    rm Miniconda3-Linux-aarch64.sh
else
    echo "Unsupported machine type: $MACHINE"
    echo "Please visit https://docs.conda.io/en/latest/miniconda.html for manual installation instructions."
    exit 2
fi

# Initialize Conda
if [[ -x ~/miniconda3/bin/conda ]]; then
    echo "Initializing Conda..."
    ~/miniconda3/bin/conda init bash
    ~/miniconda3/bin/conda init zsh
    ~/miniconda3/bin/conda config --set accept_channel_terms true

    # Define file paths
    BASHRC="$HOME/.bashrc"
    BASH_PROFILE="$HOME/.bash_profile"

    touch $BASHRC $BASH_PROFILE
    
    # Function to extract and copy the Conda block
    copy_conda_block() {
        local source_file=$1
        local target_file=$2

        # Extract the Conda block from the source file
        CONDA_BLOCK=$(sed -n '/# >>> conda initialize >>>/,/# <<< conda initialize <<</p' "$source_file")

        # Append the Conda block to the target file
        echo "Adding Conda initialization block to $target_file."
        echo ""
        echo "$source_file // $target_file // $CONDA_BLOCK"
        echo "" >> $target_file
        echo  "$CONDA_BLOCK" >> "$target_file"
        echo "Conda initialization block added to $target_file."
    }

    # Check for Conda block in both files
    BASHRC_HAS_BLOCK=false
    BASH_PROFILE_HAS_BLOCK=false

    if grep -q "# >>> conda initialize >>>" "$BASHRC"; then
        BASHRC_HAS_BLOCK=true
    fi

    if grep -q "# >>> conda initialize >>>" "$BASH_PROFILE"; then
        BASH_PROFILE_HAS_BLOCK=true
    fi

    # Handle the cases
    if $BASHRC_HAS_BLOCK && ! $BASH_PROFILE_HAS_BLOCK; then
        echo "Conda initialization block found in .bashrc but missing in .bash_profile."
        copy_conda_block "$BASHRC" "$BASH_PROFILE"
    elif $BASH_PROFILE_HAS_BLOCK && ! $BASHRC_HAS_BLOCK; then
        echo "Conda initialization block found in .bash_profile but missing in .bashrc."
        copy_conda_block "$BASH_PROFILE" "$BASHRC"
    elif $BASHRC_HAS_BLOCK && $BASH_PROFILE_HAS_BLOCK; then
        echo "Conda initialization block already exists in both .bashrc and .bash_profile. No changes made."
    else
        echo "No Conda initialization block found in either .bashrc or .bash_profile."
        echo "From a bash shell, please run 'conda init bash' to initialize Conda."
        exit 1
    fi

    echo "Miniconda installation successful."
    echo "Open a new shell, and conda should be available (try: 'conda -v')."
else
    echo "Miniconda installation failed. Please try again and/or atttempt the commands in this script manually."
    exit 3
fi
