Salome HOME
ea63d95a578658fcb9919ba768c7658ffecb1b44
[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 #$DELIM globals $DELIM
95 setenvp() {
96     xenv=\$2:\`printenv \$1\`
97     out_var=\`echo \$xenv | awk -F: '{ n=0; r=1; s=\$1; x=0; for( i=2; i<=NF; i++ ){ v=\$i; t=1; if( v==s ) r=0; for( j=0; j<=n; j++ ){ if( keys[j]==v ){ t=0; break; } }; if( t ) keys[n++]=v; x=1; }; if( r ) printf "%s",s; if( x ) printf ":"; for( j=0; j<n; j++ ) { printf "%s",keys[j]; if( j<n-1 ) printf ":"; }; printf "\n"; }'\`
98     export \$1=\$out_var
99 }
100
101 setenva() {
102     xenv=\$2:\`printenv \$1\`
103     out_var=\`echo \$xenv | awk -F: '{ n=0; r=1; s=\$1; x=0; for( i=2; i<=NF; i++ ){ v=\$i; t=1; if( v==s ) r=0; for( j=0; j<=n; j++ ){ if( keys[j]==v ){ t=0; break; } }; if( t ) keys[n++]=v; x=1; }; for( j=0; j<n; j++ ) { printf "%s",keys[j]; if( j<n-1 ) printf ":"; }; if( r ) { if( x ) printf ":"; printf "%s",s; }; printf "\n"; }'\`
104     export \$1=\$out_var
105 }
106 EOF
107 ### !!! writing salome.csh file !!! -> add global aliases
108 cat >> ${install_work}/$CSHRC <<EOF
109 setenv INSTALL_ROOT ${install_root}
110 #$DELIM globals $DELIM
111 alias cleandup  "echo \!:1 | awk -F: '"'{ n=0; r=1; s=\$1; x=0; for( i=2; i<=NF; i++ ){ v=\$i; t=1; if( v==s ) r=0; for( j=0; j<=n; j++ ){ if( keys[j]==v ){ t=0; break; } }; if( t ) keys[n++]=v; x=1; }; if( r ) printf "%s",s; if( x ) printf ":"; for( j=0; j<n; j++ ) { printf "%s",keys[j]; if( j<n-1 ) printf ":"; }; printf "\n"; }'"'"
112 alias cleandupr "echo \!:1 | awk -F: '"'{ n=0; r=1; s=\$1; x=0; for( i=2; i<=NF; i++ ){ v=\$i; t=1; if( v==s ) r=0; for( j=0; j<=n; j++ ){ if( keys[j]==v ){ t=0; break; } }; if( t ) keys[n++]=v; x=1; }; for( j=0; j<n; j++ ) { printf "%s",keys[j]; if( j<n-1 ) printf ":"; }; if( r ) { if( x ) printf ":"; printf "%s",s; }; printf "\n"; }'"'"
113
114 alias setenva 'set a="\!:1" ; set b="'\!:2'":\`printenv \$a\` ; set b=\`cleandupr \$b\` ; setenv \$a \$b ; unset a, b'
115 alias setenvp 'set a="\!:1" ; set b="'\!:2'":\`printenv \$a\` ; set b=\`cleandup  \$b\` ; setenv \$a \$b ; unset a, b'
116 EOF
117 ### !!! writing salome.csh file !!! -> set inital variables
118 pre_vars="PATH LD_LIBRARY_PATH LD_RUN_PATH PYTHONPATH TCLLIBPATH"
119 for i in $pre_vars ; do
120 cat >> ${install_work}/$CSHRC <<EOF
121 #$DELIM $i : initial settings $DELIM
122 if (! (\$?${i}) ) then
123   setenv $i
124 endif
125 EOF
126 done
127 ### !!! writing salome.(c)sh files !!! -> dump products environment files, changing 'export' by 'setenv(a|p)' where necessary
128 product_sequence="$2"
129 for i in $product_sequence ; do
130     sed -e 's%^export\([[:blank:]]*\)PATH=\$[{]\?PATH[}]\?:\(.*\)%setenva\1PATH \2%g' -e 's%^export\([[:blank:]]*\)PATH=\(.*\):\$[{]\?PATH[}]\?%setenvp\1PATH \2%g' ${install_work}/env_${i}.sh > ${INSTALL_WORK}/_tmp1 2>/dev/null
131     sed -e 's%^export\([[:blank:]]*\)LD_LIBRARY_PATH=\$[{]\?LD_LIBRARY_PATH[}]\?:\(.*\)%setenva\1LD_LIBRARY_PATH \2%g' -e 's%^export\([[:blank:]]*\)LD_LIBRARY_PATH=\(.*\):\$[{]\?LD_LIBRARY_PATH[}]\?%setenvp\1LD_LIBRARY_PATH \2%g' ${INSTALL_WORK}/_tmp1 > ${INSTALL_WORK}/_tmp2 2>/dev/null
132     sed -e 's%^export\([[:blank:]]*\)PYTHONPATH=\$[{]\?PYTHONPATH[}]\?:\(.*\)%setenva\1PYTHONPATH \2%g' -e 's%^export\([[:blank:]]*\)PYTHONPATH=\(.*\):\$[{]\?PYTHONPATH[}]\?%setenvp\1PYTHONPATH \2%g' ${INSTALL_WORK}/_tmp2 > ${INSTALL_WORK}/_tmp1 2>/dev/null
133     sed -e 's%^export\([[:blank:]]*\)LD_RUN_PATH=\$[{]\?LD_RUN_PATH[}]\?:\(.*\)%setenva\1LD_RUN_PATH \2%g' -e 's%^export\([[:blank:]]*\)LD_RUN_PATH=\(.*\):\$[{]\?LD_RUN_PATH[}]\?%setenvp\1LD_RUN_PATH \2%g' ${INSTALL_WORK}/_tmp1 > ${INSTALL_WORK}/_tmp2 2>/dev/null
134     cat ${INSTALL_WORK}/_tmp2 >> ${install_work}/$SHRC 2>/dev/null
135     
136     sed -e 's%^export \([[:alnum:]_[:blank:]]*\)\=%setenv \1 %g' ${INSTALL_WORK}/_tmp2 > ${INSTALL_WORK}/_tmp1 2>/dev/null
137     cat ${INSTALL_WORK}/_tmp1 >> ${install_work}/$CSHRC 2>/dev/null
138
139     rm -f ${INSTALL_WORK}/_tmp1 ${INSTALL_WORK}/_tmp2
140 done 
141
142 if [ $# -eq 4 ] ; then
143     product_dir=$4
144     if [ -e ${product_dir} ] ; then
145         cp -f ${install_work}/$SHRC ${install_work}/$CSHRC ${product_dir}/.
146     fi
147 fi
148 cp -f ${install_work}/$CSHRC ${install_root}/env_products.csh
149 cp -f ${install_work}/$SHRC ${install_root}/env_products.sh
150 ### !!! copying build.csh script
151 if [ -e ./build.csh ]; then
152     cp -f ./build.csh ${install_root}
153 fi
154 }
155
156 #try use already existing product
157 try_existing()
158 {
159 product_dir=$1;  install_root=$2;  install_work=$3;  product_sequence="$4";  product_type=$5
160 env_file=${product_dir}/env_${product_type}.sh
161 if [ -f ${env_file} ] ; then
162     cp -f ${env_file} ${install_work}
163     make_env ${install_root} ${install_work} "${product_sequence}"
164     source ${install_work}/$SHRC
165     check_version
166     if [ $? -eq 0 ] ; then
167         if [ -d ${product_dir} ] && [ -w ${product_dir} ] ; then
168             #makedir ${product_dir}
169             print_env
170             return 0    
171         fi
172     fi
173 fi
174 return 1;
175 }
176
177 #check existance of lib passed as first parameter
178 #return 0 if lib exists
179 #if you pass second parameter search will be done in it 
180 #otherwise search will be done in $LD_LIBRARY_PATH
181 #you should use : as dilimeter if you are going to pass second parameter
182 check_lib_version(){
183 if [ -n "$2" ]; then
184    whereIs=$2
185 else
186    whereIs=$LD_LIBRARY_PATH
187 fi
188 for L in `echo ${whereIs} | sed -e"s%:% %g"` ;  do
189     ret=`find $L -name $1 2>/dev/null`
190     if [ -n "$ret" ] ; then
191         #echo "The $1 exists on yours system in a $L folder"
192         return 0
193     fi
194 done
195 return 1
196 }
197
198 find_in_path(){
199 file=$1;  shift; 
200 path=$*
201 for i in `echo ${path} | sed -e"s%:% %g"` ; do 
202     ret=`find $i -name $file 2>/dev/null`
203     if [ -n "$ret" ] ; then
204         ret=`echo $ret | sed -e"s%/\$file$%%g"`
205         echo $ret
206         return 0
207     fi
208 done
209 return 1
210 }
211
212 sort_path(){
213 arg1=$1;  val1=$2;  arg2=$3;  val2=$4
214 tmp="^$val1$|^$val1:|:$val1$|:$val1:"
215 #echo $val2 | grep -E "$tmp" >/dev/null 2>&1
216 #if [ $? -eq 0 ] ; then 
217 #    echo "$arg2"
218 #    return
219 #fi
220 to_tail=1
221 exclude_list="usr lib bin sbin etc"
222 for i in ${exclude_list} ; do
223     tmp="^/$i/|^/$i\$"
224     echo ${val1} | grep -E "$tmp" >/dev/null 2>&1
225     if [ $? == 0 ] ; then to_tail=0; break; fi
226 done
227 if [ $to_tail -eq 0 ] ; then
228     echo $arg2:$arg1
229 else
230     echo $arg1:$arg2
231 fi
232 return $to_tail
233 }