#!/usr/bin/env bash set -euo pipefail ##? Setups the environment ##? ##? Usage: ##? installer DOTLY_REPOSITORY=${DOTLY_REPOSITORY:-CodelyTV/dotly} DOTLY_BRANCH=${DOTLY_BRANCH:-main} DOTLY_LOG_FILE=${DOTLY_LOG_FILE:-$HOME/dotly.log} export DOTLY_ENV=${DOTLY_ENV:-PROD} export DOTLY_INSTALLER=true red='\033[0;31m' green='\033[0;32m' purple='\033[0;35m' normal='\033[0m' _w() { local -r text="${1-}" echo -e "$text" } _a() { _w " > $1"; } _e() { _a "${red}$1${normal}"; } _s() { _a "${green}$1${normal}"; } _q() { read -rp "🤔 $1: " "$2"; } _log() { log_name="$1" current_date=$(date "+%Y-%m-%d %H:%M:%S") touch "$DOTLY_LOG_FILE" echo "----- $current_date - $log_name -----" >>"$DOTLY_LOG_FILE" while IFS= read -r log_message; do echo "$log_message" >>"$DOTLY_LOG_FILE" done echo "" >>"$DOTLY_LOG_FILE" } current_timestamp() { date +%s; } create_dotfiles_dir() { if [ -d "$1" ]; then local -r backup_path="$1.$(current_timestamp).back" _e "The path '$1' already exist" _s "Creating a backup in '$backup_path'" mv "$1" "$backup_path" else _a "Ok! dotfiles will be located in: ${purple}$DOTFILES_PATH${normal}" fi mkdir -p "$1" } command_exists() { type "$1" >/dev/null 2>&1 } _w " ┌────────────────────────────────────┐" _w "~ │ 🚀 Welcome to the ${green}dotly${normal} installer! │ ~" _w " └────────────────────────────────────┘" _w _q "Where do you want your dotfiles to be located? (default ~/.dotfiles)" "DOTFILES_PATH" DOTFILES_PATH="${DOTFILES_PATH:-$HOME/.dotfiles}" DOTFILES_PATH="$(eval echo "$DOTFILES_PATH")" export DOTFILES_PATH="$DOTFILES_PATH" dotly_inner_path="modules/dotly" export DOTLY_PATH="$DOTFILES_PATH/$dotly_inner_path" create_dotfiles_dir "$DOTFILES_PATH" cd "$DOTFILES_PATH" if ! command_exists git; then _e "git not installed, trying to install" if command_exists apt; then _a "Installing using apt" sudo apt -y install git 2>&1 | _log "Installing git" elif command_exists dnf; then _a "Installing using dnf" sudo dnf -y install git 2>&1 | _log "Installing git" elif command_exists yum; then _a "Installing using yum" yes | sudo yum install git 2>&1 | _log "Installing git" elif command_exists brew; then _a "Installing using brew" yes | brew install git 2>&1 | _log "Installing git" elif command_exists pacman; then _a "Installing using pacman" sudo pacman -S --noconfirm git 2>&1 | _log "Installing git" else case "$OSTYPE" in darwin*) _a "Checking if Command Line Tools are installed 🕵️‍♂️" xcode-select --install 2>&1 | grep installed >/dev/null if [[ $? ]]; then _a "Installing Command Line Tools 📺" xcode-select --install _q "Press a key after command line tools has finished to continue...👇" "CLT_INSTALLED" fi ;; *) _e "Could not install git, no package provider found" exit 1 ;; esac fi fi if ! command_exists curl; then _e "curl not installed, trying to install" if command_exists apt; then _a "Installing using apt" sudo apt -y install curl 2>&1 | _log "Installing curl" elif command_exists dnf; then _a "Installing using dnf" sudo dnf -y install curl 2>&1 | _log "Installing curl" elif command_exists yum; then _a "Installing using yum" yes | sudo yum install curl 2>&1 | _log "Installing curl" elif command_exists brew; then _a "Installing using brew" yes | brew install curl 2>&1 | _log "Installing curl" elif command_exists pacman; then _a "Installing using pacman" sudo pacman -S --noconfirm curl 2>&1 | _log "Installing curl" else _e "Could not install curl, no package provider found" exit 1 fi fi _a "Initializing your dotfiles git repository" git init 2>&1 | _log "Initializing repository" _a "Cloning dotly" git submodule add -b "$DOTLY_BRANCH" "https://github.com/$DOTLY_REPOSITORY.git" "$dotly_inner_path" 2>&1 | _log "Adding dotly submodule" _a "Installing dotly dependencies" git submodule update --init --recursive 2>&1 | _log "Installing dotly dependencies" cd "$DOTLY_PATH" "$PWD/bin/dot" self install _a "🎉 dotly installed correctly! 🎉" _a "Please, restart your terminal to see the changes"