#!/bin/bash

set -e

# Colors for output
GREEN='\033[0;32m'
NC='\033[0m' # No Color

echo -e "${GREEN}Updating system and installing required packages...${NC}"
sudo apt update
sudo apt install -y build-essential procps curl file git

echo -e "${GREEN}Installing Homebrew...${NC}"
NONINTERACTIVE=1 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Determine shell profile
if [[ -n "$ZSH_VERSION" ]]; then
    BREW_PROFILE="$HOME/.zprofile"
elif [[ -n "$BASH_VERSION" ]]; then
    BREW_PROFILE="$HOME/.bash_profile"
else
    BREW_PROFILE="$HOME/.profile"
fi

echo -e "${GREEN}Adding Homebrew to your shell profile: $BREW_PROFILE${NC}"

# Apply Homebrew environment for current session
test -d ~/.linuxbrew && eval "$(~/.linuxbrew/bin/brew shellenv)"
test -d /home/linuxbrew/.linuxbrew && eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"

# Add to shell profile for future sessions
echo "eval \"\$($(brew --prefix)/bin/brew shellenv)\"" >> "$BREW_PROFILE"

echo -e "${GREEN}Verifying brew installation...${NC}"
brew doctor || echo -e "${GREEN}Homebrew installed, but doctor reported issues. Review the output above.${NC}"

echo -e "${GREEN}Installation complete! Run 'brew help' to get started.${NC}"
