#!/bin/bash set -eo pipefail if command -v unzip > /dev/null; then UNZIPCMD="unzip" fi if command -v vim > /dev/null; then VIMCMD="vim" fi if command -v git > /dev/null; then GITCMD="git" fi if command -v wget > /dev/null; then DLCMD="wget -O" elif command -v curl > /dev/null; then DLCMD="curl -Lo" fi if [ -z "$UNZIPCMD" ] || [ -z "$VIMCMD" ] || [ -z "$DLCMD" ] || [ -z "$GITCMD" ]; then echo "requires 'unzip', 'vim', 'git', and either 'wget' or 'curl'" exit 1 fi set -u if [ ! "$(pwd)" = "$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" ]; then echo "expects to be run from the root of the dotfiles directory" exit 1 fi uname=$(uname | tr '[:upper:]' '[:lower:]') # XDG layout xcache="${XDG_CACHE_HOME:-${HOME}/.cache}" xdata="${XDG_DATA_HOME:-${HOME}/.local/share}" xconfig="${XDG_CONFIG_HOME:-${HOME}/.config}" userbin="$HOME/.local/bin" systemd="$xconfig/systemd/user" mkdir -p "$xcache" "$xdata" "$xconfig" "$userbin" mkdir -p "${xcache}/zsh" mkdir -p "${xdata}/zsh" if [ "linux" = "$uname" ]; then mkdir -p "$systemd" fi function link { local src="$1" local dest="$2" if [ -e "$dest" ] && [ ! -L "$dest" ]; then mkdir -p dotfiles-backup mv "$dest" dotfiles-backup/. fi ln -snf "$src" "$dest" } function link_contents { local src="$1" local dest="$2" local prefix="${3:-}" if [ -e "$src" ]; then for f in "$src"/* do link "$(pwd)/$f" "$dest/$prefix$(basename "$f")" done fi } function deploy { local src="$1" local dest="$2" link_contents "$src/all" "$dest" link_contents "$src/$uname" "$dest" } # deploy to home link_contents home "$HOME" '.' # deploy to XDG dirs deploy config "$xconfig" # deploy user executables deploy bin "$userbin" # deploy systemd user units deploy systemd "$systemd" autoload="$HOME/.vim/autoload" mkdir -p "$autoload" # install vim-plug if [ ! -e "$HOME/.vim/autoload/plug.vim" ]; then $DLCMD "$HOME/.vim/autoload/plug.vim" \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim $VIMCMD +PlugInstall +qall fi # deploy custom scripts to vim autoload link_contents vim "$autoload" # create local files if [ ! -e "$HOME/.config/zsh/00-local.zsh" ]; then cp local/local.zsh "$HOME/.config/zsh/00-local.zsh" fi if [ ! -e "$HOME/.config/git/config-local" ]; then cp local/gitconfig-local "$HOME/.config/git/config-local" fi if [ "$uname" = "linux" ]; then dconfig="$xconfig/duplicity" mkdir -p "$dconfig" if [ ! -e "$dconfig/config" ]; then cp local/duplicity-config "$dconfig/config" fi if [ ! -e "$dconfig/excludes" ]; then cp local/duplicity-excludes "$dconfig/excludes" fi fi