#------------------------------------------------------------------------------------
proc ReplaceAllOccurences { stringToSearch stringToFind stringToReplace } {
   upvar 1 $stringToSearch priv_stringToSearch
   set lengthOfStringToFind [ string length $stringToFind ]

   if { $lengthOfStringToFind > 0 } {
      set index 0
      #DisplayMessageDuringInstall "\n[string length $priv_stringToSearch]"
      while { $index < [string length $priv_stringToSearch] } {
         if { [ string equal -nocase [string range $priv_stringToSearch $index [ expr $index + $lengthOfStringToFind - 1] ] $stringToFind ] } {
            set priv_stringToSearch [string replace $priv_stringToSearch $index [ expr $index + $lengthOfStringToFind - 1] $stringToReplace ]
            incr index [string length $stringToReplace ]
         } else {
            incr index
         }
      }
   }
}

#-----------------------------------------------------------------
proc _Script_Action { scriptPipe showStdOut} {
   global scriptDone
   if {[catch {gets $scriptPipe line} len] || [eof $scriptPipe]} {
      catch {close $scriptPipe}
      set scriptDone 1
   } elseif {$len >= 0 && $showStdOut != 0 } {
      puts "\n$line"
   }
}

#-----------------------------------------------------------------
proc ConfigIOLoop { cmdPath showStdOut installDir } {
   global scriptDone

   if { [ catch { set _script_pipe [open "|$cmdPath"] } errors ] } {
      puts "\n   $errors"
      set finalErrorMsg "$errors"

      set tempInstallDir "$installDir"
      ReplaceAllOccurences tempInstallDir "\\" "\\\\"

      if { ! [ catch { open "$tempInstallDir/install.err.tmp" a } errorid ] } {
         puts $errorid "\n###### Error #######"
         puts $errorid $finalErrorMsg
         close $errorid
      } else {
         puts stderr "Error: $finalErrorMsg"
         puts stderr "[msgcat::mc write_msg_to_tmp_err_file_err] $tempInstallDir/install.err.tmp"
      }
   } else {
      fconfigure $_script_pipe -blocking 0 -buffering none
      set scriptDone 0
      fileevent $_script_pipe readable [list _Script_Action $_script_pipe $showStdOut]
      vwait scriptDone
      }
   }
}
