-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.sh
executable file
·163 lines (140 loc) · 4.15 KB
/
update.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
#!/bin/bash
# Update script for handling dotfiles
# Script mainly taken from https://github.com/durdn/cfg
debug=false;
version="0.1";
home=$HOME;
dotfilesname=".dotfiles";
bkpname=".dotfiles_backup";
gitrepo="git@github.com:TheMiles/dotfiles.git";
gitrepo_ro="https://github.com/TheMiles/dotfiles.git";
gitrepo_zip="https://github.com/TheMiles/dotfiles/archive/master.zip"
ignored="update.sh|.git$|.gitmodule|.gitignore|README.md|ohmyzsh_plugins";
#----debug setup----
#home=$1
#gitrepo=$2;
#------------------
dotfiles_folder=$home/$dotfilesname;
backup_folder=$home/$bkpname;
function md5prog {
if [ $(uname) == "Darwin" ]; then
md5 -q $1
fi
if [ $(uname) == "Linux" ]; then
md5sum $1 | awk {'print $1'}
fi
}
function update_submodules {
return;
if [ $debug == true ];
then
cd $dotfiles_folder
echo "|- initializing submodules [fake]"
echo "|- updating submodules [fake]"
else
echo "|* initializing submodules"
cd $dotfiles_folder && git submodule -q init
echo "|* updating submodules"
cd $dotfiles_folder && git submodule update
fi
}
function link_assets {
for asset in $assets ;
do
if [ ! -e $home/$asset ];
then
#asset does not exist, can just copy it
echo "N [new] $home/$asset";
if [ $debug == false ];
then ln -s $dotfiles_folder/$asset $home/$asset;
else echo ln -s $dotfiles_folder/$asset $home/$asset;
fi
else
#asset is there already
if [ -d $home/$asset ];
then
if [ -h $home/$asset ];
then echo "Id[ignore dir] $home/$asset";
else
echo "Cd[conflict dir] $home/$asset";
mv $home/$asset $backup_folder/$asset;
ln -s $dotfiles_folder/$asset $home/$asset;
fi
else
ha=$(md5prog $home/$asset);
ca=$(md5prog $dotfiles_folder/$asset);
if [ $ha == $ca ];
#asset is exactly the same
then
if [ -h $home/$asset ];
#asset is exactly the same and as link, all good
then echo "I [ignore] $home/$asset";
else
#asset must be relinked
echo "L [re-link] $home/$asset";
if [ $debug == false ];
then
mv $home/$asset $backup_folder/$asset;
ln -s $dotfiles_folder/$asset $home/$asset;
else
echo mv $home/$asset $backup_folder/$asset;
echo ln -s $dotfiles_folder/$asset $home/$asset;
fi
fi
else
#asset exist but is different, must back it up
echo "C [conflict] $home/$asset";
if [ $debug == false ];
then
mv $home/$asset $backup_folder/$asset;
ln -s $dotfiles_folder/$asset $home/$asset;
else
echo mv $home/$asset $backup_folder/$asset;
echo ln -s $dotfiles_folder/$asset $home/$asset;
fi
fi
fi
fi
done
}
echo "|* dotfiles version" $version
echo "|* debug is" $debug
echo "|* home is" $home
echo "|* backup folder is" $backup_folder
echo "|* dotfiles folder is" $dotfiles_folder
if [ ! -e $backup_folder ];
then mkdir -p $backup_folder;
fi
#clone config folder if not present, update if present
if [ ! -e $dotfiles_folder ];
then
if [[ -z $(command -v git) ]]
then
#git is not available, juzt unpack the zip file
echo "|* git not available downloading zip file..."
curl -LsO $gitrepo_zip
unzip master.zip
mv dotfiles-master $dotfiles_folder
rm master.zip
else
#git is available, clone from repo
echo "|* git clone from repo $gitrepo"
git clone --recursive $gitrepo $dotfiles_folder;
if [ ! -e $dotfiles_folder ];
then
echo "!!! ssh key not installed on github for this box, cloning read only repo"
git clone --recursive $gitrepo_ro $dotfiles_folder
else
update_submodules
fi
fi
else
echo "|* dotfiles already cloned to $dotfiles_folder"
echo "|* pulling origin master"
cd $dotfiles_folder && git pull origin master
update_submodules
fi
assets=$(ls -A1 $dotfiles_folder | egrep -v $ignored | xargs);
echo "|* tracking assets: [ $assets ] "
echo "|* linking assets in $home"
link_assets