Browse code

git-sync.sh - v0.0 - Initial commit

Fred authored on03/09/2014 23:33:46
Showing1 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,143 @@
1
+#!/bin/bash
2
+# Title: git-sync.sh
3
+# Version: 0.0
4
+# Author: Frédéric CHEVALIER <fcheval@txbiomedgenetics.org>
5
+# Created in: 2014-09-03
6
+# Modified in:
7
+# Licence : GPL v3
8
+
9
+
10
+
11
+#======#
12
+# Aims #
13
+#======#
14
+
15
+aim="Synchronize files from git server (master branch) on this terminal."
16
+
17
+
18
+
19
+#==========#
20
+# Versions #
21
+#==========#
22
+
23
+# v0.0 - 2014-09-03: creation
24
+
25
+
26
+
27
+#===========#
28
+# Functions #
29
+#===========#
30
+
31
+# Dependency test
32
+function test_dep {
33
+    if [[ ! $(which $1) ]]
34
+    then
35
+        error "Package $1 is needed. Exiting..." 1
36
+    fi
37
+}
38
+
39
+# Usage message
40
+function usage {
41
+    echo -e "
42
+    \e[32m${0##*/}\e[00m -l|--list list -h|--help
43
+
44
+Aim: $aim
45
+
46
+Options:
47
+    -l, --list      file containing remote path and local path of a file to synchronize.
48
+                    The list file must have a unique path pair per line and each path must
49
+                    be semi-colon separated. The remote path must be http path.
50
+
51
+    -h, --help      this message
52
+"
53
+}
54
+
55
+
56
+# Error message
57
+function error {
58
+    echo -e "\e[31mError:\e[00m $1"
59
+    exit $2
60
+}
61
+
62
+
63
+# Warning message
64
+function warning {
65
+    echo -e "\e[33mWarning:\e[00m $1"
66
+}
67
+
68
+
69
+
70
+#==============#
71
+# Dependencies #
72
+#==============#
73
+
74
+test_dep git
75
+
76
+
77
+
78
+#==========================#
79
+# Declaration of variables #
80
+#==========================#
81
+
82
+# Options
83
+while [[ $# -gt 0 ]]
84
+do
85
+    case $1 in
86
+        -l|--list   ) list="$2" ; shift 2 ;;
87
+        -h|--help   ) usage ; exit 0 ;;
88
+        *           ) error "Invalid option: $1\n$(usage)\n" 1 ;;
89
+    esac
90
+done
91
+
92
+
93
+# Check the existence of obligatory options
94
+if [[ -z "$list" ]]
95
+then
96
+    error "The option -l is required. Exiting...\n$(usage)\n" 1
97
+fi
98
+
99
+
100
+
101
+#============#
102
+# Processing #
103
+#============#
104
+
105
+# Check file list
106
+if [[ $(awk -F ";" "END {print NF}" "$list") != 2 ]]
107
+then
108
+    error "The list file "$list" is malformated. Each path pair must be on unique line and each path must be semi-colon (';') separated." 1
109
+fi
110
+
111
+
112
+# Synchronization
113
+while read line
114
+do
115
+
116
+    # Get paths
117
+    mysrc=$(echo "$line" | cut -d ";" -f 1)
118
+    mydst=$(echo "$line" | cut -d ";" -f 2)
119
+
120
+    mydst_tmp="/tmp/${mydst##*/}"
121
+
122
+    # Download file
123
+    wget -O "$mydst_tmp" "$mysrc"
124
+
125
+    # Check for errors
126
+    if [[ $? != 0 ]]
127
+    then
128
+        warning "${mydst##*/}: An error occur during the downloading. Skipping..."
129
+        continue
130
+    else
131
+        # Compare downloaded file and destination file (update file if need)
132
+        if [[ $(md5sum "$mydst_tmp") -ne $(md5sum "$mydst") ]]
133
+        then
134
+            mv "$mydst_tmp" "$mydst"
135
+        else
136
+            rm "$mydst_tmp"
137
+        fi
138
+    fi
139
+
140
+done < "$list"
141
+
142
+
143
+exit 0