forked from durdn/cfg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·167 lines (157 loc) · 4.91 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
#!/bin/bash
debug=false;
version="0.2";
#fixing installation folder if user is root
if [ $(whoami) = "root" ];
then
home="/root";
else
home=$HOME;
fi
cfgname=".cfg";
bkpname="backup.cfg";
gitrepo="[email protected]:durdn/cfg.git";
gitrepo_ro="git://github.com/durdn/cfg.git";
ignored="install.py|install.pyc|install.sh|.git$|.gitmodule|.gitignore|README|bin";
#----debug setup----
#home=$1
#gitrepo=$2;
#------------------
cfg_folder=$home/$cfgname;
backup_folder=$home/$bkpname;
md5prog() {
if [ $(uname) = "Darwin" ]; then
md5 -q $1
fi
if [ $(uname) = "Linux" ]; then
md5sum $1 | awk {'print $1'}
fi
}
update_submodules() {
return;
if [ $debug == true ];
then
cd $cfg_folder
echo "|-> initializing submodules [fake]"
echo "|-> updating submodules [fake]"
else
echo "|-> initializing submodules"
cd $cfg_folder && git submodule -q init
echo "|-> updating submodules"
cd $cfg_folder && git submodule update
fi
}
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 $cfg_folder/$asset $home/$asset;
else echo ln -s $cfg_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 $cfg_folder/$asset $home/$asset;
fi
else
ha=$(md5prog $home/$asset);
ca=$(md5prog $cfg_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 $cfg_folder/$asset $home/$asset;
else
echo mv $home/$asset $backup_folder/$asset;
echo ln -s $cfg_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 $cfg_folder/$asset $home/$asset;
else
echo mv $home/$asset $backup_folder/$asset;
echo ln -s $cfg_folder/$asset $home/$asset;
fi
fi
fi
fi
done
}
echo "|* cfg version" $version
echo "|* debug is" $debug
echo "|* home is" $home
echo "|* backup folder is" $backup_folder
echo "|* cfg folder is" $cfg_folder
if [ ! -e $backup_folder ];
then mkdir -p $backup_folder;
fi
#clone config folder if not present, update if present
if [ ! -e $cfg_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 https://github.com/durdn/cfg/archive/master.tar.gz
tar zxvf master.tar.gz
mv cfg-master $home/.cfg
rm master.tar.gz
else
#git is available, clone from repo
echo "|-> git clone from repo $gitrepo"
git clone --recursive $gitrepo $cfg_folder;
if [ ! -e $cfg_folder ];
then
echo "!!! ssh key not installed on github for this box, cloning read only repo"
git clone --recursive $gitrepo_ro $cfg_folder
echo "|* changing remote origin to read/write repo: $gitrepo"
cd $cfg_folder && git config remote.origin.url $gitrepo
if [ -e $home/id_rsa.pub ];
then
echo "|* please copy your public key below to github or you won't be able to commit";
echo
cat $home/.ssh/id_rsa.pub
else
echo "|* please generate your public/private key pair with the command:"
echo
echo "ssh-keygen"
echo
echo "|* and copy the public key to github"
fi
else
update_submodules
fi
fi
else
echo "|-> cfg already cloned to $cfg_folder"
echo "|-> pulling origin master"
cd $cfg_folder && git pull origin master
update_submodules
fi
assets=$(ls -A1 $cfg_folder | egrep -v $ignored | xargs);
echo "|* tracking assets: [ $assets ] "
echo "|* linking assets in $home"
link_assets