Browse code

git-sync.sh - v2.0 - Several functions added (see version section)

Fred authored on28/02/2015 23:30:41
Showing1 changed files
... ...
@@ -1,9 +1,9 @@
1 1
 #!/bin/bash
2 2
 # Title: git-sync.sh
3
-# Version: 1.3
3
+# Version: 2.0
4 4
 # Author: Frédéric CHEVALIER <fcheval@txbiomedgenetics.org>
5 5
 # Created in: 2014-09-03
6
-# Modified in: 2014-12-04
6
+# Modified in: 2015-02-27
7 7
 # Licence: GPL v3
8 8
 
9 9
 
... ...
@@ -20,6 +20,7 @@ aim="Synchronize files from git server (master branch) on this terminal."
20 20
 # Versions #
21 21
 #==========#
22 22
 
23
+# v2.0 - 2015-02-27: error function improved / info function added / file installed and directory created if not already present / modification of file permission can be specified in the file list and apply to the file
23 24
 # v1.3 - 2014-12-04: warning and error functions improved
24 25
 # v1.2 - 2014-09-24: temp file bug corrected / check if local file exist
25 26
 # v1.1 - 2014-09-14: test_dep bug corrected
... ...
@@ -50,10 +51,17 @@ function usage {
50 51
 Aim: $aim
51 52
 
52 53
 Options:
53
-    -l, --list      File containing remote path and local path of a file to synchronize.
54
-                    The list file must have a unique path pair per line and each path must
55
-                    be semi-colon separated (path1 ; path2). The remote path must be http path.
56
-                    Comments beginning by # are allowed anywhere.
54
+    -l, --list      Text file containing both remote and local path of
55
+                    a given file for synchronization.
56
+                        - The list file must have a unique path pair
57
+                            per line and each path must be semi-colon
58
+                            separated (remote_path ; local_path).
59
+                            The \e[33mremote path\e[0m must be \e[33mhttp\e[0m a path.
60
+                        - Modification of file permission can be specified
61
+                            in a third column. The syntax is the chmod
62
+                            syntax (ie, remote_path ; local_path ; uog+rwx).
63
+                        - Comments beginning by # are allowed anywhere
64
+                            in the list file.
57 65
 
58 66
     -d, --dry-run   Run the synchronization but print a message instead of updating the files. 
59 67
 
... ...
@@ -67,11 +75,11 @@ function error {
67 75
     if [[ -t 1 ]]
68 76
     then
69 77
         echo -e "\e[31mError:\e[00m $1"
70
-        exit $2
71 78
     else
72 79
         echo -e "Error: $1"
73
-        exit $2
74 80
     fi
81
+    
82
+    exit $2
75 83
 }
76 84
 
77 85
 
... ...
@@ -86,6 +94,17 @@ function warning {
86 94
 }
87 95
 
88 96
 
97
+# Info message
98
+function info {
99
+    if [[ -t 1 ]]
100
+    then
101
+        echo -e "\e[32mInfo:\e[00m $1"
102
+    else
103
+        echo -e "Info: $1"
104
+    fi
105
+}
106
+
107
+
89 108
 
90 109
 #==============#
91 110
 # Dependencies #
... ...
@@ -128,7 +147,7 @@ tmp_list="/tmp/$(basename $list)-$mydate"
128 147
 #============#
129 148
 
130 149
 # Check file list
131
-if [[ $(awk -F ";" "END {print NF}" "$list") != 2 ]]
150
+if [[ $(awk -F ";" "END {print NF}" "$list") -gt 3 ]]
132 151
 then
133 152
     error "The list file "$list" is malformated. Each path pair must be on unique line and each path must be semi-colon (';') separated." 1
134 153
 fi
... ...
@@ -136,6 +155,7 @@ fi
136 155
 
137 156
 # Remove comments from list file
138 157
 sed "/^\s*#/d;s/\s*#.*$//" "$list" > "$tmp_list"
158
+sed -i "/^$/d" "$tmp_list"
139 159
 
140 160
 
141 161
 # Synchronization
... ...
@@ -145,17 +165,22 @@ do
145 165
     # Get paths
146 166
     mysrc=$(echo "$line" | cut -d ";" -f 1 | sed -e "s/^ *//" -e "s/ *$//")
147 167
     mydst=$(echo "$line" | cut -d ";" -f 2 | sed -e "s/^ *//" -e "s/ *$//")
168
+    mymod=$(echo "$line" | cut -d ";" -f 3 | sed -e "s/^ *//" -e "s/ *$//")
148 169
 
149 170
     mydst_tmp="/tmp/${mydst##*/}"
150 171
 
151
-    # Check if destination file exists
152
-    if [[ ! -e "$mydst" ]]
172
+
173
+    # Check if mydst variable is empty or contain directory
174
+    if [[ -z "$mydst" ]]
153 175
     then
154
-        warning "${mydst##*/}: Local file does not exist. Skipping..."
176
+        warning "The destination name is absent. Skipping..."
155 177
         continue
178
+    elif [[ $(echo "$mydst" | grep "/") && ! -d "${mydst%/*}" ]]
179
+    then
180
+        info "${mydst##*/}: ${mydst%/*} directory created."
181
+        mkdir "${mydst%/*}"
156 182
     fi
157 183
 
158
-
159 184
     # Check if remote path is http
160 185
     if [[ ! $(echo "$mysrc" | grep -i "^http") ]]
161 186
     then
... ...
@@ -171,6 +196,16 @@ do
171 196
     then
172 197
         warning "${mydst##*/}: An error occured during the downloading. Skipping..."
173 198
         continue
199
+    # Check if destination file exists
200
+    elif [[ ! -e "$mydst" ]]
201
+    then
202
+        if [[ $dry == 1 ]]
203
+        then
204
+            info "${mydst##*/}: file available but not installed."
205
+        else
206
+            mv "$mydst_tmp" "$mydst"
207
+            info "${mydst##*/} installed."
208
+        fi
174 209
     else
175 210
         # Compare downloaded file and destination file (update file if need)
176 211
         mydst_tmp_md=$(md5sum "$mydst_tmp" | cut -d " " -f 1)
... ...
@@ -179,15 +214,25 @@ do
179 214
         then
180 215
             if [[ $dry == 1 ]]
181 216
             then
182
-                warning "${mydst##*/}: A new version is available."
217
+                info "${mydst##*/}: new version available."
183 218
             else
184 219
                 mv "$mydst_tmp" "$mydst"
220
+                info "${mydst##*/} updated."
185 221
             fi
186 222
         else
187 223
             rm "$mydst_tmp"
188 224
         fi
189 225
     fi
190 226
 
227
+    if [[ -z $dry && -n "$mymod" && -n $(echo "$mymod" | sed -n "/[u,o,g,][+,-][r,w,x,s,t,S,T]/p") ]]
228
+    then
229
+        chmod "$mymod" "$mydst"
230
+    elif [[ -z $dry && -n "$mymod" && -z $(echo "$mymod" | sed -n "/[u,o,g,][+,-][r,w,x,s,t,S,T]/p") ]]
231
+    then
232
+        warning "${mydst##*/}: Impossible to change permission, \"$mymod\" does not follow the syntax of the chmod command (see help)."
233
+        continue
234
+    fi
235
+
191 236
 done < "$tmp_list"
192 237
 
193 238