-
Notifications
You must be signed in to change notification settings - Fork 11
/
copyzshell.zsh
executable file
·94 lines (81 loc) · 2.74 KB
/
copyzshell.zsh
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
#!/bin/zsh
if [[ ! ARGC -eq 1 ]] then
echo "1 argument required, $# provided"
exit 1
elif [[ $1 = '-h' || $1 = '--help' ]] then
echo "HELP MSG"
exit 0
else
# exit on failure!
set -e
zmodload zsh/regex
#check that the ZSH path is not wierd
if [[ ! $ZSH -regex-match ^$HOME/(.+) ]] then
echo "Your ZSH folder "$ZSH" doesn't seem to be in your home folder. Quitting"
exit 1
fi
#TODO: run a git gc in oh-my-zsh directory
DATESTR=$(date "+%Y-%m-%d-%H:%M:%S")
ZSH_FOLDER=${ZSH#$HOME/} # could be in multiple subfolders relative to $HOME TODO TEST!
ZSH_BASE=${ZSH##*/}
ZSH_FOLDER_WITHOUT_BASE=./${ZSH_FOLDER%$ZSH_BASE}
echo $ZSH_FOLDER
echo $ZSH_FOLDER_WITHOUT_BASE
#mkdir -p
cd ~
# We want to move all files in a batch so we don't have to ask for passwords so much.
# move our files into a subdirectory of tmp so that we don't have to worry about
# permissions when overwriting old files on the remote machine.
# Otherwise, this can happen with .git objects when a previous run of this script
# failed for some reason.
echo Transfering $ZSH, .zshrc and .gitconfig.
TMP_DIR=/tmp/copyshell_$DATESTR
mkdir $TMP_DIR
cp -r $ZSH $TMP_DIR/oh-my-zsh
# echo cp -r $ZSH $TMP_DIR/oh-my-zsh
cp .zshrc $TMP_DIR
cp .gitconfig $TMP_DIR
# echo scp -r $TMP_DIR $1":"$TMP_DIR
scp -r $TMP_DIR $1":"$TMP_DIR
# echo "files should now be in "$TMP_DIR
# rc=$?
#DO NOT TRANSFER WIERD GIT FILES!
# if [[ ! $rc = 1 && ! $rc = 0 ]] ; then
# echo "failing because error " $rc
# exit $rc
# fi
echo 'File transfer complete. We will now setup the shell via ssh.'
remote_commands="
cd ~
# check for pre-existing zsh folder!
if [ -d $ZSH_FOLDER ]; then
echo $ZSH_FOLDER folder aldready exists, copying it to ${ZSH_FOLDER}_${DATESTR};
mv $ZSH_FOLDER ${ZSH_FOLDER}_${DATESTR};
fi
#move the new zsh folder into position
mkdir -p $ZSH_FOLDER_WITHOUT_BASE
mv ${TMP_DIR}/oh-my-zsh $ZSH_FOLDER
# check for pre-existing files
if [ -f .zshrc ]; then
mv .zshrc .zshrc_$DATESTR
echo 'An existing .zshrc was found. It was moved to .zshrc_$DATESTR'
fi
if [ -f .gitconfig ]; then
mv .gitconfig .gitconfig_$DATESTR
echo 'An existing .gitconfig was found. It was moved to .gitconfig_$DATESTR'
fi
# move the new files into position
mv ${TMP_DIR}/.zshrc .zshrc
mv ${TMP_DIR}/.gitconfig .gitconfig
#check that zsh is installed
command -v zsh1 >/dev/null 2>&1
if [ \$? -ne 0 ]; then
echo 'zsh does not appear to be installed. Your configuration is prepared, so install zsh and then change your shell using chsh -s /bin/zsh, and your configuration should become active.'
else
chsh -s /bin/zsh
echo Successfully copied your configuration and
fi"
# echo "$remote_commands"
echo "Starting remote session:"
ssh -t $1 "echo '$remote_commands' | sh"
fi