#!/usr/bin/env bash
# Install Rails

trap 'ret=$?; test $ret -ne 0 && printf "failed\n\n" >&2; exit $ret' EXIT
set -e

create_zshrc_and_set_it_as_shell_file() {
  if [ ! -f "$HOME/.zshrc" ]; then
    touch "$HOME/.zshrc"
  fi
  shell_file="$HOME/.zshrc"
  shell_profile="$HOME/.zprofile"
}

create_bashrc_and_set_it_as_shell_file() {
  if [ ! -f "$HOME/.bashrc" ]; then
    touch "$HOME/.bashrc"
  fi
  shell_file="$HOME/.bashrc"
  shell_profile="$HOME/.bash_profile"
}

case "$SHELL" in
  */zsh) :
    create_zshrc_and_set_it_as_shell_file
  ;;
  *)
    create_bashrc_and_set_it_as_shell_file
  ;;
esac

log_info() {
  printf "\n\e[0;35m $1\e[0m\n\n"
}

if ! command -v brew &>/dev/null; then
  log_info "Installing Homebrew, a good OS X package manager ..."
  /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  eval "$(/opt/homebrew/bin/brew shellenv)"
else
  log_info "Homebrew already installed. Skipping ..."
fi

log_info "Updating Homebrew formulas ..."
  brew update

log_info "Installing Homebrew Services"
  brew tap homebrew/services

log_info "Installing Postgres, a good open source relational database ..."
  brew install postgresql@13 libpq

log_info "Starting Postgres ..."
  brew services start postgresql@13

log_info "Adding Postgres to PATH ..."
echo 'export PATH="/opt/homebrew/opt/postgresql@13/bin:$PATH"' >> $shell_file
echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> $shell_file
export PATH="/opt/homebrew/opt/postgresql@13/bin:$PATH"
export PATH="/opt/homebrew/opt/libpq/bin:$PATH"

log_info "Installing Redis, a good key-value database ..."
  brew install redis

log_info "Starting Redis ..."
  brew services start redis

log_info "Installing and linking ImageMagick, to crop and resize images ..."
  brew install imagemagick

log_info "Installing rbenv, to change Ruby versions ..."
  brew install rbenv

  if ! command -v rbenv &>/dev/null; then
    rbenv init
    eval "$(rbenv init -)"
  fi

log_info "Installing ruby-build, to install Rubies ..."
  brew install ruby-build

log_info "Upgrading OpenSSL ..."
  brew install openssl

log_info "Installing image libs ..."
  brew install advancecomp jhead jpegoptim jpeg optipng oxipng pngcrush pngquant

log_info "Installing coreutils ..."
  brew install coreutils

ruby_version="3.2.1"

log_info "Installing Ruby $ruby_version ..."
  rbenv install "$ruby_version"

log_info "Setting $ruby_version as global default Ruby ..."
  rbenv global "$ruby_version"

log_info "Updating to latest Rubygems version ..."
  gem update --system

log_info "Installing Bundler to install project-specific Ruby gems ..."
  gem install bundler

log_info "Configuring Bundler for faster, parallel gem installation ..."
  number_of_cores=$(sysctl -n hw.ncpu)
  bundle config --global jobs $((number_of_cores - 1))

log_info "Installing Rails ..."
  gem install rails

log_info "Installing MailHog ..."
  brew install mailhog

log_info "Installing Node ..."
  brew install node

log_info "Installing pnpm ..."
  npm install -g pnpm