aboutsummaryrefslogtreecommitdiff
path: root/deploy.sh
blob: f4fe57a15226be2e15f2e303d1c495fcb21c1363 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/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="$xdata/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