-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit-dots
130 lines (106 loc) · 4.07 KB
/
init-dots
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
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash
#
# Initialize a dotfile repository using the dots manager
#
# 1. Create the $HOME/src/git.other directory
# 2. Clone the dots manager tool into this directory
# 3. Kick off the dots/contrib/initialize script to handle the rest of the setup
# of the dotfiles. The DOTS_CLONE_DIR environment will be set for this
#
# This script must be sourced!
[[ $_ == $0 ]] && echo "Please source me!" && exit 1
# XDG directories
export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
# Clone the dots manager utility
if [[ ! -d "$HOME/src/git.other/dots" ]]
then
echo -e "\033[0;32m==>\033[0m Installing dots managment utility"
git clone -q https://github.com/EvanPurkhiser/dots "$HOME/src/git.other/dots" --branch releases/1.x
fi
# Setup the environment for the new dots repository
DOTS_CLONE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Make sure target directories exist
mkdir -p "$XDG_CONFIG_HOME" "$XDG_CACHE_HOME" "$XDG_DATA_HOME"
mkdir -p "$HOME/.local/bin" "$HOME/src/git.other"
# dots expects stuff in ~/.local/etc/… create symlink so stuff stays working
# will be replaced by config option in future version of dots
ln -sf "$HOME/src/git.other/dotfiles" "$HOME/.local/etc"
# contrib/initalize script
# Initalization script which will do a few things:
#
# 1. Move the cloned dotfiles directory into $HOME/src/git.other/dotfiles
# 2. Link the dots executable into $HOME/.local/bin
# 3. Add the $HOME/.local/bin directory to the $PATH if it isn't already
# 4. Source the dots/contrib/bash_completion script
#
# The `DOTS_CLONE_DIR` environment should be set to the
#
# This script MUST be sourced!
__dots_init()
{
local arrow_bad="\033[0;31m==>\033[0m"
local arrow_good="\033[0;32m==>\033[0m"
local arrow_warn="\033[0;33m==>\033[0m"
if [[ -z "$DOTS_CLONE_DIR" ]]
then
echo -e "$arrow_bad \$DOTS_CLONE_DIR must be set to the dots repository being setup"
return
fi
# The directory containing the cloned dotfiles to setup
local cloned_dir="$DOTS_CLONE_DIR"
# The dots utility directory
local dots_util_dir="$(cd "$HOME/src/git.other/dots/" && git rev-parse --show-toplevel)"
# The installation directory
local install_dir="$HOME/src/git.other/dotfiles"
# The directory where the user keeps his executables
local bin_dir="$HOME/.local/bin"
# 1. Move everything into the install directory if we need to
if [[ "$install_dir" != "$cloned_dir" ]]
then
# Make sure the installation directory isn't in use
if [ -e "$install_dir" ] && ! find "$install_dir" -maxdepth 0 -empty | read v
then
echo -e "$arrow_bad $install_dir directory not empty"
return
fi
# Move everything over
echo -e "$arrow_good Moving $cloned_dir to $install_dir"
# 1. Create the directory tree up to the install directory
# 2. Remove the empty install_dir base directory so we can move the
# cloned_dir into its place instead of inside of it
mkdir -p "$install_dir"
rm -rf "$install_dir"
mv -f "$cloned_dir" "$install_dir"
# Get out of the empty directory if we were in it
[[ "$(pwd)" == "$cloned_dir" ]] && cd
fi
# 2. Link the dots script into the users local bin
if [[ ! -e "$bin_dir/dots" ]]
then
echo -e "$arrow_good Linking $install_dir/dots into $bin_dir"
mkdir -p "$bin_dir"
ln -s "$dots_util_dir/bin/dots" "$bin_dir/"
# If the dots file already exists in bin check if it's not the same
elif [[ "$(readlink "$bin_dir/dots")" != "$dots_util_dir/bin/dots" ]]
then
echo -e "$arrow_warn $bin_dir/dots already exists!"
fi
# 3. Add the bin directory to the front of the path
if [[ ":$PATH:" != *":$bin_dir:"* ]]
then
echo -e "$arrow_good Temporarily adding $bin_dir to your PATH"
PATH="$HOME/.local/bin:$PATH"
fi
# 4. Source in the dots completion
if ! type -t __dots_completions | grep -q '^function$' 2>/dev/null
then
echo -e "$arrow_good Sourcing bash completions for dots"
source "$dots_util_dir/contrib/bash_completion"
fi
echo -e "$arrow_good Dot files ready to be installed!"
}
__dots_init
unset -f __dots_init
unset DOTS_CLONE_DIR