-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·208 lines (161 loc) · 6.63 KB
/
install.sh
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
#!/bin/bash
# set script to exit if failures occur
set -eEuo pipefail
shopt -s inherit_errexit
dotfiles_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
dotfiles_log=${dotfiles_dir}/dotfiles_install.log
# log to stdout and/or file based on log level
# usage:
# logger info "message"
# logger debug "message"
# echo "message" | logger info
# echo "message" | logger debug
# logger debug # dont do this, it will hang
function logger() {
log_levels=("error" "warn" "info" "debug" "trace")
log_level=info
# set log-level, if a valid level was provided as first arg
if [[ "$#" -gt 0 && " ${log_levels[@]} " =~ " $1 " ]]; then
log_level=$1
fi
# if multiple args were provided, and first is a valid log level
if [[ "$#" -gt 1 && " ${log_levels[@]} " =~ " $1 " ]]; then
log_lines="${@:2}" # then take log lines from args 2-n
# if at least 1 arg was provided and it is not a valid log level
elif [[ "$#" -gt 0 && ! " ${log_levels[@]} " =~ " $1 " ]]; then
log_lines="$@" # then take log lines from all args
# in all other cases
else
log_lines=$(cat) # take log lines from stdin
fi
# read all log_lines and output to destination according to log level
while IFS= read -r line; do
# info level also goes to stdout
if [[ "${log_level}" = "info" ]]; then
printf '%s\n' "$line"
fi
# all levels go to log file, prefixed with timestamp
printf '%s %-5s %s\n' "$(date "+%Y-%m-%dT%H:%M:%S%z")" "${log_level^^}" "$line" >> ${dotfiles_log}
done <<< $(echo "${log_lines}")
}
function install_vim_plug() {
if [[ ! -e "$HOME/.vim/autoload/plug.vim" ]]; then
logger info "Installing vim-plug"
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim | logger debug
logger info "NOTE: Run \":PlugInstall\" in vim to install plugins"
fi
}
function install_zsh_syntax_highlighting() {
if [[ ! -d "$HOME/.zsh-syntax-highlighting" ]]; then
logger info "Installing zsh-syntax-highlighting"
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $HOME/.zsh-syntax-highlighting | logger debug
fi
}
# Set default shell to zsh
function set_default_shell_zsh() {
if [[ -n "$(which zsh)" ]] && [[ ! "$SHELL" = "/bin/zsh" ]]; then
logger info "Changing default shell from $SHELL to /bin/zsh (needs password)"
chsh -s /bin/zsh | logger info
#else
# stdout_and_log "Default shell already set to /bin/zsh"
fi
}
function install_linux_pkgs() {
logger debug ""
logger info "Installing Packages (might prompt password for sudo)"
logger debug "==========================="
sudo -v || { echo >&2 "No sudo privilege. Skipping package installation"; exit 1; }
logger info "Updating apt. (this may take a while if not done recently)"
sudo apt update 2>&1 | logger debug
logger debug ""
logger debug "(APT)"
pkg_list_apt=$(cat ${dotfiles_dir}/linux/packages_apt.txt | grep -Ev '^\s*$|^\s*\#' | tr '\n' ' ') # get package list on single line space-delimted, removing commented lines
echo "apt: ${pkg_list_apt}" | logger info
sudo apt install -y ${pkg_list_apt} 2>&1 | logger debug
logger debug ""
logger debug "(PIP)"
pkg_list_pip=$(cat ${dotfiles_dir}/linux/packages_pip.txt | grep -Ev '^\s*$|^\s*\#' | tr '\n' ' ') # get package list on single line space-delimted, removing commented lines
echo "pip: ${pkg_list_pip}" | logger info
python3 -m pip install ${pkg_list_pip} 2>&1 | logger debug
}
# copy_files()
# ============
# recursively copy files $1 to $2 preseving any directory structure
#
function copy_files() {
copy_from=$1
copy_to=$2
echo "copying from $copy_from to $copy_to" | logger debug
if [[ -d "${copy_from}" ]]; then
logger info "Copying files"
cp -vR "${copy_from}/" "${copy_to}/" | logger info
fi
#if [[ -d "${script_dir}/copy" ]]; then
# echo "Copying files"
# cp -vR ${script_dir}/copy/ $HOME/
#fi
}
function link_files() {
logger debug ""
logger info "[Re-]creating symbolic links"
logger debug "============================"
ln -snfv $HOME/dotfiles/zsh/zshrc $HOME/.zshrc | logger info
ln -snfv $HOME/dotfiles/zsh/functions $HOME/.zsh_func | logger info
ln -snfv $HOME/dotfiles/vim/vimrc $HOME/.vimrc | logger info
}
logger debug ""
logger info "Installing dotfiles"
logger debug "==================="
logger info "Log: ${dotfiles_log}"
# Linux (incl. WSL on Windows)
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
logger info "Platform: Linux"
install_linux_pkgs
set_default_shell_zsh
install_zsh_syntax_highlighting
install_vim_plug
link_files
# && [ -e "/etc/lsb-release" ]; then os='ubuntu'
# Mac
elif [[ "$OSTYPE" == "darwin"* ]]; then
echo "Installing Dotfiles for Mac"
echo "==========================="
${dotfiles_dir}/mac/install-mac-pkgs.sh # install mac packages (w/ brew).
${dotfiles_dir}/mac/install-mac-dock.sh # config dock settings
${dotfiles_dir}/mac/install-mac-prefs.sh # confic mac defaults
#set_default_shell_zsh # default for mac these days
install_zsh_syntax_highlighting
install_vim_plug
link_files
copy_files ${dotfiles_dir}/mac/copy $HOME
# POSIX compatibility layer and Linux environment emulation for Windows
#elif [[ "$OSTYPE" == "cygwin" ]]; then
# Lightweight shell and GNU utilities compiled for Windows (part of MinGW)
#elif [[ "$OSTYPE" == "msys" ]]; then
# I'm not sure this can happen.
#elif [[ "$OSTYPE" == "win32" ]]; then
# TODO
# windows terminal settings
# /mnt/c/Users/kbouck/AppData/Local/Packages/Microsoft.WindowsTerminal_8wekyb3d8bbwe/LocalState/settings.json
# cp ./windows-terminal-settings.json "$terminalDir/LocalState/settings.json"
# ...
#elif [[ "$OSTYPE" == "freebsd"* ]]; then
# Unknown
else
echo "Unrecognized OS: ${OSTYPE}"
exit 1
fi
logger debug ""
logger info "Done"
# .bash_profile
# =============
#bash_profile="${bash_profile} ${script_dir}/bash_profile/path"
#bash_profile="${bash_profile} ${script_dir}/bash_profile/aliases"
#bash_profile="${bash_profile} ${script_dir}/bash_profile/prompt"
#bash_profile="${bash_profile} ${script_dir}/bash_profile/python_miniconda"
#bash_profile="${bash_profile} ${script_dir}/bash_profile/python_venv"
#bash_profile="${bash_profile} ${script_dir}/bash_profile/groovy"
#cat ${bash_profile} > /tmp/bash_profile.txt
# .vimrc
# ======