Salome HOME
Update for version 2.2.3
[tools/install.git] / config_files / common.sh
1 #!/bin/sh
2
3 # This script installs a subset of products necessary for SALOME application
4 #
5 # Usage: install <target-directory> [all]
6 #        install <target-directory> <product> ... <product>
7 #
8 # Type 'install --help' to get more information
9 #
10 # Important note: 
11 #   <target-directory> must be an absolute path
12 #
13 # The script is the part of the SALOME PRO installation procedure.
14 #
15 # Copyright : 2003 CEA/DEN, EDF R&D
16 ####################################################################################
17
18 export DELIM="------"
19 export SHRC="salome.sh"
20 export CSHRC="salome.csh"
21 export DEBUG=1
22
23 #this function takes several parameters
24 #first parameter contains descriptor of output file
25 #next parameters is command for execution
26 #in case of bad result of execution ask user about furher steps
27 #(user can cancel installation procedure)
28 check_job()
29 {
30   out=$1
31   shift
32   echo -e "`pwd` -> $* >> $out" 
33   if [ $DEBUG == 0 ] ; then
34     $* | tee $out
35   else
36     if [ $out == "1" ] ; then
37       $*
38     else
39       $* >> $out 2>&1
40     fi 
41   fi
42   if [ "$?" -ne "0" ] ; then
43     if [ $out != "1" ] ; then
44       cat $out
45     fi
46     echo -n " " >&2
47     echo -e "NOT DONE !!! => $*"
48     echo -n "Would you like to continue to install this product: (Y,N) [Y] : "
49     read rep;
50     rep=`echo $rep  | tr "[A-Z]" "[a-z]"`
51     if test -z $rep || [ $rep = "y" ] ; then
52       return 1
53     fi
54     exit 1
55   fi
56   return 0
57 }
58
59 #invoke check_job function
60 #all output will be put into terminal
61 check_jb()
62 {
63   check_job 1 $*
64   return
65 }
66
67 # make directory
68 makedir()
69 {
70   rm -rf "$1" > /dev/null
71   mkdir -p "$1"
72 }
73
74 #create environment for sh and csh
75 make_env()
76 {
77 install_root=$1;  shift
78 install_work=$1; 
79 for i in $SHRC $CSHRC ; do
80     if [ -e ${install_work}/$i ]; then
81         rm ${install_work}/$i
82     fi
83     touch ${install_work}/$i
84 done
85
86 if [ $# -eq 3 ] ; then
87     product_name=$3;
88     product_env=${install_work}/env_${product_name}.sh
89     (test -e ${product_env} && rm ${product_env} )
90 fi
91 ### !!! writing salome.sh file !!! -> add global functions
92 cat >> ${install_work}/$SHRC <<EOF
93 export INSTALL_ROOT=${install_root}
94
95 ##########################################################################
96 # Functions exporta and exportp are used to append/prepend correspondingly 
97 # one directory or a set of directories separated by semicolon symbol (':')
98 # to the environment variables like PATH, LD_LIBRARY_PATH, PYTHONPATH,
99 # LD_RUN_PATH etc. 
100 # The main purpose is to replace default setenv command behavior:
101 # exporta, exportp also remove duplicated entries, shortening in that way
102 # the environment variables.
103 # If some directory being added is already included into the variable
104 # nothing is done for it.
105 # Note, that these functions work some slower that setenv command itself.
106 #
107 #### cleandup ###
108 # appends/prepends set of directories (second parameter) 
109 # to the another set of directories (first parameter) and
110 # removes duplicated entries;
111 # the third parameter defines the mode: 0 - append, 1 - prepend
112 cleandup() {
113 out_var=\`echo \$1 \$2 | awk -v dir=\$3 '{                   \\
114     na = split(\$2,a,":");                                \\
115     nb = split(\$1,b,":");                                \\
116     s = "" ;                                             \\
117     for(i=1;i<=nb;i++) {                                 \\
118         if(!(b[i] in ccc)) {                             \\
119             if(length(s)!=0)                             \\
120                 s = sprintf("%s:",s);                    \\
121             s = sprintf("%s%s",s,b[i]);                  \\
122             ccc[b[i]];                                   \\
123         };                                               \\
124     };                                                   \\
125     s1 = "";                                             \\
126     for(i=1;i<=na;i++) {                                 \\
127         if(!(a[i] in ccc)) {                             \\
128             if(length(s1)!=0)                            \\
129                 s1 = sprintf("%s:",s1);                  \\
130             s1 = sprintf("%s%s",s1,a[i]);                \\
131             ccc[a[i]];                                   \\
132         };                                               \\
133     };                                                   \\
134     if(dir)                                              \\
135         s = length(s) == 0 ? s1 : sprintf("%s:%s",s1,s); \\
136     else                                                 \\
137         s = length(s1) == 0 ? s : sprintf("%s:%s",s,s1); \\
138     printf("%s\n",s);                                    \\
139     }'\`
140 echo \$out_var
141 }
142 ### exporta ###
143 # appends directory or set of directories, separated by ':' (second parameter)
144 # to the variable (first parameter)
145 exporta () { 
146 xenv=\`printenv \$1\`
147 out_var=\`cleandup \$xenv \$2 0\`
148 export \$1=\$out_var
149 }
150 ### exportp ###
151 # prepends directory or set of directories, separated by ':' (second parameter)
152 # to the variable (first parameter)
153 exportp () { 
154 xenv=\`printenv \$1\`
155 out_var=\`cleandup \$xenv \$2 1\`
156 export \$1=\$out_var
157 }
158 ###########################################################################
159
160 EOF
161 ### !!! writing salome.csh file !!! -> add global aliases
162 cat >> ${install_work}/$CSHRC <<EOF
163 setenv INSTALL_ROOT ${install_root}
164
165 ###########################################################################
166 # Aliases setenva and setenvp are used to append/prepend correspondingly 
167 # one directory or a set of directories separated by semicolon symbol (':')
168 # to the environment variables like PATH, LD_LIBRARY_PATH, PYTHONPATH,
169 # LD_RUN_PATH etc. 
170 # The main purpose is to replace default setenv command behavior:
171 # setenva, setenvp also remove duplicated entries, shortening in that way
172 # the environment variables.
173 # If some directory being added is already included into the variable
174 # nothing is done for it.
175 # Note, that these aliases work some slower that setenv command itself.
176 #
177 #### cleandup ###
178 # appends/prepends set of directories (second parameter) 
179 # to the another set of directories (first parameter) and
180 # removes duplicated entries;
181 # the third parameter defines the mode: 0 - append, 1 - prepend
182 alias cleandup "echo \!:1 \!:2 | awk -v dir=\!:3         \\\\
183 '"'{ na = split(\$2,a,":");                               \\\\
184      nb = split(\$1,b,":");                               \\\\
185      s = "" ;                                            \\\\
186      for(i=1;i<=nb;i++) {                                \\\\
187         if(!(b[i] in ccc)) {                             \\\\
188             if(length(s)!=0)                             \\\\
189                 s = sprintf("%s:",s);                    \\\\
190             s = sprintf("%s%s",s,b[i]);                  \\\\
191             ccc[b[i]];                                   \\\\
192         };                                               \\\\
193     };                                                   \\\\
194     s1 = "";                                             \\\\
195     for(i=1;i<=na;i++) {                                 \\\\
196         if(!(a[i] in ccc)) {                             \\\\
197             if(length(s1)!=0)                            \\\\
198                 s1 = sprintf("%s:",s1);                  \\\\
199             s1 = sprintf("%s%s",s1,a[i]);                \\\\
200             ccc[a[i]];                                   \\\\
201         };                                               \\\\
202     };                                                   \\\\
203     if(dir)                                              \\\\
204         s = length(s) == 0 ? s1 : sprintf("%s:%s",s1,s); \\\\
205     else                                                 \\\\
206         s = length(s1) == 0 ? s : sprintf("%s:%s",s,s1); \\\\
207     printf("%s\n",s);                                    \\\\
208     }'"'"
209 ### setenva ###
210 # appends directory or set of directories, separated by ':' (second parameter)
211 # to the variable (first parameter)
212 alias setenva 'set a=\!:1 ; set b=\!:2 ;  \\
213                set c=\`printenv \$a\` ;      \\
214                set b=\`cleandup \$c \$b 0\` ; \\
215                setenv \$a \$b ;             \\
216                unset a, b, c'
217
218 ### setenvp ###
219 # prepends directory or set of directories, separated by ':' (second parameter)
220 # to the variable (first parameter)
221 alias setenvp 'set a=\!:1 ; set b=\!:2 ;  \\
222                set c=\`printenv \$a\` ;      \\
223                set b=\`cleandup \$c \$b 1\` ; \\
224                setenv \$a \$b ;             \\
225                unset a, b, c'
226 ###########################################################################
227
228 EOF
229 ### !!! writing salome.csh file !!! -> set inital variables
230 pre_vars="PATH LD_LIBRARY_PATH LD_RUN_PATH PYTHONPATH TCLLIBPATH"
231 for i in $pre_vars ; do
232 cat >> ${install_work}/$CSHRC <<EOF
233 #$DELIM $i : initial settings $DELIM
234 if (! (\$?${i}) ) then
235   setenv $i
236 endif
237 EOF
238 done
239 ### !!! writing salome.(c)sh files !!! -> dump products environment files, changing 'export' by 'setenv(a|p)' where necessary
240 product_sequence="$2"
241 for i in $product_sequence ; do
242     sed -e 's%^export\([[:blank:]]*\)PATH=\$[{]\?PATH[}]\?:\(.*\)%exporta\1PATH \2%g' -e 's%^export\([[:blank:]]*\)PATH=\(.*\):\$[{]\?PATH[}]\?%exportp\1PATH \2%g' ${install_work}/env_${i}.sh > ${INSTALL_WORK}/_tmp1 2>/dev/null
243     sed -e 's%^export\([[:blank:]]*\)LD_LIBRARY_PATH=\$[{]\?LD_LIBRARY_PATH[}]\?:\(.*\)%exporta\1LD_LIBRARY_PATH \2%g' -e 's%^export\([[:blank:]]*\)LD_LIBRARY_PATH=\(.*\):\$[{]\?LD_LIBRARY_PATH[}]\?%exportp\1LD_LIBRARY_PATH \2%g' ${INSTALL_WORK}/_tmp1 > ${INSTALL_WORK}/_tmp2 2>/dev/null
244     sed -e 's%^export\([[:blank:]]*\)PYTHONPATH=\$[{]\?PYTHONPATH[}]\?:\(.*\)%exporta\1PYTHONPATH \2%g' -e 's%^export\([[:blank:]]*\)PYTHONPATH=\(.*\):\$[{]\?PYTHONPATH[}]\?%exportp\1PYTHONPATH \2%g' ${INSTALL_WORK}/_tmp2 > ${INSTALL_WORK}/_tmp1 2>/dev/null
245     sed -e 's%^export\([[:blank:]]*\)LD_RUN_PATH=\$[{]\?LD_RUN_PATH[}]\?:\(.*\)%exporta\1LD_RUN_PATH \2%g' -e 's%^export\([[:blank:]]*\)LD_RUN_PATH=\(.*\):\$[{]\?LD_RUN_PATH[}]\?%exportp\1LD_RUN_PATH \2%g' ${INSTALL_WORK}/_tmp1 > ${INSTALL_WORK}/_tmp2 2>/dev/null
246     cat ${INSTALL_WORK}/_tmp2 >> ${install_work}/$SHRC 2>/dev/null
247     
248     sed -e 's%^export \([[:alnum:]_[:blank:]]*\)\=%setenv \1 %g' ${INSTALL_WORK}/_tmp2 > ${INSTALL_WORK}/_tmp1 2>/dev/null
249     sed -e 's%^exporta \([[:alnum:]_[:blank:]]*\)%setenva \1%g' ${INSTALL_WORK}/_tmp1 > ${INSTALL_WORK}/_tmp2 2>/dev/null
250     sed -e 's%^exportp \([[:alnum:]_[:blank:]]*\)%setenvp \1%g' ${INSTALL_WORK}/_tmp2 > ${INSTALL_WORK}/_tmp1 2>/dev/null
251     cat ${INSTALL_WORK}/_tmp1 >> ${install_work}/$CSHRC 2>/dev/null
252
253     rm -f ${INSTALL_WORK}/_tmp1 ${INSTALL_WORK}/_tmp2
254 done 
255
256 if [ $# -eq 4 ] ; then
257     product_dir=$4
258     if [ -e ${product_dir} ] ; then
259         cp -f ${install_work}/$SHRC ${install_work}/$CSHRC ${product_dir}/.
260     fi
261 fi
262 cp -f ${install_work}/$CSHRC ${install_root}/env_products.csh
263 cp -f ${install_work}/$SHRC ${install_root}/env_products.sh
264 ### !!! copying build.csh script
265 if [ -e ./build.csh ]; then
266     cp -f ./build.csh ${install_root}
267 fi
268 }
269
270 #try use already existing product
271 try_existing()
272 {
273 product_dir=$1;  install_root=$2;  install_work=$3;  product_sequence="$4";  product_type=$5
274 env_file=${product_dir}/env_${product_type}.sh
275 if [ -f ${env_file} ] ; then
276     cp -f ${env_file} ${install_work}
277     make_env ${install_root} ${install_work} "${product_sequence}"
278     source ${install_work}/$SHRC
279     check_version
280     if [ $? -eq 0 ] ; then
281         if [ -d ${product_dir} ] && [ -w ${product_dir} ] ; then
282             #makedir ${product_dir}
283             print_env
284             return 0    
285         fi
286     fi
287 fi
288 return 1;
289 }
290
291 #check existance of lib passed as first parameter
292 #return 0 if lib exists
293 #if you pass second parameter search will be done in it 
294 #otherwise search will be done in $LD_LIBRARY_PATH
295 #you should use : as dilimeter if you are going to pass second parameter
296 check_lib_version(){
297 if [ -n "$2" ]; then
298    whereIs=$2
299 else
300    whereIs=$LD_LIBRARY_PATH
301 fi
302 for L in `echo ${whereIs} | sed -e"s%:% %g"` ;  do
303     ret=`find $L -name $1 2>/dev/null`
304     if [ -n "$ret" ] ; then
305         #echo "The $1 exists on yours system in a $L folder"
306         return 0
307     fi
308 done
309 return 1
310 }
311
312 find_in_path(){
313 file=$1;  shift; 
314 path=$*
315 for i in `echo ${path} | sed -e"s%:% %g"` ; do 
316     ret=`find $i -name $file 2>/dev/null`
317     if [ -n "$ret" ] ; then
318         ret=`echo $ret | sed -e"s%/\$file$%%g"`
319         echo $ret
320         return 0
321     fi
322 done
323 return 1
324 }
325
326 sort_path(){
327 arg1=$1;  val1=$2;  arg2=$3;  val2=$4
328 tmp="^$val1$|^$val1:|:$val1$|:$val1:"
329 #echo $val2 | grep -E "$tmp" >/dev/null 2>&1
330 #if [ $? -eq 0 ] ; then 
331 #    echo "$arg2"
332 #    return
333 #fi
334 to_tail=1
335 exclude_list="usr lib bin sbin etc"
336 for i in ${exclude_list} ; do
337     tmp="^/$i/|^/$i\$"
338     echo ${val1} | grep -E "$tmp" >/dev/null 2>&1
339     if [ $? == 0 ] ; then to_tail=0; break; fi
340 done
341 if [ $to_tail -eq 0 ] ; then
342     echo $arg2:$arg1
343 else
344     echo $arg1:$arg2
345 fi
346 return $to_tail
347 }