dotfiles/install.sh

146 lines
3.2 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
set -eo pipefail
user=$(whoami)
if [ "x$HOME" = "x" ]; then
echo "HOME must be set"
exit 1
fi
function is_sudo() {
if [ "x$SUDO_UID" != "x" ]; then
echo "Please run without sudo"
exit 1
fi
}
function detect_os() {
# Adapted from https://unix.stackexchange.com/a/6348
if [ -f /etc/os-release ]; then
# freedesktop.org and systemd
. /etc/os-release
OS=Arch
VER=$VERSION_ID
elif type lsb_release >/dev/null 2>&1; then
# linuxbase.org
OS=$(lsb_release -si)
VER=$(lsb_release -sr)
elif [ -f /etc/lsb-release ]; then
# For some versions of Debian/Ubuntu without lsb_release command
. /etc/lsb-release
OS=$DISTRIB_ID
VER=$DISTRIB_RELEASE
elif [ -f /etc/debian_version ]; then
# Older Debian/Ubuntu/etc.
OS=Debian
VER=$(cat /etc/debian_version)
else
# Fall back to uname, e.g. "Linux <version>", also works for BSD, etc.
OS=$(uname -s)
VER=$(uname -r)
fi
OS=$(echo "$OS" | tr '[:upper:]' '[:lower:]')
}
function detect_package_manager() {
local sudo
if [ "x$user" != "xroot" ]; then sudo="sudo "; fi
if [ "x$OS" = "xarch" ]; then
install_package_cmd="${sudo}pacman -S --noconfirm"
check_package_cmd="pacman -Q"
elif [ "x$OS" = "xdebian" ]; then
install_package_cmd="${sudo}apt-get install -y"
2024-02-15 16:00:56 -07:00
elif [ "x$OS" = "xdarwin" ]; then
if [ "x$user" = "xroot" ]; then
echo "Running as root on macOS is unsupported"
exit 1
fi
install_package_cmd="brew install"
check_package_cmd="brew list --formula"
else
echo "Unknown and unsupported package manager"
exit 1
fi
}
function check_package_installed() {
$check_package_cmd "$1" &>/dev/null || return 1
}
function install_git() {
if check_package_installed lazygit; then
echo "Git is already installed...skipping"
return
fi
echo "Installing git..."
$install_package_cmd lazygit
}
function install_oh_my_zsh() {
local oh_my_zsh_dir="$HOME/.oh-my-zsh"
if [ -d "$oh_my_zsh_dir" ]; then
echo "Oh My Zsh is already installed"
return
fi
echo "Installing Oh My Zsh..."
git -C "$HOME" clone https://code.cotugno.family/kevin/ohmyzsh.git "$(basename "$oh_my_zsh_dir")"
}
function install_asdf() {
local asdf_dir="$HOME/.asdf"
if [ -d "$asdf_dir" ]; then
echo "asdf is already installed"
return
fi
echo "Installing asdf..."
if [ "$OS" = "darwin" ]; then
$install_package_cmd asdf
. "/usr/local/opt/asdf/libexec/asdf.sh"
else
git -C "$HOME" clone https://code.cotugno.family/kevin/asdf.git "$(basename "$asdf_dir")"
git -C "$asdf_dir" checkout "$(git -C "$asdf_dir" describe --tags --abbrev=0)"
. "$asdf_dir/asdf.sh"
fi
}
function setup_asdf() {
local asdf_plugins=(ruby nodejs)
for plugin in ${asdf_plugins[@]}; do
if [[ ! -d "$HOME/.asdf/plugins/$plugin" ]]; then
echo "Installing asdf $plugin plugin"
asdf plugin add "$plugin"
asdf install "$plugin" latest
else
echo "asdf plugin $plugin already installed"
fi
done
}
function install_files() {
for file in $(cat install.txt); do
echo "Installing $file"
if [ "$(dirname "$file")" != "." ]; then
mkdir -p "$HOME/$(dirname "$file")"
fi
cp "$file" "$HOME/$file"
done
}
is_sudo
detect_os
detect_package_manager
install_git
install_oh_my_zsh
install_asdf
setup_asdf
install_files