diff --git a/Library/Homebrew/cmd/update.sh b/Library/Homebrew/cmd/update.sh
index 49dcb5038d522356e72bd391d25b78b0efbb07aa..d6ce41b006036a68d8ec28062f4370e14fea8d7a 100644
--- a/Library/Homebrew/cmd/update.sh
+++ b/Library/Homebrew/cmd/update.sh
@@ -306,7 +306,7 @@ EOS
         # (so the API does not return 304: unmodified).
         UPSTREAM_SHA_HTTP_CODE="$(curl --silent '--max-time' 3 \
            --output /dev/null --write-out "%{http_code}" \
-           --user-agent "Homebrew $HOMEBREW_VERSION" \
+           --user-agent "$HOMEBREW_USER_AGENT_CURL" \
            --header "Accept: application/vnd.github.chitauri-preview+sha" \
            --header "If-None-Match: \"$UPSTREAM_BRANCH_LOCAL_SHA\"" \
            "https://api.github.com/repos/$UPSTREAM_REPOSITORY/commits/$UPSTREAM_BRANCH")"
diff --git a/Library/Homebrew/global.rb b/Library/Homebrew/global.rb
index cd0baf742a0a113763f483b6703baa3d682608ff..e6b7f7e5d27ad78fea921a07cfb82e94c75b12b2 100644
--- a/Library/Homebrew/global.rb
+++ b/Library/Homebrew/global.rb
@@ -26,7 +26,8 @@ else
 end
 RUBY_BIN = RUBY_PATH.dirname
 
-HOMEBREW_USER_AGENT = "Homebrew #{HOMEBREW_VERSION} (Ruby #{RUBY_VERSION}-#{RUBY_PATCHLEVEL}; #{OS_VERSION})"
+HOMEBREW_USER_AGENT_CURL = ENV["HOMEBREW_USER_AGENT_CURL"]
+HOMEBREW_USER_AGENT_RUBY = "#{ENV["HOMEBREW_USER_AGENT"]} ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
 
 HOMEBREW_CURL_ARGS = "-f#LA"
 
diff --git a/Library/Homebrew/os.rb b/Library/Homebrew/os.rb
index 1b8ed7b8ec18f51532922948f167e40adb2aed0d..b0d685fa4df447dbd86ab2028d4ccf059e0fd410 100644
--- a/Library/Homebrew/os.rb
+++ b/Library/Homebrew/os.rb
@@ -7,6 +7,8 @@ module OS
     /linux/i === RUBY_PLATFORM
   end
 
+  ::OS_VERSION = ENV["HOMEBREW_OS_VERSION"]
+
   if OS.mac?
     require "os/mac"
     ISSUES_URL = "https://git.io/brew-troubleshooting"
@@ -14,13 +16,11 @@ module OS
     # compatibility
     ::MACOS_FULL_VERSION = OS::Mac.full_version.to_s
     ::MACOS_VERSION = OS::Mac.version.to_s
-    ::OS_VERSION = "OS X #{MACOS_FULL_VERSION}"
   elsif OS.linux?
     ISSUES_URL = "https://github.com/Homebrew/linuxbrew/wiki/troubleshooting"
     PATH_OPEN = "xdg-open"
     # compatibility
     ::MACOS_FULL_VERSION = ::MACOS_VERSION = "0"
-    ::OS_VERSION = RUBY_PLATFORM
   else
     raise "Unknown operating system"
   end
diff --git a/Library/Homebrew/os/mac.rb b/Library/Homebrew/os/mac.rb
index af27911ebb55abd799dfac8297c482f75cef67bb..fcaaf27233e02cc92acade7df5f9556e3451c791 100644
--- a/Library/Homebrew/os/mac.rb
+++ b/Library/Homebrew/os/mac.rb
@@ -21,7 +21,7 @@ module OS
     # This can be compared to numerics, strings, or symbols
     # using the standard Ruby Comparable methods.
     def full_version
-      @full_version ||= Version.new(`/usr/bin/sw_vers -productVersion`.chomp)
+      @full_version ||= Version.new(ENV["HOMEBREW_OSX_VERSION"].chomp)
     end
 
     def prerelease?
diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb
index 509a5ff2066c779912353b46ed2d15f88228e473..f5fb738e73fc5f5675ef2ff35533ecf59a08ee08 100644
--- a/Library/Homebrew/utils.rb
+++ b/Library/Homebrew/utils.rb
@@ -309,18 +309,13 @@ def quiet_system(cmd, *args)
 end
 
 def curl(*args)
