#!/bin/bash

# Font decoration for better a e s t h e t i c                                                                                                                                                                                    
RED="\e[0;31m"                                                                                                                                                                                                                    
GREEN="\e[0;32m"                                                                                                                                                                                                                  
BLUE="\e[0;34m"                                                                                                                                                                                                                   
BOLD="\e[1m"                                                                                                                                                                                                                      
ULINE="\e[4m"                                                                                                                                                                                                                     
RESET="\e[0m"                                                                                                                                                                                                                     

# Make sure that ctrl+C actually exits the script                                                                                                                                                                                 
trap "exit" INT  

# Give informative error messages
trap 'echo "Oops! Something went wrong on line $LINENO of this script. Exit code was $?" >&2' ERR

# --------------- Functions ---------------
# Coding Moment: generally, whenever you see something with brackets at the end of it, like this()                                                                                                                                
# or like(this), it's a function! It takes inputs and gives (or does) something as an output                                                                                                                                      

install_if_needed() {                                                                                                                                                                                                             
    for package in "$@"                                                                                                                                                                                                           
    do                                                                                                                                                                                                                            
        # TODO Better installation detection than "which"
        if [ -z $(which $package 2>/dev/null) ]; then                                                                                                                                                                             
            echo "installing" $package                                                                                                                                                                                            

            case $DISTRO in                                                                                                                                                                                                       
                "debian")                                                                                                                                                                                                         
                    sudo apt install -y $package                                                                                                                                                                                  
                    ;;                                                                                                                                                                                                            
                "arch")                                                                                                                                                                                                           
                    sudo pacman -S $package --noconfirm --needed                                                                                                                                                                  
                    ;;                                                                                                                                                                                                            
                "fedora")                                                                                                                                                                                                         
                    sudo dnf install -y $package                                                                                                                                                                                  
                    ;;                                                                                                                                                                                                            
                "mac")                                                                                                                                                                                                            
                    brew install $package                                                                                                                                                                                         
                    ;;                                                                                                                                                                                                            
            esac                                                                                                                                                                                                                  
        else                                                                                                                                                                                                                      
            echo $package 'already installed!'                                                                                                                                                                                    
        fi                                                                                                                                                                                                                        
    done                                                                                                                                                                                                                          
}     

# --------------- Environment Setup ---------------
if [ -f .env ]; then
    export $(grep -v '^#' .env | xargs)
else
    echo "No .env file, this might cause some issues..."
fi