-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall_bashfiles.bash
More file actions
executable file
·162 lines (141 loc) · 5.32 KB
/
install_bashfiles.bash
File metadata and controls
executable file
·162 lines (141 loc) · 5.32 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env bash
function _platform() {
local kernel
kernel="$(uname -s)"
case "$kernel" in
Linux)
echo "ubuntu"
;;
Darwin)
echo "macos"
;;
esac
}
function do_ipython_install() {
# determine the folder this script is in
local ROOTDIR
ROOTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
local IPYTHON_PROFILE_DIR
IPYTHON_PROFILE_DIR="$(ipython locate profile)"
# shellcheck disable=SC2181
if [[ $? != 0 ]]; then
return
fi
for ipython_config_file in "$ROOTDIR/.ipython/"*; do
ln -s -n "$ipython_config_file" "$IPYTHON_PROFILE_DIR/$(basename "$ipython_config_file")" 2>/dev/null
done
}
function remove_broken_symlinks() {
local ROOTDIR
ROOTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
local target="$1"
shopt -s dotglob
for file in "$target/"*; do
if [[ -h "$file" && ! -e "$file" ]]; then
local symlink_target
symlink_target="$(readlink -n "$file")"
[[ "$symlink_target" = "$ROOTDIR"/* ]] && rm "$file"
fi
done
}
function do_install() {
# if we're being piped into bash, then clone
if [[ ! -t 0 && "$0" == "bash" && -z "${BASH_SOURCE[0]}" ]]; then
if [[ -e "$HOME/bashfiles" ]]; then
echo "$HOME/bashfiles already exists. Perhaps you meant to run $HOME/bashfiles/install_bashfiles.bash?"
return 1
fi
git clone https://github.com/stevenkaras/bashfiles.git "$HOME/bashfiles" && return $?
"$HOME"/bashfiles/install_bashfiles.bash
return $?
fi
# determine the folder this script is in
local ROOTDIR
ROOTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
remove_broken_symlinks "$HOME"
for bashfile in "$ROOTDIR"/.bash*; do
local bashfilename
bashfilename="$(basename "$bashfile")"
# don't accidentally create recursive symlinks
if [[ ! -h "$bashfile" ]]; then
ln -s -n "$bashfile" "$HOME/$bashfilename" 2>/dev/null
fi
done
local platform
platform=$(_platform)
# inject the bashfiles
if ! grep -E "$HOME/.bashrc" -e "(\\.|source)\\s+('|\")?($HOME|\\\$HOME)/.bashlib" >/dev/null; then
cat <<-BASH >> "$HOME/.bashrc"
if [[ -f "\$HOME/.bashrc_$platform" ]]; then
. "\$HOME/.bashrc_$platform"
fi
if [[ -f "\$HOME/.bashlib" ]]; then
. "\$HOME/.bashlib"
fi
BASH
fi
# warn about problematic history declarations in .bashrc, /etc/bash.bashrc, etc
for bashfile in "$HOME/.bash_profile" "$HOME/.profile" "$HOME/.bashrc" "/etc/bash.bashrc" "/etc/bashrc"; do
if [[ -f "$bashfile" ]]; then
if grep -e '^[^#]*HISTSIZE=[0-9]' "$bashfile"; then
echo "WARNING: $bashfile sets HISTSIZE. This is known to truncate history files even though we set it to unlimited"
fi
fi
done
# Setup binary links
mkdir -p "$HOME/bin"
remove_broken_symlinks "$HOME/bin"
for binary in "$ROOTDIR"/bin/*; do
ln -s -n "$binary" "$HOME/bin/$(basename "$binary")" 2>/dev/null
done
for ssh_binary in "$ROOTDIR"/ssh/*.bash; do
ln -s -n "$ssh_binary" "$HOME/bin/$(basename "${ssh_binary%%.bash}")" 2>/dev/null
done
# other files to symlink
for otherfile in .agignore .tmux.conf .tmux_profile .vim .irbrc .psqlrc .lessfilter .inputrc; do
ln -s -n "$ROOTDIR/$otherfile" "$HOME/$otherfile" 2>/dev/null
done
# ipython config installation
if type -t ipython >/dev/null; then
do_ipython_install
fi
# Migrate over some stuff to XDG style
[[ -z "$XDG_CONFIG_HOME" ]] && export XDG_CONFIG_HOME="$HOME/.config"
# git: To be removed no earlier than 20190601
mkdir -p "$XDG_CONFIG_HOME/git"
[[ -e "$HOME/.gitconfig" ]] && mv "$HOME/.gitconfig" "$XDG_CONFIG_HOME/git/config"
# shellcheck disable=SC2088
[[ ! -e "$HOME/.gitignore_global" && "$(git config --global core.excludesfile)" == "~/.gitignore_global" ]] && git config --global --unset core.excludesfile
# symlink XDG configs
remove_broken_symlinks "$XDG_CONFIG_HOME"
for config_entry in "$ROOTDIR/.config"/*; do
if [[ -d "$config_entry" ]]; then
mkdir -p "$XDG_CONFIG_HOME/$(basename "$config_entry")"
remove_broken_symlinks "$XDG_CONFIG_HOME/$(basename "$config_entry")"
for config_file in "$config_entry"/*; do
ln -s -n "$config_file" "$XDG_CONFIG_HOME/$(basename "$config_entry")/$(basename "$config_file")" 2>/dev/null
done
elif [[ -f "$config_entry" ]]; then
ln -s -n "$config_entry" "$XDG_CONFIG_HOME/$(basename "$config_entry")" 2>/dev/null
fi
done
ln -s -n "$XDG_CONFIG_HOME" "$ROOTDIR/.config-real" 2>/dev/null # convenience symlink
# copy over templates
[[ ! -e "$HOME/.bash_features" ]] && cp "$ROOTDIR/templates/.bash_features" "$HOME/.bash_features"
ln -s -n "$HOME/.bash_features" "$ROOTDIR/.bash_features" 2>/dev/null # convenience symlink
for config_folder in "$ROOTDIR/templates/.config"/*; do
mkdir -p "$XDG_CONFIG_HOME/$(basename "$config_folder")"
for config_file in "$config_folder"/*; do
if [[ ! -e "$XDG_CONFIG_HOME/$(basename "$config_folder")/$(basename "$config_file")" ]]; then
cp "$config_file" "$XDG_CONFIG_HOME/$(basename "$config_folder")/$(basename "$config_file")"
fi
done
done
# symlink the local profile files into the repo for convenience
[[ -f "$HOME/.profile" ]] && ln -s -n "$HOME/.profile" "$ROOTDIR/.profile" 2>/dev/null
[[ -f "$HOME/.bash_profile" ]] && ln -s -n "$HOME/.bash_profile" "$ROOTDIR/.bash_profile" 2>/dev/null
[[ -f "$HOME/.bashrc" ]] && ln -s -n "$HOME/.bashrc" "$ROOTDIR/.bashrc" 2>/dev/null
[[ -f "$HOME/.bash_features" ]] && ln -s -n "$HOME/.bash_features" "$ROOTDIR/.bash_features" 2>/dev/null
return 0
}
do_install "$@"