-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell-startup
212 lines (163 loc) · 6.03 KB
/
shell-startup
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/bin/bash
# Debug tty login (ssh user@server):
# ssh -t localhost "PS4='[\$BASH_SOURCE[0]:\$LINENO]: ' bash -xl" |& tee login.log
# Debug no tty login (ssh user@server somecommand)
# ssh localhost "PS4='[\$BASH_SOURCE[0]:\$LINENO]: ' bash -xl" |& tee login.log
#-----------------------------------------------------------------------------
# Use these functions to clean up the environment at end of startup.
#
# Add any various variables or functions that you want to unset to the
# appropriate array.
#
# Usage:
# unsetvars 'varname'
# unsetfuncs 'funcname'
declare -a _unset_vars _unset_funcs
# shellcheck disable=2317
unsetvars() { _unset_vars+=("$@"); }
unsetfuncs() { _unset_funcs+=("$@"); }
#-----------------------------------------------------------------------------
# XXX: FIXME
# Setup for git bash on windows.
# o If MSYS is not set, then link is just a copy of stuff.
# o If MSYS is winsymlinks, it creates a Windows shortcut.
# o If MSYS is winsymlinks:nativestrict, it creates a more real type of symlink
#export MSYS="winsymlinks:nativestrict"
##############################################################################
# Base Global variable and path
#-----------------------------------------------------------------------------
# Assume that this file is in the dotfiles directory, then check if we are
# linnked to the dotfiles directory. If we are, then use the real directory
# as the dotfiles directory.
declare -x DOTFILES="$HOME"
declare -x PROJECTS_DIR="$HOME/projects"
[[ -L ${BASH_SOURCE[0]} ]] && {
DOTFILES=$(dirname "$(readlink -nf "${BASH_SOURCE[0]}")")
PROJECTS_DIR="${DOTFILES%/*}"
}
# Export initial path settings
export PATH="$DOTFILES/bin:$HOME/.local/bin:$PATH"
#-----------------------------------------------------------------------------
# XDG Variables
export XDG_CONFIG_HOME="$DOTFILES/config"
export XDG_CACHE_HOME="$HOME/.cache"
export XDG_DATA_HOME="$HOME/.local/share"
export XDG_STATE_HOME="$HOME/.local/state"
mkdir -p "$XDG_CACHE_HOME"
mkdir -p "$XDG_STATE_HOME"
export INPUTRC="$XDG_CONFIG_HOME/readline/inputrc"
export EDITOR=vim
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
##############################################################################
# Functions
#-----------------------------------------------------------------------------
# Set a default debug and, if set, check if debug is available and source it.
debug() { true; }
[[ -n $DEBUG ]] \
&& [[ -f "$DOTFILES/bin/debug" ]] \
&& source "$DOTFILES/bin/debug"
unsetfuncs 'debug'
#-----------------------------------------------------------------------------
# Add a path to the PATH variable. This is a function so that we can
# easily add paths to the PATH variable.
#
# Usage:
# addpath PATH /path/to/add
# addpath LD_LIBRARY_PATH /path/to/add
# addpath /path/to/add
#
# The last one defaults to PATH
# XXX: Add options for first and last place
addpath() {
local target="PATH"
if [[ $1 == "PATH" ]] || [[ $1 == "LD_LIBRARY_PATH" ]]; then
target="$1"
shift
fi
for p in "$@"; do
[[ -z $p ]] && continue
debug "adding $p to $target"
printf -v "$target" "%s:%s" "${!target}" "$p"
done
}
unsetfuncs 'addpath'
#-----------------------------------------------------------------------------
# XXX: Does this actually work?
run_hook() {
local hook="$dfdir/$1"
debug "running hook: $hook"
[[ -r $hook ]] || {
debug "hook not readable: $hook"
return 1
}
source "$hook" || {
debug "hook failed: $hook"
return 1
}
return 0
}
unsetfuncs 'run_hook'
#-----------------------------------------------------------------------------
# Add custom binary directories to the system's PATH variable, allowing users
# to execute their own scripts and commands from the command line. It reads the
# directory paths from a configuration file and appends them to the PATH
# variable in the specified order.
#
# This is not cumulative, either the defaults or the custom bin dirs are used.
# XXX: Add documentation for deault bin dirs.
set_bin_dirs() {
bin_dirs_file="$DOTFILES/bin-dirs-defaults"
[[ -r "$HOME/.bin-dirs" ]] && bin_dirs_file="$HOME/.bin-dirs"
readarray -t bin_dirs < <(grep -v '^[[:space:]]*#' "$bin_dirs_file" | envsubst)
addpath "${bin_dirs[@]}"
}
#-----------------------------------------------------------------------------
load_files() {
declare -a load_dirs
load_dirs+=("$XDG_CONFIG_HOME/shell-startup")
load_dirs+=("$HOME/.shell_startup.d")
# Run each directory instead of doing a find on all directories at once
# because we want these files loaded in this particular order.
for load_dir in "${load_dirs[@]}"; do
[[ -d $load_dir ]] || continue
readarray -t load_files < <(/usr/bin/find "$load_dir" -type f -not -iname '*_inactive' | /usr/bin/sort)
for f in "${load_files[@]}"; do
# shellcheck disable=SC1090
[[ -r $f ]] && source "$f"
done
done
}
##############################################################################
# Setup environment
# XXX: WTF am I trying doing here?
path_to_generate_exports_script="/path/to/generate_exports.sh"
path_to_exports_script="/path/to/exports.sh"
# Generate export commands if needed
if [[ -x $path_to_generate_exports_script ]]; then
# shellcheck disable=SC1090
source "$path_to_generate_exports_script"
fi
# Source the export commands
if [[ -r $path_to_exports_script ]]; then
# shellcheck disable=SC1090
source "$path_to_exports_script"
fi
[[ -r "$DOTFILES/app_env_vars" ]] && source "$DOTFILES/app_env_vars"
# shellcheck disable=SC1091
[[ -r "$DOTFILES/shell_startup.d/pre-setup" ]] \
&& source "$DOTFILES/shell_startup.d/pre-setup"
#-----------------------------------------------------------------------------
# Check if various dotfiles are linked properly
[[ -x "$(command -v check-dotfiles 2> /dev/null)" ]] && check-dotfiles
set_bin_dirs
load_files
set_bin_dirs
load_files
##############################################################################
# Cleanup
unset -f "${_unset_funcs[@]}"
unset "${_unset_vars[@]}"
unset -f unsetvars unsetfuncs
unset _unset_vars _unset_funcs