46e5f9623457a5ae5604772f4b64d1444caed8f9
[ldapsaisie.git] / upgradeFromGit.sh
1 #!/bin/bash
2
3
4 ROOT_DIR=$( cd `dirname $0`; pwd )
5 LOCAL_SAV_DIR="$ROOT_DIR/config.local"
6
7 # Import config
8 if [ ! -f $LOCAL_SAV_DIR/local.sh ]
9 then
10     echo "Error : You don't have create your own local.sh file in config.local directory. You could rely on the local.sh.example file to create your version."
11     exit 1
12 fi
13
14 source $LOCAL_SAV_DIR/local.sh
15
16 function msg() {
17     echo $2 "$1" | tee -a "$LOG_FILE"
18 }
19
20 function check_file_or_symlink() {
21     [ -f "$1" ] && echo 0 && return 0
22     if [ -L "$1" ]
23     then
24         [ -r "$1" ] && echo 0 && return 0
25         rm -f "$1"
26     fi
27     echo 1 && return 1
28 }
29
30 cd $ROOT_DIR
31
32 msg "-> Clean git repos : "
33 for i in $LOCAL_FILES
34 do
35         msg "\t-> $i : " -en
36         if [ -L $i ]
37         then
38         msg "\n\t\t-> Delete file : " -en
39                 rm -fr $i >> $LOG_FILE 2>&1
40                 if [ $? -gt 0 ]
41                 then
42                         msg "Error"
43                         exit 1
44                 else
45                         msg "Ok"
46                 fi
47                 if [ -f $i.sav ]
48                 then
49                 msg "\t\t-> Restore orignal file : " -en
50                         mv $i.sav $i >> $LOG_FILE 2>&1
51                         if [ $? -gt 0 ]
52                         then
53                                 msg "Error"
54                                 exit 1
55                         else
56                                 msg "Ok"
57                         fi
58                 fi
59         else
60                 msg "file does not exist, pass..."
61         fi
62 done
63
64 if [ $BUILD_DOC -eq 1 ]
65 then
66         msg "-> Clean the doc : " -en
67         cd $ROOT_DIR/doc >> $LOG_FILE && make clean >> $LOG_FILE && cd - >> $LOG_FILE
68         if [ $? -gt 0 ]
69         then
70                 msg "Error"
71                 exit 1
72         else
73                 msg "Ok"
74         fi
75 fi
76
77 if [ "$THEME" != "" ]
78 then
79         msg "\t\t-> Remove theme : " -en
80         rm -f $ROOT_DIR/public_html/templates/$THEME $ROOT_DIR/public_html/images/$THEME $ROOT_DIR/public_html/css/$THEME >> $LOG_FILE 2>&1
81         if [ $? -gt 0 ]
82         then
83         msg "Error"
84     exit 1
85   else
86         msg "Ok"
87   fi
88 fi
89
90 msg "\t\t-> Clean template cache : " -en
91 rm -f $ROOT_DIR/public_html/tmp/*.tpl.php
92 if [ $? -gt 0 ]
93 then
94         msg "Error"
95         exit 1
96 else
97         msg "Ok"
98 fi
99
100 msg "-> Verification of git repos state : "
101 git status >> $LOG_FILE 2>&1
102 if [ "$ETAT" != "" ]
103 then
104         msg "\n\t-> [Error] Some changes have been made to source code since the last update." -e
105         exit 1
106 fi
107 msg "\t->[OK]" -e
108
109 msg "-> Upgrade git repos : "
110 RES_GIT=`git pull`
111 RES=$?
112 msg "$RES_GIT" -e
113 if [ $RES -gt 0 ]
114 then
115         msg "\t-> [Error] Problem during git repos pull." -e
116         exit 1
117 fi
118 msg "\t-> [OK]" -e
119
120 msg "-> Install local files : "
121 for i in $LOCAL_FILES
122 do
123         msg "\t-> $i : " -ne
124         SRC="$LOCAL_SAV_DIR/`basename $i`"
125         if [ -f $SRC ]
126         then
127                 msg
128         if [ -f $ROOT_DIR/$i ]
129                 then
130                         msg "\t\t-> Backup original file : " -en
131                         mv $ROOT_DIR/$i $ROOT_DIR/$i.sav >> $LOG_FILE 2>&1
132                         if [ $? -gt 0 ]
133                         then
134                                 msg "Error"
135                                 exit 1
136                         fi
137                         msg "Ok"
138
139                         msg "\t\t-> Check possible change of the original file since last upgrade : " -en
140                         if [ -f $SRC.orig ]
141                         then
142                                 DIFF=`diff $ROOT_DIR/$i.sav $SRC.orig`
143                                 if [ "$DIFF" != "" ]
144                                 then    
145                                     msg "\n$DIFF\n\t\t\t-> Caution : This file changed. Do you want edit this file now ? [y/N] " -en
146                                         read a
147                                         echo "Reponse : $a"  >> $LOG_FILE
148                                         if [ "$a" == "y" -o "$a" == "Y" ]
149                                         then
150                                                 vi -d $SRC $ROOT_DIR/$i.sav
151                                         fi
152                                 else
153                                         msg "No change"
154                                 fi
155                         else
156                                 msg "Original backup file does not exist. Pass ..."
157                         fi
158                         msg "\t\t-> Backup file for next upgrade : " -en
159                         cp -f $ROOT_DIR/$i.sav $SRC.orig >> $LOG_FILE 2>&1
160                         if [ $? -gt 0 ]
161                         then
162                                 msg "Error"
163                 exit 1
164                         fi
165                         msg "Ok"
166                 fi
167                 msg "\t\t-> Install local file : " -en
168                 ln -s $SRC $ROOT_DIR/$i >> $LOG_FILE 2>&1
169                 if [ $? -gt 0 ]
170                 then
171                         msg "Error"
172                         exit 1
173                 fi
174                 msg "Ok"
175         else
176                 msg "file does not exist. Pass..."
177         fi
178 done
179
180 if [ "$THEME" != "" ]
181 then
182         msg "-> Install theme : "
183
184         # TPL
185         msg "\t- Template : " -e
186         ln -s $LOCAL_SAV_DIR/theme/templates $ROOT_DIR/public_html/templates/$THEME >> $LOG_FILE 2>&1
187         if [ -d $ROOT_DIR/public_html/templates/$THEME_TPL_REF ]
188         then
189                 msg "\t\t-> Vérification de la présence des fichiers : " -e
190                 for i in $ROOT_DIR/public_html/templates/$THEME_TPL_REF/*
191                 do
192                         f=`basename $i`
193                         msg "\t\t\t- $f : " -en
194                         if [ `check_file_or_symlink "$ROOT_DIR/public_html/templates/$THEME/$f"` -eq 0 ]
195                         then
196                                 msg "present."
197                         else
198                                 ln -s $ROOT_DIR/public_html/templates/$THEME_TPL_REF/$f $ROOT_DIR/public_html/templates/$THEME/$f
199                                 msg "link."
200                         fi
201                 done
202         fi
203
204         # IMG
205         msg "\t- Images : " -e
206         ln -s $LOCAL_SAV_DIR/theme/images $ROOT_DIR/public_html/images/$THEME >> $LOG_FILE 2>&1
207         if [ -d $ROOT_DIR/public_html/images/$THEME_IMG_REF ]
208         then
209                 msg "\t\t-> Vérification de la présence des fichiers : " -e
210                 for i in $ROOT_DIR/public_html/images/$THEME_IMG_REF/*
211                 do
212                         f=`basename $i`
213                         msg "\t\t\t- $f : " -en
214                         if [ `check_file_or_symlink "$ROOT_DIR/public_html/images/$THEME/$f"` -eq 0 ]
215                         then
216                                 msg "present."
217                         else
218                                 ln -s $ROOT_DIR/public_html/images/$THEME_IMG_REF/$f $ROOT_DIR/public_html/images/$THEME/$f
219                                 msg "link."
220                         fi
221                 done
222         fi
223
224         # CSS
225         msg "\t- CSS : " -e
226         ln -s $LOCAL_SAV_DIR/theme/css $ROOT_DIR/public_html/css/$THEME >> $LOG_FILE 2>&1
227         if [ -d $ROOT_DIR/public_html/css/$THEME_CSS_REF ]
228         then
229                 msg "\t\t-> Vérification de la présence des fichiers : " -e
230                 for i in $ROOT_DIR/public_html/css/$THEME_CSS_REF/*
231                 do
232                         f=`basename $i`
233                         msg "\t\t\t- $f : " -en
234                         if [ `check_file_or_symlink "$ROOT_DIR/public_html/css/$THEME/$f"` -eq 0 ]
235                         then
236                                 msg "present."
237                         else
238                                 ln -s $ROOT_DIR/public_html/css/$THEME_CSS_REF/$f $ROOT_DIR/public_html/css/$THEME/$f
239                                 msg "link."
240                         fi
241                 done
242         fi
243 fi
244
245 if [ $BUILD_DOC -eq 1 ]
246 then
247         [ -n "$LAST_UPDATE_FILE" ] && [ "`$ROOT_DIR/checkDocExportsNecessity.sh`" == "" ] && echo "Export documentation is not necessary. Pass." && exit 
248         msg "-> Do you want build the documentation (y/N) ? " -en
249         read a
250         if [ "$a" == "y" -o "$a" == "Y" ]
251         then
252                 msg "-> Build the doc : " -en
253                 cd $ROOT_DIR/doc
254                 
255                 make clean >> $LOG_FILE 2>&1
256                 make >> $LOG_FILE 2>&1 &
257                 
258                 export P=$!
259                 
260                 trap exitwhell INT
261                 
262                 function exitwhell() {
263                         [ -n "$P" ] && kill -9 $P 2> /dev/null
264                         echo " -- INT -- "
265                         exit 1
266                 }
267                 
268                 while [ -d /proc/$P ]
269                 do
270                         echo -n .
271                         sleep 1
272                 done
273                 echo done.
274
275                 if [ -n "$EXPORT_DOC_DIR" ]
276                 then
277                     $ROOT_DIR/buildDocExports.sh
278                 fi
279         fi
280 fi