-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-environment.sh
187 lines (167 loc) · 6.17 KB
/
setup-environment.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
#!/bin/sh
# export arguments to script
export REMOTE_USER=$1
export LAUNCH_SHELL=${2:=$SHELL}
if [ "$3" = "--reset" ]; then RESET=1; fi
# set locally-used variables which get overridden by environment
REPO=${REPO:=https://github.com/squarebracket/dotfiles.git}
# export important environment variables
export LOOPBACK_PORT=$LOOPBACK_PORT
export POWERLINE_FONT=$POWERLINE_FONT
export ENVIRONMENT=$ENVIRONMENT
export LOCAL_DISPLAY=$LOCAL_DISPLAY
export HOST_FINGERPRINT="$HOST_FINGERPRINT"
# make sure we're in ~
cd ~
# set and export $DOTFILES
if [ "$REMOTE_USER" = "LOCAL" ]; then
export DOTFILES="dotfiles"
TERMINATOR=''
else
export DOTFILES=".dotfiles-$REMOTE_USER"
TERMINATOR="-$REMOTE_USER"
fi
# detect installer
yum --version > /dev/null 2>&1 && [ $? = 0 ] && export INSTALLER=yum
apt-get --version > /dev/null 2>&1 && [ $? = 0 ] && export INSTALLER=apt-get
# Are we root?
if [ "$UID" != "0" ]; then
# if not, set $SUDO
export SUDO="sudo"
fi
# Tiny little function to install stuff if need it
# It's very error-prone considering some binaries (like pip) have
# a package name that differs from the binary name
_install_if_needed() {
which $1 > /dev/null
if [ $? = 1 ]; then
$SUDO $INSTALLER install -y $1
fi
}
# If we've been asked to reset the state, do a clean-up
if [ -n "$RESET" ]; then
rm -rf $HOME/$DOTFILES/
fi
# Install git if it's not present
_install_if_needed git
# Do the dotfiles magic if the folder doesn't exist
if [ ! -d "$DOTFILES" ]; then
echo "Getting the dotfiles"
git clone --recursive $REPO $DOTFILES
echo "Making links"
ln -fs $DOTFILES/vim $HOME/.vim$TERMINATOR
ln -fs $DOTFILES/tmux/tmux.conf $HOME/.tmux.conf$TERMINATOR
ln -fs $DOTFILES/screen/screenrc $HOME/.screenrc$TERMINATOR
ln -fs $DOTFILES/dircolors $HOME/.dircolors$TERMINATOR
ln -fs $DOTFILES/bash/bashrc $HOME/.bashrc$TERMINATOR
ln -fs $DOTFILES/oh-my-zsh $HOME/.oh-my-zsh
ln -fs $DOTFILES/zsh/zshrc $HOME/.zshrc$TERMINATOR
ln -fs $DOTFILES/weechat $HOME/.weechat$TERMINATOR
# TODO: Change this to links?
# Copy over custom termcaps
[ ! -d $HOME/.terminfo ] && mkdir $HOME/.terminfo
cd $HOME/$DOTFILES/terminfo
for file in $(ls */*); do
mkdir -p $HOME/.terminfo/${file%*/*}
cp $file $HOME/.terminfo/$file
done
cd ~
# Vim is fucking dumb and can't handle using alternate .vimrc files,
# so we have to literally append some bullshit to ~/.vimrc just to make
# it work
VIMHACK="if exists('\$DOTFILES')
source $HOME/$DOTFILES/vim/vimrc
endif"
echo "Managing vimrc hack"
if [ ! -f $HOME/.vimrc ]; then
echo "$VIMHACK" > $HOME/.vimrc
elif [ -f $HOME/.vimrc ]; then
grep "source $HOME/$DOTFILES/vim/vimrc" < ~/.vimrc > /dev/null || echo "$VIMHACK" >> ~/.vimrc
fi
echo "Installing public key"
mkdir -p $HOME/.ssh
for file in `ls $HOME/$DOTFILES/keys/*`; do
cat $file >> $HOME/.ssh/authorized_keys
done
# Remote -> local copying requires the remote server having a public key
# So check if we have a public key, and if not, generate it
if [ ! -f $HOME/.ssh/id_rsa.pub ]; then
ssh-keygen -t rsa -b 4096 -f $HOME/.ssh/id_rsa -N ''
fi
# If you need to install software or do anything else before launching
# your fancy new environment, add it to $DOTFILES/required.sh
# Remember that $INSTALLER and $SUDO are exported above -- you should
# use those variables for installing software
if [ -f $DOTFILES/required.sh ]; then
echo "Performing required pre-environment actions"
bash $DOTFILES/required.sh
fi
# Likewise, if you need to `pip install` anything, add it to the usual
# requirements.txt file
if [ -f $DOTFILES/requirements.txt ]; then
echo "installing pip requirements..."
# Custom test/install for pip
pip --version > /dev/null
if [ $? != 0 ]; then $SUDO $INSTALLER install -y python-pip; fi
# Do the installing
$SUDO pip install -r $DOTFILES/requirements.txt
fi
# Update the repo(s) on every login
else
# Update the dotfiles and all submodules to the latest version
cd $DOTFILES
git pull
git submodule update --init
cd $HOME
fi
# If we're remote and we've got a $LOOPBACK_PORT...
if [ "$REMOTE_USER" != "LOCAL" -a -n "$LOOPBACK_PORT" ]; then
# ensure our current config's fingerprint is removed from known hosts
ssh-keygen -R "[localhost]:$LOOPBACK_PORT"
# insert the current config's fingerprint
echo "$HOST_FINGERPRINT" | sed "s/localhost/[localhost]:$LOOPBACK_PORT/g" >> ~/.ssh/known_hosts
# test to see if we have ssh keys set up for remote copying back to host...
ssh -q -o PasswordAuthentication=no -o StrictHostKeyChecking=no -p $LOOPBACK_PORT $REMOTE_USER@localhost -t 'echo "success!"'
EXIT_CODE=$?
if [ $EXIT_CODE != 0 ]; then
# ...and if we don't, exit with status 13
exit 13
fi
fi
# Set the title of the pane for embedded screen/tmux
if [ "$REMOTE_USER" != "LOCAL" ]; then
case "$LAUNCH_SHELL" in
*tmux*)
TMUX_VER=$(tmux -V)
printf '\033]2;%s\033\\' "$TMUX_VER"
;;
*)
printf '\033]2;%s\033\\' "$LAUNCH_SHELL"
;;
esac
fi
# Your local and remote shell may be different, but you probably still want
# a customized environment. So here we set up a custom variable if needed.
case "$SHELL" in
/bin/bash)
export CUSTOM_SHELL_OPTS="--rcfile $HOME/$DOTFILES/bash/bashrc"
;;
/bin/zsh)
export ZDOTDIR="$HOME/$DOTFILES/zsh/"
;;
esac
echo "shell is: $SHELL"
_install_if_needed $LAUNCH_SHELL
# Some environment variables need to be set before launching a
# terminal multiplexer, so we source them here.
if [ -f $HOME/$DOTFILES/${LAUNCH_SHELL#/bin/*}/pre-init ]; then
source $HOME/$DOTFILES/${LAUNCH_SHELL#/bin/*}/pre-init
fi
if [ "$LAUNCH_SHELL" = "screen" ]; then
screen -xRR -S $REMOTE_USER -c $HOME/.screenrc$TERMINATOR $SHELL $CUSTOM_SHELL_OPTS
elif [ "$LAUNCH_SHELL" = "tmux" ]; then
tmux has-session -t $REMOTE_USER && tmux attach -t $REMOTE_USER || tmux -f $HOME/.tmux.conf$TERMINATOR new-session -s $REMOTE_USER "$SHELL $CUSTOM_SHELL_OPTS"
else
$LAUNCH_SHELL
fi
exit $?