Salome HOME
Improve pick-up environment mechanizm
[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
92 cat >> ${install_work}/$SHRC <<EOF
93 export INSTALL_ROOT=${install_root}
94 EOF
95 product_sequence="$2"
96 #for i in `ls -a ${install_work} | grep -E "..*" | grep -v ".salome.*"` ; do
97 for i in $product_sequence ; do
98     cat ${install_work}/env_${i}.sh >> ${install_work}/$SHRC 2>/dev/null
99 done 
100
101 sed -e 's%^export \([[:alnum:]_[:blank:]]*\)\=%setenv \1 %g' -e 's%^#csh-specific  %%g' ${INSTALL_WORK}/$SHRC > ${INSTALL_WORK}/_$CSHRC
102 pre_vars="PATH LD_LIBRARY_PATH LD_RUN_PATH PYTHONPATH"
103 for i in $pre_vars ; do
104 cat >> ${install_work}/$CSHRC <<EOF
105 if (! (\$?${i}) ) then
106   setenv $i
107 endif
108 EOF
109 done
110 cat ${INSTALL_WORK}/_$CSHRC >> ${INSTALL_WORK}/$CSHRC
111 rm -f ${INSTALL_WORK}/_$CSHRC
112
113 if [ $# -eq 4 ] ; then
114     product_dir=$4
115     if [ -e ${product_dir} ] ; then
116         cp -f ${install_work}/$SHRC ${install_work}/$CSHRC ${product_dir}/.
117     fi
118 fi
119 cp -f ${install_work}/$CSHRC ${install_root}/env_products.csh
120 cp -f ${install_work}/$SHRC ${install_root}/env_products.sh
121 }
122
123 #try use already existing product
124 try_existing()
125 {
126 product_dir=$1;  install_root=$2;  install_work=$3;  product_sequence="$4";  product_type=$5
127 env_file=${product_dir}/env_${product_type}.sh
128 if [ -f ${env_file} ] ; then
129     cp -f ${env_file} ${install_work}
130     make_env ${install_root} ${install_work} "${product_sequence}"
131     source ${install_work}/$SHRC
132     check_version
133     if [ $? -eq 0 ] ; then
134         if [ -d ${product_dir} ] && [ -w ${product_dir} ] ; then
135             #makedir ${product_dir}
136             print_env
137             return 0    
138         fi
139     fi
140 fi
141 return 1;
142 }
143
144 #check existance of lib passed as first parameter
145 #return 0 if lib exists
146 #if you pass second parameter search will be done in it 
147 #otherwise search will be done in $LD_LIBRARY_PATH
148 #you should use : as dilimeter if you are going to pass second parameter
149 check_lib_version(){
150 if [ -n "$2" ]; then
151    whereIs=$2
152 else
153    whereIs=$LD_LIBRARY_PATH
154 fi
155 for L in `echo ${whereIs} | sed -e"s%:% %g"` ;  do
156     ret=`find $L -name $1 2>/dev/null`
157     if [ -n "$ret" ] ; then
158         #echo "The $1 exists on yours system in a $L folder"
159         return 0
160     fi
161 done
162 return 1
163 }
164
165 find_in_path(){
166 file=$1;  shift; 
167 path=$*
168 for i in `echo ${path} | sed -e"s%:% %g"` ; do 
169     ret=`find $i -name $file 2>/dev/null`
170     if [ -n "$ret" ] ; then
171         ret=`echo $ret | sed -e"s%/\$file$%%g"`
172         echo $ret
173         return 0
174     fi
175 done
176 return 1
177 }
178
179 sort_path(){
180 arg1=$1;  val1=$2;  arg2=$3;  val2=$4
181 tmp="^$val1$|^$val1:|:$val1$|:$val1:"
182 #echo $val2 | grep -E "$tmp" >/dev/null 2>&1
183 #if [ $? -eq 0 ] ; then 
184 #    echo "$arg2"
185 #    return
186 #fi
187 to_tail=1
188 exclude_list="usr lib bin sbin etc"
189 for i in ${exclude_list} ; do
190     tmp="^/$i/|^/$i\$"
191     echo ${val1} | grep -E "$tmp" >/dev/null 2>&1
192     if [ $? == 0 ] ; then to_tail=0; break; fi
193 done
194 if [ $to_tail -eq 0 ] ; then
195     echo $arg2:$arg1
196 else
197     echo $arg1:$arg2
198 fi
199 return $to_tail
200 }