Files
dotfiles/bin/.local/bin/bootstrap.sh

156 lines
4.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
log() { printf "\n==> %s\n" "$*"; }
have() { command -v "$1" >/dev/null 2>&1; }
DOTFILES_REPO="https://code.lab.unbl.ink/secstate/dotfiles"
DOTFILES_DIR="${DOTFILES_DIR:-$HOME/.dotfiles}"
OMZ_DIR="${ZSH:-$HOME/.oh-my-zsh}"
DOOM_DIR="${DOOM_DIR:-$HOME/.emacs.d}"
OS="$(uname -s)"
PLATFORM=""
# -------- Detect platform --------
if [[ "$OS" == "Darwin" ]]; then
PLATFORM="macos"
elif [[ "$OS" == "FreeBSD" ]]; then
PLATFORM="freebsd"
elif [[ "$OS" == "Linux" ]]; then
if [[ -r /etc/os-release ]]; then
# shellcheck disable=SC1091
. /etc/os-release
fi
is_arch=0
case "${ID:-}" in
arch | endeavouros) is_arch=1 ;;
esac
if [[ $is_arch -eq 0 && -n "${ID_LIKE:-}" ]] && echo "$ID_LIKE" | grep -qi arch; then
is_arch=1
fi
is_fedora=0
case "${ID:-}" in
fedora) is_fedora=1 ;;
esac
if [[ $is_fedora -eq 0 && -n "${ID_LIKE:-}" ]] && echo "$ID_LIKE" | grep -qiE 'fedora|rhel'; then
is_fedora=1
fi
if [[ $is_arch -eq 1 ]]; then
PLATFORM="arch"
elif [[ $is_fedora -eq 1 ]]; then
PLATFORM="fedora"
else
echo "Unsupported Linux distro (ID=${ID:-unknown}, ID_LIKE=${ID_LIKE:-unknown})."
exit 1
fi
else
echo "Unsupported OS: $OS"
exit 1
fi
# -------- Install basics (git, stow, zsh, curl, make) --------
install_arch() {
log "Installing prerequisites (Arch/EndeavourOS)..."
sudo pacman -Sy --needed --noconfirm git stow zsh curl make openssh
}
install_fedora() {
log "Installing prerequisites (Fedora)..."
sudo dnf install -y git stow zsh curl make openssh-clients
}
install_macos() {
log "Ensuring Homebrew..."
if ! have brew; then
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
# shellenv for both Apple Silicon and Intel
if [[ -x /opt/homebrew/bin/brew ]]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
elif [[ -x /usr/local/bin/brew ]]; then
eval "$(/usr/local/bin/brew shellenv)"
fi
log "Installing prerequisites (macOS via brew)..."
brew update
brew install git stow zsh curl gnu-make
}
install_freebsd() {
log "Installing prerequisites (FreeBSD)..."
sudo pkg update -f
# gmake is GNU make; stow is stow; git/curl/zsh are available
sudo pkg install -y git stow zsh curl gmake
}
case "$PLATFORM" in
arch) install_arch ;;
fedora) install_fedora ;;
macos) install_macos ;;
freebsd) install_freebsd ;;
esac
# -------- Clone/update dotfiles --------
if [[ -d "$DOTFILES_DIR/.git" ]]; then
log "Updating dotfiles in $DOTFILES_DIR..."
git -C "$DOTFILES_DIR" pull --rebase
else
log "Cloning dotfiles to $DOTFILES_DIR..."
git clone "$DOTFILES_REPO" "$DOTFILES_DIR"
fi
# -------- Build/apply dotfiles (make vs gmake) --------
MAKE_CMD="make"
if [[ "$PLATFORM" == "freebsd" ]]; then
MAKE_CMD="gmake"
elif [[ "$PLATFORM" == "macos" ]]; then
# prefer GNU make if available (brew installs as gmake)
if have gmake; then MAKE_CMD="gmake"; fi
fi
log "Running $MAKE_CMD in dotfiles repo..."
(cd "$DOTFILES_DIR" && "$MAKE_CMD")
# -------- oh-my-zsh --------
if [[ -d "$OMZ_DIR" ]]; then
log "oh-my-zsh already present at $OMZ_DIR"
else
log "Cloning oh-my-zsh..."
git clone --depth=1 https://github.com/ohmyzsh/ohmyzsh "$OMZ_DIR"
fi
# -------- Doom Emacs --------
if [[ -d "$DOOM_DIR/.git" ]]; then
log "Doom Emacs already present at $DOOM_DIR (updating)..."
git -C "$DOOM_DIR" pull --rebase || true
elif [[ -e "$DOOM_DIR" ]]; then
log "Skipping Doom Emacs clone because $DOOM_DIR exists but is not a git repo."
log "Move it aside if you want to install Doom there."
else
log "Cloning Doom Emacs to $DOOM_DIR..."
git clone --depth=1 https://github.com/doomemacs/doomemacs "$DOOM_DIR"
fi
# -------- Next steps --------
log "Next steps"
cat <<'EOF'
1) Start a new shell (or open a new terminal) so shell config reloads.
2) If you want zsh as default:
- Linux: chsh -s $(command -v zsh)
- macOS: chsh -s /bin/zsh (or $(command -v zsh) if preferred)
- FreeBSD: chsh -s $(command -v zsh)
3) Doom Emacs:
~/.emacs.d/bin/doom install
(And later, when you change config: ~/.emacs.d/bin/doom sync)
EOF
log "Done."