2022-07-30 20:11:35 -05:00
#!/usr/bin/env bash
set -eo pipefail
2022-07-30 21:13:25 -05:00
DEPS_PATH = " ${ HOME } /.local/share "
2022-07-30 22:24:52 -05:00
SCRIPT_DIR = " $( cd -- " $( dirname -- " ${ BASH_SOURCE [0] } " ) " & >/dev/null && pwd ) "
PKG_INSTALL_CMD = ""
2022-07-30 21:13:25 -05:00
BOLD = $( tput bold)
CYAN = $( tput setaf 6)
GREEN = $( tput setaf 2)
YELLOW = $( tput setaf 3)
2022-07-30 21:19:56 -05:00
RED = $( tput setaf 1)
2022-07-30 21:13:25 -05:00
RESET = $( tput sgr0)
2022-07-30 22:24:52 -05:00
RESET_BOLD = " ${ RESET } ${ BOLD } "
2022-07-30 21:13:25 -05:00
log( ) {
# Print a message and send it to stdout or stderr depending upon log level, also configurable with debug etc.
#
# Arguments:
# level <type: string> <position: 1> <required: true>
# - The log level, defined within a case check in this function
# message <type: string> <position: 2> <required: true>
# - The info message
#
# Usage:
# log "info" "Could not find that directory"
#
# POSIX Compliant:
# Yes
#
# Convert the level to uppercase
local level
2022-07-30 22:24:52 -05:00
level = $( printf "%s" " ${ 1 } " | tr '[:lower:]' '[:upper:]' )
2022-07-30 21:13:25 -05:00
local message
2022-07-30 22:24:52 -05:00
message = " ${ 2 } "
2022-07-30 21:13:25 -05:00
case " ${ level } " in
INFO)
printf "%sINFO:%s %s%s%s\n" \
" ${ CYAN } " \
" ${ RESET } " \
" ${ BOLD } " \
" ${ message } " \
" ${ RESET } " >& 2
return 0
; ;
WARN*)
printf "%sWARN:%s %s%s%s\n" \
" ${ YELLOW } " \
" ${ RESET } " \
" ${ BOLD } " \
" ${ message } " \
" ${ RESET } " >& 2
return 0
; ;
ERROR)
printf "%sERROR:%s %s%s%s\n" \
" ${ RED } " \
" ${ RESET } " \
" ${ BOLD } " \
" ${ message } " \
" ${ RESET } " >& 2
return 0
; ;
2022-07-30 22:24:52 -05:00
# Further log levels can be added by extending this switch statement with more comparisons
2022-07-30 21:13:25 -05:00
esac
}
print-break( ) {
printf " ${ GREEN } %.s─ ${ RESET } " $( seq 1 " $( tput cols) " )
}
log " ${ @ } "
2022-07-30 20:11:35 -05:00
install-fzf( ) {
2022-07-30 22:24:52 -05:00
local install_path = " ${ 1 } /fzf "
2022-07-31 20:53:59 -05:00
if ! [ [ -e " ${ install_path } " ] ] ; then
2022-07-30 22:24:52 -05:00
git clone --depth 1 https://github.com/junegunn/fzf.git " ${ install_path } "
2022-07-31 20:53:09 -05:00
fi
if ! [ [ -e " ${ HOME } /.config/fzf/fzf.zsh " ] ] ; then
2022-07-31 20:55:05 -05:00
log "info" " No ${ GREEN } fzf ${ RESET_BOLD } config file found, generating "
2022-07-30 22:24:52 -05:00
" ${ install_path } /install " \
--key-bindings \
--completion \
--no-update-rc \
--xdg >/dev/null
else
log "info" " ${ GREEN } FZF ${ RESET_BOLD } already installed, skipping "
fi
2022-07-30 21:13:25 -05:00
}
install-rust( ) {
local install_path = " ${ 1 } "
export CARGO_HOME = " ${ install_path } /cargo "
export RUSTUP_HOME = " ${ install_path } /rustup "
2022-07-30 22:24:52 -05:00
if ! [ [ -e " ${ CARGO_HOME } " ] ] || ! [ [ -e " ${ RUSTUP_HOME } " ] ] ; then
2022-07-30 22:58:57 -05:00
log "info" " Rust installs set to ${ GREEN } cargo ${ RESET_BOLD } : ${ GREEN } ${ CARGO_HOME } ${ RESET_BOLD } & ${ GREEN } rustup ${ RESET_BOLD } : ${ GREEN } ${ RUSTUP_HOME } ${ RESET_BOLD } "
2022-07-30 22:56:10 -05:00
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --quiet
2022-07-30 22:24:52 -05:00
else
log "info" " ${ GREEN } Rust ${ RESET_BOLD } already installed, skipping "
fi
2022-07-30 23:16:37 -05:00
if [ [ -z " $( ls " ${ RUSTUP_HOME } /toolchains " >/dev/null) " ] ] ; then
log "info" " No toolchain found, installing a ${ GREEN } Rust ${ RESET_BOLD } toolchain "
log "info" " Setting ${ GREEN } Rust ${ RESET_BOLD } 's toolchain to ${ GREEN } stable ${ RESET_BOLD } "
if ! " ${ CARGO_HOME } /bin/rustup " default stable; then
log "error" " Failed to set a ${ GREEN } Rust ${ RESET_BOLD } toolchain "
return 1
fi
else
log "info" " ${ GREEN } Rust ${ RESET_BOLD } toolchain found, skipping toolchain setup "
fi
2022-07-30 23:10:30 -05:00
export PATH = " ${ PATH } : ${ install_path } /cargo/bin "
2022-07-30 22:30:20 -05:00
2022-07-30 21:13:25 -05:00
}
2022-07-30 23:40:34 -05:00
install-omz( ) {
local install_path = " ${ 1 } /omz "
if ! [ [ -e " ${ install_path } " ] ] ; then
git clone https://github.com/ohmyzsh/ohmyzsh.git " ${ install_path } "
else
log "info" " ${ GREEN } oh-my-zsh ${ RESET_BOLD } already installed, skipping "
fi
}
2022-07-30 21:13:25 -05:00
install-cargo-binary( ) {
local binary = " ${ 1 } "
cargo install " ${ binary } "
2022-07-30 20:11:35 -05:00
}
2022-07-31 00:36:04 -05:00
install-autojump( ) {
local install_path = " ${ 1 } /autojump "
2022-07-31 20:44:23 -05:00
if ! [ [ -e " ${ install_path } " ] ] ; then
cd " $( mktemp -d) "
git clone "https://github.com/wting/autojump.git" "autojump" || return 1
(
cd "autojump"
python3 "install.py" -d " ${ install_path } "
sed -i "s/\#\!\/usr\/bin\/env\ python/\#\!\/usr\/bin\/env\ python3/" \
" ${ install_path } /bin/autojump "
) || return 1
else
log "info" " ${ GREEN } Autojump ${ RESET_BOLD } already installed, skipping "
fi
2022-07-31 00:36:04 -05:00
}
2022-07-30 20:11:35 -05:00
check-script-deps( ) {
2022-07-30 21:13:25 -05:00
local commands_to_check = (
2022-07-30 20:11:35 -05:00
git
2022-07-30 21:13:25 -05:00
gcc
2022-07-30 23:13:13 -05:00
make
2022-07-31 00:14:18 -05:00
python3
2022-07-30 20:11:35 -05:00
)
2022-07-30 21:13:25 -05:00
local ret_code = 0
2022-07-30 20:11:35 -05:00
for cmd in " ${ commands_to_check [@] } " ; do
2022-07-30 22:24:52 -05:00
if ! command -v " ${ cmd } " >/dev/null; then
2022-07-30 23:15:13 -05:00
log "error" " Could not find command: ${ GREEN } ${ cmd } ${ RESET_BOLD } , ensure you install this dependency "
2022-07-30 21:13:25 -05:00
ret_code = 1
2022-07-30 22:24:52 -05:00
else
log "info" " Found dependency ${ GREEN } ${ cmd } ${ RESET_BOLD } "
2022-07-30 20:11:35 -05:00
fi
done
2022-07-30 21:13:25 -05:00
2022-07-30 21:18:28 -05:00
return " ${ ret_code } "
2022-07-30 20:11:35 -05:00
}
2022-07-30 22:24:52 -05:00
install-from-pkg-mngr( ) {
if [ [ " ${ SKIP_PKG_INSTALL } " = true ] ] ; then
return
fi
local pkg_to_install = " ${ * } "
2022-07-30 23:05:27 -05:00
local ret_code = 0
2022-07-30 22:24:52 -05:00
log "info" " Installing ${ GREEN } ${ pkg_to_install } ${ RESET_BOLD } "
2022-07-30 23:05:27 -05:00
if ! " $( eval " ${ PKG_INSTALL_CMD } " " ${ pkg_to_install } " ) " ; then
2022-07-30 22:24:52 -05:00
log "error" " Failed to install ${ GREEN } ${ pkg_to_install } ${ RESET_BOLD } "
2022-07-30 23:05:27 -05:00
ret_code = 1
fi
if ( ( ret_code > 0) ) ; then
return 1
else
log "info" " Successfully installed ${ GREEN } ${ pkg_to_install } ${ RESET_BOLD } "
2022-07-30 22:24:52 -05:00
fi
}
2022-07-31 00:13:12 -05:00
install-pyenv( ) {
local install_path = " ${ 1 } /pyenv "
2022-07-31 00:49:37 -05:00
if ! [ [ -e " ${ install_path } " ] ] ; then
git clone https://github.com/pyenv/pyenv.git " ${ install_path } " || return 1
(
cd " ${ install_path } "
src/configure && make -C src
) || return 1
else
log "info" " ${ GREEN } Pyenv ${ RESET_BOLD } already installed, skipping "
fi
2022-07-31 00:13:12 -05:00
}
2022-07-31 00:30:45 -05:00
create-dirs( ) {
local dirs = (
" ${ HOME } /.local/bin "
" ${ HOME } /.local/share "
)
ret_code = 0
for dir in " ${ dirs [@] } " ; do
log "info" " Creating directory ${ GREEN } ${ dir } ${ RESET_BOLD } "
if ! mkdir -p " ${ dir } " ; then
log "error" " Failed to create directory: ${ GREEN } ${ dir } ${ RESET_BOLD } "
ret_code = 1
fi
done
return " ${ ret_code } "
}
2022-07-30 22:24:52 -05:00
deploy-config( ) {
local install_paths
declare -A install_paths = (
[ "zshrc" ] = ".zshrc"
[ "ZSH-Config" ] = ".config/zsh"
)
for install_key in " ${ !install_paths[@] } " ; do
local src_path = " ${ SCRIPT_DIR } / ${ install_paths [ ${ install_key } ] } "
local dest_path = " ${ HOME } / ${ install_paths [ ${ install_key } ] } "
if [ [ -e " ${ dest_path } " ] ] ; then
log "info" " ${ GREEN } ${ install_key } ${ RESET_BOLD } already exists at ${ GREEN } ${ dest_path } ${ RESET_BOLD } , skipping "
continue
fi
2022-07-30 22:27:28 -05:00
log "info" " Linking ${ GREEN } ${ install_key } ${ RESET_BOLD } , ${ GREEN } ${ src_path } ${ RESET_BOLD } to ${ GREEN } ${ dest_path } ${ RESET_BOLD } "
if ! ln -s " ${ src_path } " " ${ dest_path } " ; then
log "error" " Failed linking ${ GREEN } ${ install_key } ${ RESET_BOLD } , ${ GREEN } ${ src_path } ${ RESET_BOLD } to ${ GREEN } ${ dest_path } ${ RESET_BOLD } "
2022-07-30 22:24:52 -05:00
return 1
fi
done
}
2022-07-30 20:11:35 -05:00
main( ) {
2022-07-30 22:46:09 -05:00
local tasks_done = ( )
2022-07-30 22:24:52 -05:00
PKG_INSTALL_CMD = " ${ * } "
2022-07-30 22:56:10 -05:00
if [ [ -z " ${ PKG_INSTALL_CMD } " ] ] && ! [ [ " ${ SKIP_PKG_INSTALL } " = true ] ] ; then
2022-07-30 23:24:39 -05:00
log "error" " A package installer must be passed to install missing packages, or ${ GREEN } SKIP_PKG_INSTALL=true ${ RESET_BOLD } must be set. "
2022-07-30 22:24:52 -05:00
return 1
fi
log "info" " Dependencies directory set to ${ GREEN } ${ DEPS_PATH } ${ RESET_BOLD } "
log "info" " Packager install command set to ${ GREEN } ${ PKG_INSTALL_CMD } ${ RESET_BOLD } "
2022-07-30 20:11:35 -05:00
mkdir -p " ${ DEPS_PATH } "
2022-07-30 21:13:25 -05:00
print-break
2022-07-30 20:11:35 -05:00
2022-07-30 21:13:25 -05:00
log "info" "Checking script dependencies..."
2022-07-30 20:11:35 -05:00
if ! check-script-deps; then
2022-07-30 23:06:21 -05:00
log "error" "Script dependencies failed, install missing dependencies and try again"
exit 1
2022-07-30 21:13:25 -05:00
fi
log "info" "Script dependencies good"
print-break
2022-07-31 00:30:45 -05:00
log "info" "Creating directories"
if ! create-dirs; then
log "error" "Unable to create required directories"
exit 1
fi
log "info" "Finished creating directories"
print-break
2022-07-30 22:24:52 -05:00
log "info" " Installing ${ GREEN } FZF ${ RESET_BOLD } "
2022-07-30 21:13:25 -05:00
if ! install-fzf " ${ DEPS_PATH } " ; then
2022-07-30 22:24:52 -05:00
log "error" " Failed to install ${ GREEN } FZF ${ RESET_BOLD } "
2022-07-30 21:13:25 -05:00
exit 1
fi
2022-07-30 22:24:52 -05:00
log "info" " Successfully installed ${ GREEN } FZF ${ RESET_BOLD } "
2022-07-30 22:46:09 -05:00
tasks_done += ( "Install FZF" )
2022-07-30 21:13:25 -05:00
print-break
log "info" "Installing Rust"
if ! install-rust " ${ DEPS_PATH } " ; then
2022-07-30 22:24:52 -05:00
log "error" " Failed to install ${ GREEN } Rust ${ RESET_BOLD } "
2022-07-30 21:13:25 -05:00
exit 1
2022-07-30 20:11:35 -05:00
fi
2022-07-30 22:24:52 -05:00
log "info" " Successfully installed ${ GREEN } Rust ${ RESET_BOLD } "
2022-07-30 22:46:09 -05:00
tasks_done += ( "Install Rust" )
2022-07-30 21:13:25 -05:00
print-break
2022-07-30 20:11:35 -05:00
2022-07-30 21:13:25 -05:00
log "info" "Installing Cargo programs"
local cargo_binaries = (
ripgrep
exa
2022-07-30 23:13:13 -05:00
fd-find
2022-07-30 23:18:15 -05:00
bat
2022-07-30 21:13:25 -05:00
)
for pkg in " ${ cargo_binaries [@] } " ; do
2022-07-30 22:24:52 -05:00
log "info" " Attempting install of ${ GREEN } ${ pkg } ${ RESET_BOLD } "
2022-07-30 21:13:25 -05:00
if ! install-cargo-binary " ${ pkg } " ; then
2022-07-30 22:24:52 -05:00
log "error" " Failed installation of ${ GREEN } ${ pkg } ${ RESET_BOLD } "
2022-07-30 21:13:25 -05:00
exit 1
fi
done
log "info" "Finished installing Cargo programs"
2022-07-30 22:24:52 -05:00
print-break
2022-07-31 00:16:07 -05:00
log "info" " Installing ${ GREEN } Pyenv ${ RESET_BOLD } "
if ! install-pyenv " ${ DEPS_PATH } " ; then
2022-07-31 00:36:04 -05:00
log "error" " Failed to install ${ GREEN } Pyenv ${ RESET_BOLD } "
exit 1
fi
log "info" " Finished installing ${ GREEN } Pyenv ${ RESET_BOLD } "
print-break
log "info" " Installing ${ GREEN } autojump ${ RESET_BOLD } "
2022-07-31 00:37:43 -05:00
if ! install-autojump " ${ DEPS_PATH } " ; then
2022-07-31 00:36:04 -05:00
log "error" " Failed to install ${ GREEN } autojump ${ RESET_BOLD } "
exit 1
2022-07-31 00:16:07 -05:00
fi
2022-07-31 00:36:04 -05:00
log "info" " Finished installing ${ GREEN } autojump ${ RESET_BOLD } "
2022-07-31 20:41:04 -05:00
print-break
2022-07-31 00:16:07 -05:00
2022-07-30 23:42:43 -05:00
log "info" " Install ${ GREEN } Oh My ZSH ${ RESET_BOLD } "
2022-07-30 23:43:20 -05:00
install-omz " ${ DEPS_PATH } "
2022-07-30 23:42:43 -05:00
log "info" " Finished installing ${ GREEN } Oh My ZSH ${ RESET_BOLD } "
2022-07-30 23:43:48 -05:00
print-break
2022-07-30 23:42:43 -05:00
2022-07-30 22:24:52 -05:00
log "info" " Installing ${ GREEN } ZSH ${ RESET_BOLD } "
if ! command -v "zsh" >/dev/null; then
if ! install-from-pkg-mngr "zsh" ; then
log "error" " Failed to install ${ GREEN } ZSH ${ RESET_BOLD } "
exit 1
fi
else
log "info" " ${ GREEN } ZSH ${ RESET_BOLD } already installed, skipping "
fi
log "info" " Successfully installed ${ GREEN } ZSH ${ RESET_BOLD } "
log "info" " Deploying ${ GREEN } ZSH ${ RESET_BOLD } configuration files "
if ! deploy-config; then
log "error" "Failed to deploy configuration files"
exit 1
fi
log "info" " Successfully installed ${ GREEN } ZSH ${ RESET_BOLD } configuration files "
2022-07-30 23:31:44 -05:00
print-break
2022-07-30 22:24:52 -05:00
log "info" " Finished installing ${ GREEN } ZSH ${ RESET_BOLD } configurations and dependencies "
2022-07-30 20:11:35 -05:00
}
2022-07-30 21:16:11 -05:00
2022-07-30 22:24:52 -05:00
main " ${ @ } "