#!/usr/bin/env bash set -euo pipefail source "$PWD/bash/bash_funcs" usage() { echo "Usage: $0 [link|remove|update]" exit 1 } # What operation to perform: link, remove, or update. OP="${1:-}" if [[ "$OP" == "" ]]; then OP="link" fi if [[ "$OP" != "link" && "$OP" != "remove" && "$OP" != "update" ]]; then usage fi GREEN_CHECK="$(printf '\x1b[32m✓\x1b[m')" BLACK=0 GREEN=2 YELLOW=3 BLUE=4 WHITE=7 # Print a status message with colors. status() { local text="$1" local bg_color="$2" local fg_color="$3" # Convert color numbers to ANSI codes. local bg_code=$((bg_color < 8 ? bg_color + 40 : bg_color + 92)) local fg_code=$((fg_color < 8 ? fg_color + 30 : fg_color + 82)) local padding padding=6 printf "\x1b[1;${bg_code};${fg_code}m %-${padding}s \x1b[m" "$text" } # Rendered status messages. SKIP="$(status "SKIP" "$BLUE" "$WHITE")" LINKED="$(status "LINKED" "$GREEN" "$BLACK")" BACKUP="$(status "BACKUP" "$YELLOW" "$BLACK")" UNLINKED="$(status "UNLINK" "$GREEN" "$BLACK")" # Create or remove a symlink. link() { local op="$1" local source="$PWD/$2" local target="$3" local display_source="./$2" local display_target="${target/#$HOME/\~}" if [[ $op == "remove" ]]; then # Remove symlink. if [ -L "$target" ]; then rm "$target" echo "$UNLINKED $display_target" else echo "$SKIP No symlink to unlink at $display_target" fi else # Create symlink. mkdir -p "$(dirname "$target")" if [ -L "$target" ] && [ "$(readlink "$target")" = "$source" ]; then # Link already exists. echo "$SKIP Link $display_source -> $display_target exists" elif [ -L "$target" ]; then # Wrong symlink exists, just replace it. ln -sf "$source" "$target" echo "$LINKED $display_source -> $display_target (replaced wrong symlink)" elif [ -e "$target" ]; then # There's a real file/directory there. Back it up, then link. mv "$target" "$target.backup.$(date +%s)" ln -sf "$source" "$target" echo "$BACKUP Linked $display_source to $display_target (backed up existing)" else # Nothing there, create link. ln -sf "$source" "$target" echo "$LINKED $display_source -> $display_target" fi fi } # Ensure a command exists. If it does not, install it using the given command. ensure() { local name="$1" local cmd="$2" local script="$3" # Require curl for installation checks. if ! command -v curl &>/dev/null; then printf "curl is not installed. Please install curl first.\n" return fi if command -v "$cmd" &>/dev/null; then printf "%s %s is installed.\n" "$GREEN_CHECK" "$name" else printf "Installing %s...\n" "$name" eval "$script" fi } # Install a Haskell package using cabal. cabal_install() { local pkg="$1" cabal install "$pkg" -j"$(num_cores)" --overwrite-policy=always } # Install the V programming language from source. install_v() { local here="$PWD" local targetDir="$HOME/src/v" rm -rf "$targetDir" mkdir -p "$targetDir" git clone --depth=1 https://github.com/vlang/v "$targetDir" cd "$targetDir" make sudo ./v symlink cd "$here" } os="$(which_os)" # Generally install or update stuff. if [[ $OP == "update" ]]; then git submodule update --init --recursive bash "./scripts/setup-bash" ensure "GHCup" "ghcup" "curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh" if command -f cabal &>/dev/null; then cabal update fi ensure "Fourmolu" "fourmolu" "cabal_install fourmolu" ensure "Floskell" "floskell" "cabal_install floskell" ensure "V" "v" "install_v" case $os in darwin) ensure "Nix" "nix" "sh <(curl --proto '=https' --tlsv1.2 -L https://nixos.org/nix/install)" ensure "Homebrew" "brew" "sh -c \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\"" ensure "cabal-fmt" "cabal-fmt" "cabal install cabal-fmt -j$(num_cores)" brew bundle --verbose ;; esac fi if [ -z "$XDG_CONFIG_HOME" ]; then XDG_CONFIG_HOME="$HOME/.config" fi if [ -z "$XDG_DATA_HOME" ]; then XDG_DATA_HOME="$HOME/.local/share" fi # Bash. link "$OP" "bash/profile" "$HOME/.profile" link "$OP" "bash/bash_profile" "$HOME/.bash_profile" link "$OP" "bash/bashrc" "$HOME/.bashrc" link "$OP" "bash/bash_funcs" "$XDG_DATA_HOME/meowgorithm/bash_funcs" link "$OP" "pantera/keys.asc" "$XDG_DATA_HOME/meowgorithm/keys.asc" link "$OP" "git/config" "$XDG_CONFIG_HOME/git/config" link "$OP" "rio/config.toml" "$XDG_CONFIG_HOME/rio/config.toml" link "$OP" "crush/config.json" "$XDG_CONFIG_HOME/crush/crush.json" link "$OP" "tmux/tmux.conf" "$HOME/.tmux.conf" link "$OP" "vim" "$XDG_CONFIG_HOME/vim" link "$OP" "helix" "$XDG_CONFIG_HOME/helix" # Kitty. link "$OP" "kitty/kitty.conf" "$XDG_CONFIG_HOME/kitty/kitty.conf" if [[ $os = "darwin" ]]; then link "$OP" "kitty/darwin.conf" "$XDG_CONFIG_HOME/kitty/os.conf" else link "$OP" "kitty/linux.conf" "$XDG_CONFIG_HOME/kitty/os.conf" fi # Ghostty link "$OP" "ghostty/config" "$XDG_CONFIG_HOME/ghostty/config" if [[ $os = "darwin" ]]; then link "$OP" "ghostty/darwin" "$XDG_CONFIG_HOME/ghostty/os" else link "$OP" "ghostty/linux" "$XDG_CONFIG_HOME/ghostty/os" fi # Fourmolu. link "$OP" "fourmolu/fourmolu.yaml" "$XDG_CONFIG_HOME/fourmolu.yaml" # Hyprland. case "$os" in nixos) link "$OP" "hypr/hyprland.conf" "$XDG_CONFIG_HOME/hypr/hyprland.conf" ;; esac # Scripts link "$OP" "scripts/install_crush" "$HOME/.bin/install_crush" link "$OP" "scripts/uninstall_crush" "$HOME/.bin/uninstall_crush" link "$OP" "scripts/gpg-edit-key" "$HOME/.bin/gpg-edit-key" link "$OP" "scripts/gpg-send" "$HOME/.bin/gpg-recv"