-
Notifications
You must be signed in to change notification settings - Fork 1
/
sshrc
executable file
·95 lines (81 loc) · 2.41 KB
/
sshrc
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
#!/bin/bash
if [ -z "$AMIGRAVE" ]; then
echo "start.sh has not been executed or sourced as .profile, .bashrc or .zshrc"
echo "You can symlink start.sh to one of those files using 'start.sh -i'"
return
fi
update=0
verbose=0
start_opts=""
on_logout=""
dest=${SSHRC_LOCATION:-$(basename $AMIGRAVE)}
function usage {
if [[ $1 != '' ]]; then
echo $1
echo
fi
echo "Usage: `basename $0` [-b 'force bash'] [-u 'update config'] [-d 'delete config on logout'] [-v --verbose]" 1>&2; exit 1;
exit
}
while getopts "budv" o; do
case "${o}" in
b) start_opts="$start_opts -b" ;;
u) update=1 ;;
v) verbose=1 ;;
d) on_logout="rm -rf $dest;" ;;
*) usage ;;
esac
done
shift $((OPTIND-1))
[[ $verbose == 1 ]] && set -x
sshargs="$@"
if [[ $sshargs == '' ]]; then
usage "ERROR: Please specify a host"
fi
function upload_config() {
cd $AMIGRAVE
tmpfile=$(mktemp --suffix='-agr-config.tar')
branch=master
git diff --exit-code --quiet
if [[ $1 = 'update' && $? != 0 ]]; then
# make a stash in order to get unstaged changes
# can be garbage collected with git gc --prune=now if needed
branch=$(git stash create)
fi
git archive -o $tmpfile $branch
# take vim config in my luggages
tar -u --file=$tmpfile config/.xdg/vim \
--exclude='config/.xdg/vim/bundle/vim-xpath/*' \
--exclude='config/.xdg/vim/bundle/vim-notes/*' \
--exclude='config/.xdg/vim/bundle/syntastic/*' \
--exclude='config/.xdg/vim/bundle/jedi-vim/*' \
--exclude='config/.xdg/vim/bundle/tern_for_vim/*' \
--exclude='config/.xdg/vim/bundle/vim-color-picker-osx/*' \
--exclude='config/.xdg/vim/bundle/*/doc/*' \
--exclude='config/.xdg/vim/bundle/*/*.gif' \
--exclude='.git'
scp -C $tmpfile "$sshargs":.agr.tgz
rm $tmpfile
ssh "$sshargs" "mkdir -p $dest; cd $dest; tar -xf ~/.agr.tgz; rm ~/.agr.tgz"
}
function connect() {
term_title "SSH <$sshargs>"
ssh "$sshargs" -t "$dest/start.sh $start_opts; $on_logout"
}
if [ $update = 1 ]; then
upload_config update
fi
connect
if [ $? -eq 127 ]; then
printf '%s ' "Upload configuration on remote server in '$dest' ? [y/N]"
read answer
if [[ $answer =~ [yY](es)* ]]; then
upload_config
connect
term_title
else
echo "User aborted."
term_title
exit 1
fi
fi