-  brewed_curl = HOMEBREW_PREFIX/"opt/curl/bin/curl"
-  curl = if MacOS.version <= "10.8" && brewed_curl.exist?
-    brewed_curl
-  else
-    Pathname.new "/usr/bin/curl"
-  end
+  curl = Pathname.new ENV["HOMEBREW_CURL"]
   raise "#{curl} is not executable" unless curl.exist? && curl.executable?
 
   flags = HOMEBREW_CURL_ARGS
   flags = flags.delete("#") if ARGV.verbose?
 
-  args = [flags, HOMEBREW_USER_AGENT, *args]
+  args = [flags, HOMEBREW_USER_AGENT_CURL, *args]
   args << "--verbose" if ENV["HOMEBREW_CURL_VERBOSE"]
   args << "--silent" if !$stdout.tty? || ENV["TRAVIS"]
 
@@ -594,7 +589,7 @@ module GitHub
 
   def api_headers
     {
-      "User-Agent" => HOMEBREW_USER_AGENT,
+      "User-Agent" => HOMEBREW_USER_AGENT_RUBY,
       "Accept"     => "application/vnd.github.v3+json"
     }
   end
diff --git a/Library/brew.sh b/Library/brew.sh
index db575c407214e64c3fdc8704cb6a7dfd82cae39f..eb6c19a97d3de9f797f272081f17830a56b170b7 100644
--- a/Library/brew.sh
+++ b/Library/brew.sh
@@ -57,9 +57,13 @@ then
   unset HOMEBREW_RUBY_PATH
 fi
 
-if [[ "$(uname -s)" = "Darwin" ]]
+HOMEBREW_SYSTEM="$(uname -s)"
+if [[ "$HOMEBREW_SYSTEM" = "Darwin" ]]
 then
   HOMEBREW_OSX="1"
+elif [[ "$HOMEBREW_SYSTEM" = "Linux" ]]
+then
+  HOMEBREW_LINUX="1"
 fi
 
 if [[ -z "$HOMEBREW_RUBY_PATH" ]]
@@ -72,13 +76,47 @@ then
   fi
 fi
 
-export HOMEBREW_VERSION
+HOMEBREW_CURL="/usr/bin/curl"
+HOMEBREW_PROCESSOR="$(uname -p)"
+if [[ -n "$HOMEBREW_OSX" ]]
+then
+  HOMEBREW_PRODUCT="Homebrew"
+  HOMEBREW_SYSTEM="Macintosh"
+  # This is i386 even on x86_64 machines
+  [[ "$HOMEBREW_PROCESSOR" = "i386" ]] && HOMEBREW_PROCESSOR="Intel"
+  HOMEBREW_OSX_VERSION="$(/usr/bin/sw_vers -productVersion)"
+  HOMEBREW_OS_VERSION="Mac OS X $HOMEBREW_OSX_VERSION"
+
+  HOMEBREW_OSX_VERSION_NUMERIC="$(printf "%02d%02d%02d" $(echo "${HOMEBREW_OSX_VERSION//./ }"))"
+  if [[ "$HOMEBREW_OSX_VERSION_NUMERIC" -lt "100900" &&
+        -x "$HOMEBREW_PREFIX/opt/curl/bin/curl" ]]
+  then
+    HOMEBREW_CURL="$HOMEBREW_PREFIX/opt/curl/bin/curl"
+  fi
+else
+  HOMEBREW_PRODUCT="${HOMEBREW_SYSTEM}brew"
+  [[ -n "$HOMEBREW_LINUX" ]] && HOMEBREW_OS_VERSION="$(lsb_release -sd 2>/dev/null)"
+  HOMEBREW_OS_VERSION="${HOMEBREW_PRODUCT:=$(uname -r)}"
+fi
+HOMEBREW_USER_AGENT="$HOMEBREW_PRODUCT/$HOMEBREW_VERSION ($HOMEBREW_SYSTEM; $HOMEBREW_PROCESSOR $HOMEBREW_OS_VERSION)"
+HOMEBREW_CURL_VERSION="$("$HOMEBREW_CURL" --version 2>/dev/null | head -n1 | awk '{print $1"/"$2}')"
+HOMEBREW_USER_AGENT_CURL="$HOMEBREW_USER_AGENT $HOMEBREW_CURL_VERSION"
+
+# Declared in bin/brew
 export HOMEBREW_BREW_FILE
-export HOMEBREW_RUBY_PATH
 export HOMEBREW_PREFIX
 export HOMEBREW_REPOSITORY
 export HOMEBREW_LIBRARY
+
+# Declared in brew.sh
+export HOMEBREW_VERSION
 export HOMEBREW_CELLAR
+export HOMEBREW_RUBY_PATH
+export HOMEBREW_CURL
+export HOMEBREW_OS_VERSION
+export HOMEBREW_OSX_VERSION
+export HOMEBREW_USER_AGENT
+export HOMEBREW_USER_AGENT_CURL
 
 if [[ -n "$HOMEBREW_OSX" ]]
 then