+ env_file=${install_work}/env_${i}.sh
+ ( test -e $env_file && cat $env_file >> ${install_work}/$SHRC 2>/dev/null )
+done
+
+# 2. writing global functions to _salome.sh file
+cat >> ${install_work}/_$SHRC <<EOF
+##########################################################################
+# Functions exporta and exportp are used to append/prepend correspondingly
+# one directory or a set of directories separated by semicolon symbol (':')
+# to the environment variables like PATH, LD_LIBRARY_PATH, PYTHONPATH,
+# LD_RUN_PATH etc.
+# The main purpose is to replace default setenv command behavior:
+# exporta, exportp also remove duplicated entries, shortening in that way
+# the environment variables.
+# If some directory being added is already included into the variable
+# nothing is done for it.
+# Note, that these functions work some slower that setenv command itself.
+#
+#### cleandup ###
+# appends/prepends set of directories (second parameter)
+# to the another set of directories (first parameter) and
+# removes duplicated entries;
+# the third parameter defines the mode: 0 - append, 1 - prepend
+cleandup() {
+out_var=\`echo \$1 \$2 | awk -v dir=\$3 '{ \\
+ na = split(\$2,a,":"); \\
+ if ( \$1 == "<empty>") nb = split("",b,":"); \\
+ else nb = split(\$1,b,":"); \\
+ s = "" ; \\
+ for(i=1;i<=nb;i++) { \\
+ if(!(b[i] in ccc)) { \\
+ if(length(s)!=0) \\
+ s = sprintf("%s:",s); \\
+ s = sprintf("%s%s",s,b[i]); \\
+ ccc[b[i]]; \\
+ }; \\
+ }; \\
+ s1 = ""; \\
+ for(i=1;i<=na;i++) { \\
+ if(!(a[i] in ccc)) { \\
+ if(length(s1)!=0) \\
+ s1 = sprintf("%s:",s1); \\
+ s1 = sprintf("%s%s",s1,a[i]); \\
+ ccc[a[i]]; \\
+ }; \\
+ }; \\
+ if(dir) \\
+ s = length(s) == 0 ? s1 : sprintf("%s:%s",s1,s); \\
+ else \\
+ s = length(s1) == 0 ? s : sprintf("%s:%s",s,s1); \\
+ printf("%s\n",s); \\
+ }'\`
+echo \$out_var
+}
+### exporta ###
+# appends directory or set of directories, separated by ':' (second parameter)
+# to the variable (first parameter)
+exporta () {
+xenv=\`printenv \$1\`
+if [ -z "\$xenv" ]; then xenv="<empty>"; fi
+out_var=\`cleandup \$xenv \$2 0\`
+export \$1=\$out_var
+}
+### exportp ###
+# prepends directory or set of directories, separated by ':' (second parameter)
+# to the variable (first parameter)
+exportp () {
+xenv=\`printenv \$1\`
+if [ -z "\$xenv" ]; then xenv="<empty>"; fi
+out_var=\`cleandup \$xenv \$2 1\`
+export \$1=\$out_var
+}
+###########################################################################
+
+EOF
+
+# 3. writing global functions to _salome.csh file
+cat >> ${install_work}/_$CSHRC <<EOF
+###########################################################################
+# Aliases setenva and setenvp are used to append/prepend correspondingly
+# one directory or a set of directories separated by semicolon symbol (':')
+# to the environment variables like PATH, LD_LIBRARY_PATH, PYTHONPATH,
+# LD_RUN_PATH etc.
+# The main purpose is to replace default setenv command behavior:
+# setenva, setenvp also remove duplicated entries, shortening in that way
+# the environment variables.
+# If some directory being added is already included into the variable
+# nothing is done for it.
+# Note, that these aliases work some slower that setenv command itself.
+#
+#### cleandup ###
+# appends/prepends set of directories (second parameter)
+# to the another set of directories (first parameter) and
+# removes duplicated entries;
+# the third parameter defines the mode: 0 - append, 1 - prepend
+alias cleandup "echo \!:1 \!:2 | awk -v dir=\!:3 \\\\
+'"'{ na = split(\$2,a,":"); \\\\
+ nb = split(\$1,b,":"); \\\\
+ s = "" ; \\\\
+ for(i=1;i<=nb;i++) { \\\\
+ if(!(b[i] in ccc)) { \\\\
+ if(length(s)!=0) \\\\
+ s = sprintf("%s:",s); \\\\
+ s = sprintf("%s%s",s,b[i]); \\\\
+ ccc[b[i]]; \\\\
+ }; \\\\
+ }; \\\\
+ s1 = ""; \\\\
+ for(i=1;i<=na;i++) { \\\\
+ if(!(a[i] in ccc)) { \\\\
+ if(length(s1)!=0) \\\\
+ s1 = sprintf("%s:",s1); \\\\
+ s1 = sprintf("%s%s",s1,a[i]); \\\\
+ ccc[a[i]]; \\\\
+ }; \\\\
+ }; \\\\
+ if(dir) \\\\
+ s = length(s) == 0 ? s1 : sprintf("%s:%s",s1,s); \\\\
+ else \\\\
+ s = length(s1) == 0 ? s : sprintf("%s:%s",s,s1); \\\\
+ printf("%s\n",s); \\\\
+ }'"'"
+### setenva ###
+# appends directory or set of directories, separated by ':' (second parameter)
+# to the variable (first parameter)
+alias setenva 'set a=\!:1 ; set b=\!:2 ; \\
+ set c=\`printenv \$a\` ; \\
+ set b=\`cleandup \$c \$b 0\` ; \\
+ setenv \$a \$b ; \\
+ unset a, b, c'
+
+### setenvp ###
+# prepends directory or set of directories, separated by ':' (second parameter)
+# to the variable (first parameter)
+alias setenvp 'set a=\!:1 ; set b=\!:2 ; \\
+ set c=\`printenv \$a\` ; \\
+ set b=\`cleandup \$c \$b 1\` ; \\
+ setenv \$a \$b ; \\
+ unset a, b, c'
+###########################################################################
+
+EOF
+cat ${install_work}/$CSHRC >> ${install_work}/_$CSHRC
+
+# 4. put the contents of salome.sh to _salome.sh replacing export by export(a|p) aliases where necessary
+sed -e 's%export\([[:blank:]]*\)PATH=\$[{]\?PATH[}]\?:\(.*\)%exporta\1PATH \2%g' -e 's%export\([[:blank:]]*\)PATH=\(.*\):\$[{]\?PATH[}]\?%exportp\1PATH \2%g' ${install_work}/$SHRC > ${INSTALL_WORK}/_tmp1 2>/dev/null
+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
+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
+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
+cat ${INSTALL_WORK}/_tmp2 >> ${install_work}/_$SHRC
+
+# 5. Create a salome.csh file from salome.sh by replacing export by setenv, etc.
+sed -e 's%export \([[:alnum:]_[:blank:]]*\)\=%setenv \1 %g' ${install_work}/$SHRC > ${INSTALL_WORK}/_tmp1 2>/dev/null
+cat ${INSTALL_WORK}/_tmp1 >> ${install_work}/$CSHRC
+
+# 6. Create a _salome.csh file from _salome.sh by replacing export by setenv, exporta by setenva, etc.
+sed -e 's%export \([[:alnum:]_[:blank:]]*\)\=%setenv \1 %g' ${INSTALL_WORK}/_tmp2 > ${INSTALL_WORK}/_tmp1 2>/dev/null
+sed -e 's%exporta \([[:alnum:]_[:blank:]]*\)%setenva \1%g' ${INSTALL_WORK}/_tmp1 > ${INSTALL_WORK}/_tmp2 2>/dev/null
+sed -e 's%exportp \([[:alnum:]_[:blank:]]*\)%setenvp \1%g' ${INSTALL_WORK}/_tmp2 > ${INSTALL_WORK}/_tmp1 2>/dev/null
+sed -e 's%if \[ -n "\${ENV_FOR_LAUNCH}" \] ; then%if ( ${?ENV_FOR_LAUNCH} ) then%g' ${INSTALL_WORK}/_tmp1 > ${INSTALL_WORK}/_tmp2 2>/dev/null
+sed -e 's%if \[ "\${ENV_FOR_LAUNCH}" == "1" \] ; then%if ( "\${ENV_FOR_LAUNCH}" == "1" ) then%g' ${INSTALL_WORK}/_tmp2 > ${INSTALL_WORK}/_tmp1 2>/dev/null
+sed -e 's%^\([[:blank:]]*\)fi\([[:blank:]]*\)$%\1endif\2%g' ${INSTALL_WORK}/_tmp1 > ${INSTALL_WORK}/_tmp2 2>/dev/null
+cat ${INSTALL_WORK}/_tmp2 >> ${install_work}/_$CSHRC 2>/dev/null
+
+rm -f ${INSTALL_WORK}/_tmp1 ${INSTALL_WORK}/_tmp2