#  Phusion Passenger - https://www.phusionpassenger.com/
#  Copyright (c) 2010-2025 Asynchronous B.V.
#
#  "Passenger", "Phusion Passenger" and "Union Station" are registered
#  trademarks of Asynchronous 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 shared library extension ('so' on most Unices).
    def self.library_extension
      if os_name_simple == "macosx"
        return "bundle"
      else
        return "so"
      end
    end

    # Returns the `uname` command, or nil if `uname` cannot be found.
    # In addition to looking for `uname` in `PATH`, this method also looks
    # for `uname` in /bin and /usr/bin, just in case the user didn't
    # configure its PATH properly.
    def self.uname_command
      if result = find_command("uname")
        result
      elsif File.exist?("/bin/uname")
        return "/bin/uname"
      elsif File.exist?("/usr/bin/uname")
        return "/usr/bin/uname"
      else
        return nil
      end
    end

    # Returns a list of all CPU architecture names that the current machine CPU
    # supports. If there are multiple such architectures then the first item in
    # the result denotes that OS runtime's main/preferred architecture.
    #
    # This function normalizes some names. For example x86 is always reported
    # as "x86" regardless of whether the OS reports it as "i386" or "i686".
    # x86_64 is always reported as "x86_64" even if the OS reports it as "amd64".
    #
    # Please note that even if the CPU supports multiple architectures, the
    # operating system might not. For example most x86 CPUs nowadays also
    # support x86_64, but x86_64 Linux systems require various x86 compatibility
    # libraries to be installed before x86 executables can be run. This function
    # does not detect whether these compatibility libraries are installed.
    # The only guarantee that you have is that the OS can run executables in
    # the architecture denoted by the first item in the result.
    #
    # For example, on x86_64 Linux this function can return ["x86_64", "x86"].
    # This indicates that the CPU supports both of these architectures, and that
    # the OS's main/preferred architecture is x86_64. Most executables on the
    # system are thus be x86_64. It is guaranteed that the OS can run x86_64
    # executables, but not x86 executables per se.
    #
    # Another example: on MacOS X this function will return some subset of
    # "x86_64", "x86", and "arm".
    def self.cpu_architectures
      if os_name_simple == "macosx"
        # Macs were intel from around 2006 until 2022.
        # All but the first 3 Intel Macs' CPUs supported both x86_64 and x86,
        # and every OS X version from 10.4 to 10.14 had both x86 and x86_64 runtimes installed,
        # from 10.15 on x86 was dropped, and from (11/10.16) on arm (aarch64) was added.
        major, minor, *rest = os_version.split(".").map(&:to_i)
        if major >= 11 || (major == 10 && minor >= 16)
          # Since Big Sur aarch64 is supported, and default on m1 macs.
          if `#{uname_command} -m` =~ /arm64/
            ["arm64", "x86_64"]
          elsif `sysctl -in sysctl.proc_translated` == "1"
            ["arm64", "x86_64"]
          else
            ["x86_64", "arm64"]
          end
        elsif minor == 15
          # Since Catalina x86 is gone.
          ["x86_64"]
        elsif minor >= 6
          # Since Snow Leopard x86_64 is the default on intel macs.
          ["x86_64", "x86"]
        else
          # Before Snow Leopard x86 was the default.
          ["x86", "x86_64"]
        end
      else
        uname = uname_command
        raise "The 'uname' command cannot be found" if !uname
        arch = `#{uname} -p`.strip
        # On some systems 'uname -p' returns something like
        # 'Intel(R) Pentium(R) M processor 1400MHz' or
        # 'Intel(R)_Xeon(R)_CPU___________X7460__@_2.66GHz'.
        if arch == "unknown" || arch =~ / / || arch =~ /Hz$/
          arch = `#{uname} -m`.strip
        end
        if arch =~ /^i.86$/
          arch = "x86"
        elsif arch == "amd64"
          arch = "x86_64"
        end

        if arch == "x86"
          # Most x86 operating systems nowadays are probably running on
          # a CPU that supports both x86 and x86_64, but we're not gonna
          # go through the trouble of checking that. The main architecture
          # is what we usually care about.
          ["x86"]
        elsif arch == "x86_64"
          # I don't think there's a single x86_64 CPU out there
          # that doesn't support x86 as well.
          ["x86_64", "x86"]
        else
          [arch]
        end
      end
    end
    memoize :cpu_architectures, true

    # Returns whether the flock() function is supported on this OS.
    def self.supports_flock?
      defined?(File::LOCK_EX) && os_name_simple != 'solaris'
    end

    def self.requires_no_tls_direct_seg_refs?
      return File.exist?("/proc/xen/capabilities") && cpu_architectures[0] == "x86"
    end
    memoize :requires_no_tls_direct_seg_refs?, true

    # Returns true if the current os is running in Windows Subsystem for Linux
    def self.windows_subsystem?
      uname = uname_command
      raise "The 'uname' command cannot be found" if !uname
      `#{uname} -r`.include?("Microsoft")
    end
    memoize :windows_subsystem?, true
  end

end # module PhusionPassenger
