online
  8   ˆl–Ãa¾”ªeJe@ãB¸©‹Fÿ�þDëñ¬Ü¬²Ð„G¥)üÿÎ„ËÿÍÈÂÊ '›    #  Phusion Passenger - https://www.phusionpassenger.com/
#  Copyright (c) 2010-2017 Phusion Holding B.V.
#
#  "Passenger", "Phusion Passenger" and "Union Station" are registered
#  trademarks of Phusion Holding B.V.
#
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included in
#  all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#  THE SOFTWARE.

require 'rbconfig'
require 'rubygems'
PhusionPassenger.require_passenger_lib 'platform_info'

module PhusionPassenger

  module PlatformInfo
    class VersionComparer < String
      def <=>(other)
        Gem::Version.new(self) <=> Gem::Version.new(other.to_s)
      end
    end

    # Returns the operating system's name in as simple a form as possible. For example,
    # Linux is always identified as "linux". OS X is always identified as "macosx" (despite
    # the actual os name being something like "darwin"). This is useful as a stable indicator
    # of the os without having to worry about version numbers, etc.
    # N.B. unrecognized os names will just be returned as-is.
    def self.os_name_simple
      if rb_config['target_os'] =~ /darwin/ && (sw_vers = find_command('sw_vers'))
        'macosx'
      elsif rb_config['target_os'] =~ /^linux-/
        'linux'
      elsif rb_config['target_os'] =~ /solaris/
        'solaris'
      elsif rb_config['target_os'] =~ /freebsd/
        'freebsd'
      elsif rb_config['target_os'] =~ /aix/
        'aix'
      else
        rb_config['target_os']
      end
    end
    memoize :os_name_simple

    # Returns the operating system's name exactly as advertised by the system. While it is
    # in lowercase and contains no spaces, it can contain things like version number or
    # may be less intuitive (e.g. "darwin" for OS X).
    def self.os_name_full
      rb_config['target_os']
    end
    memoize :os_name_full

    # Returns the operating system's version number, or nil if unknown.
    # This includes the patch version, so for example on macOS Sierra
    # it could return "10.12.5".
    #
    # On Debian/Ubuntu, this returns the version number (e.g. "16.04")
    # as opposed to the codename ("Trusty").
    def self.os_version
      case os_name_simple
      when 'macosx'
        VersionComparer.new(`/usr/bin/sw_vers -productVersion`.strip.split.last)

      when 'linux'
        # Parse LSB (applicable to e.g. Ubuntu)
        if read_file('/etc/lsb-release') =~ /DISTRIB_RELEASE=(.+)/
          version = $1.delete(%q['"])
          return VersionComparer.new(version) if !version.empty?
        end

        # Parse CentOS/RedHat
        data = read_file('/etc/centos-release')
        data = read_file('/etc/redhat-release') if data.empty?
        if !data.empty?
          data =~ /^(.+?) (Linux |Stream )?(release |version )?(.+?)( |$)/i
          return VersionComparer.new($4) if $4
        end

        # Parse Debian
        if File.exist?('/etc/debian_version')
          return VersionComparer.new(read_file('/etc/debian_version').strip)
        end

        nil

      else
        nil
      end
    end

    # The current platform's share