# Check to see if a .deb package is currently installed. def deb_package_installed?(pkg_name) IO.popen("dpkg-query -s #{pkg_name} 2> /dev/null") do |dpkg| return !dpkg.read.empty? end end # Execute a command and return if the command was successful or not. def sh(cmd_line) `#{cmd_line}` return $? == 0 end def executable_exists?(name, harmless_option = '-h') IO.popen("/usr/bin/env #{name} #{harmless_option} 2>/dev/null", 'r') do |bin| return !bin.read.empty? end end REQUIRED_PACKAGES = ['build-essential', 'ruby1.8-dev'] if RUBY_PLATFORM !~ /mswin32/ && executable_exists?('dpkg-query') # if we're not on Windows and we're using .debs if deb_package_installed?('ruby1.8') # if we're using the package needed_packages = REQUIRED_PACKAGES.dup.delete_if{ |pkg_name| deb_package_installed?(pkg_name) } unless needed_packages.empty? puts "Mongrel requires the following Debian packages be installed:" needed_packages.each{ |pkg_name| puts " * #{pkg_name}" } print "\nWould you like to install these now? [y/n] " if STDIN.gets.strip.downcase == 'y' puts 'Installing required packages...' if !sh("sudo apt-get install #{needed_packages.join(' ')}") puts "Error installing packages for Mongrel!" exit end else puts "\nWarning: There's a very good chance that you won't be able to compile" puts " Mongrel. Unless you have a C compiler and Ruby's development" puts " files installed, Mongrel won't work." print "\nAre you sure you want to continue? [y/n] " exit if STDIN.gets.strip.downcase != 'y' end end elsif executable_exists?('ruby') # if we're using a source install puts "\nWarning: It looks like you've installed Ruby from source. Make sure" puts " you also have all the development headers for Ruby. Without" puts " those, Mongrel won't be able to compile its C extension." else # can't find Ruby, can't find the packages... and yet we're running... odd puts "\nWarning: Cannot detect your Ruby installation. Installing Mongrel may" puts " or may not work. Here be dragons." end end