#!/usr/bin/env ruby
# encoding: binary
#  Phusion Passenger - https://www.phusionpassenger.com/
#  Copyright (c) 2010-2014 Phusion
#
#  "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
#
#  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.

## Magic comment: begin bootstrap ##
source_root = File.expand_path("..", File.dirname(__FILE__))
$LOAD_PATH.unshift("#{source_root}/lib")
begin
  require 'rubygems'
rescue LoadError
end
require 'phusion_passenger'
## Magic comment: end bootstrap ##

PhusionPassenger.locate_directories

# The Apache executable may be located in an 'sbin' folder. We add
# the 'sbin' folders to $PATH just in case. On some systems
# 'sbin' isn't in $PATH unless the user is logged in as root from
# the start (i.e. not via 'su' or 'sudo').
ENV["PATH"] += ":/usr/sbin:/sbin:/usr/local/sbin"

require 'optparse'
require 'stringio'
PhusionPassenger.require_passenger_lib 'constants'
PhusionPassenger.require_passenger_lib 'platform_info/ruby'
PhusionPassenger.require_passenger_lib 'platform_info/apache'
PhusionPassenger.require_passenger_lib 'platform_info/apache_detector'
PhusionPassenger.require_passenger_lib 'abstract_installer'
PhusionPassenger.require_passenger_lib 'utils/terminal_choice_menu'

class Installer < PhusionPassenger::AbstractInstaller
  include PhusionPassenger
  TerminalChoiceMenu = PhusionPassenger::Utils::TerminalChoiceMenu

  AUTOINSTALL_BEGIN_LOAD_BLOCK = "### Begin automatically installed #{PROGRAM_NAME} load snippet ###"
  AUTOINSTALL_END_LOAD_BLOCK   = "### End automatically installed #{PROGRAM_NAME} load snippet ###"
  AUTOINSTALL_BEGIN_CONF_BLOCK = "### Begin automatically installed #{PROGRAM_NAME} config snippet ###"
  AUTOINSTALL_END_CONF_BLOCK   = "### End automatically installed #{PROGRAM_NAME} config snippet ###"

  def dependencies
    specs = [
      'depcheck_specs/compiler_toolchain',
      'depcheck_specs/ruby',
      'depcheck_specs/gems',
      'depcheck_specs/libs',
      'depcheck_specs/apache2'
    ]
    ids = [
      'cc',
      'c++',
      'libcurl-dev',
      'openssl-dev',
      'zlib-dev',
      'apache2',
      'apache2-dev',
      'rake',
      'ruby-openssl',
      'rubygems'
    ]
    if @languages.include?("ruby")
      if PlatformInfo.passenger_needs_ruby_dev_header?
        ids << 'ruby-dev'
      end
      ids << 'rack'
    end
    # Some broken servers don't have apr-config or apu-config installed.
    # Nevertheless, it is possible to compile Apache modules if Apache
    # was configured with --included-apr. So here we check whether
    # apr-config and apu-config are available. If they're not available,
    # then we only register them as required dependency if no Apache
    # module can be compiled without their presence.
    if (PlatformInfo.apr_config && PlatformInfo.apu_config) ||
       PlatformInfo.apr_config_needed_for_building_apache_modules?
      ids << 'apr-dev'
      ids << 'apu-dev'
    end
    return [specs, ids]
  end

  def users_guide_path
    return PhusionPassenger.apache2_doc_path
  end

  def users_guide_url
    return APACHE2_DOC_URL
  end

  def run_steps
    if PhusionPassenger.build_system_dir.nil?
      # Invariant: PhusionPassenger.custom_packaged?
      if apache_module_available?
        notify_apache_module_installed
        show_deployment_example
      else
        install_apache_module_from_native_package || exit(1)
      end
      exit
    end

    Dir.chdir(PhusionPassenger.build_system_dir)
    show_welcome_screen
    query_interested_languages
    check_gem_install_permission_problems || exit(1)
    check_directory_accessible_by_web_server
    check_dependencies || exit(1)
    check_whether_there_are_multiple_apache_installs || exit
    check_whether_apache_uses_compatible_mpm
    check_whether_os_is_broken
    check_whether_system_has_enough_ram
    check_write_permission_to_passenger_root || exit(1)
    check_write_permission_to_web_server_config_files || exit(1)
    if compile_apache2_module
      install_apache2_config_snippets || exit(1)
      show_deployment_example
    else
      show_possible_solutions_for_compilation_and_installation_problems
      exit(1)
    end
  end

private
  def show_welcome_screen
    render_template 'apache2/welcome', :version => VERSION_STRING
    wait
  end

  def query_interested_languages
    menu = TerminalChoiceMenu.new(["Ruby", "Python", "Node.js", "Meteor"])
    menu["Ruby"].checked = interesting_language?('ruby')
    menu["Python"].checked = interesting_language?('python')
    menu["Node.js"].checked = interesting_language?('nodejs', 'node')
    menu["Meteor"].checked = interesting_language?('meteor')

    new_screen
    puts "<banner>Which languages are you interested in?</banner>"
    puts
    if interactive?
      puts "Use <space> to select."
      puts "<dgray>If the menu doesn't display correctly, press '!'</dgray>"
    else
      puts "Override selection with --languages."
    end
    puts

    if interactive?
      begin
        menu.query
      rescue Interrupt
        raise Abort
      end
    else
      menu.display_choices
      puts
    end

    @languages = menu.selected_choices.map{ |x| x.downcase.gsub(/\./, '') }
  end

  def interesting_language?(name, command = nil)
    if @languages
      return @languages.include?(name)
    else
      return !!PlatformInfo.find_command(command || name)
    end
  end

  def check_whether_there_are_multiple_apache_installs
    new_screen
    puts '<banner>Sanity checking Apache installation...</banner>'

    output = StringIO.new
    detector = PlatformInfo::ApacheDetector.new(output)
    begin
      detector.detect_all
      detector.report
      @apache2 = detector.result_for(PlatformInfo.apxs2)
      if @apache2.nil?
        render_template 'apache2/apache_install_broken',
          :apxs2 => PlatformInfo.apxs2,
          :sudo_s_e => PhusionPassenger::PlatformInfo.ruby_sudo_shell_command("-E"),
          :ruby => PhusionPassenger::PlatformInfo.ruby_command,
          :passenger_config => "#{PhusionPassenger.bin_dir}/passenger-config"
        return false
      end
      if detector.results.size > 1
        other_installs = detector.results - [@apache2]
        render_template 'apache2/multiple_apache_installations_detected',
          :current => @apache2,
          :other_installs => other_installs
        puts
        if interactive?
          result = prompt_confirmation "Are you sure you want to install " +
            "against Apache #{@apache2.version} (#{@apache2.apxs2})?"
          if !result
            puts
            line
            render_template 'apache2/installing_against_a_different_apache',
              :other_installs => other_installs
          end
          return result
        else
          puts '<yellow>Continuing installation because --auto is given.</yellow>'
          return true
        end
      else
        puts '<green>All good!</green>'
        return true
      end
    ensure
      detector.finish
    end
  end

  def check_whether_apache_uses_compatible_mpm
    # 'httpd -V' output is in the form of:
    #
    # Server MPM:      Prefork     # <--- this line is not always available!
    # ...
    # Server compiled with....
    #  -D APACHE_MPM_DIR="server/mpm/prefork"
    output = PlatformInfo.httpd_V
    output =~ /^Server MPM: +(.*)$/
    if $1
      mpm = $1.downcase
    else
      output =~ /APACHE_MPM_DIR="server\/mpm\/(.*)"/
      if $1
        mpm = $1.downcase
      else
        mpm = nil
      end
    end
    if mpm != "prefork" && mpm != "worker" && mpm != "event"
      new_screen
      render_template 'apache2/apache_must_be_compiled_with_compatible_mpm',
        :current_mpm => mpm
      wait
    end
  end

  def check_write_permission_to_passenger_root
    File.new("__test__.txt", "w").close
    return true
  rescue SystemCallError
    puts
    line
    if Process.uid == 0
      render_template 'installer_common/cannot_access_files_as_root',
        :type => "directory",
        :files => [PhusionPassenger.build_system_dir]
    else
      render_template 'installer_common/run_installer_as_root',
        :dir  => PhusionPassenger.build_system_dir,
        :sudo => PhusionPassenger::PlatformInfo.ruby_sudo_command,
        :sudo_s_e => PhusionPassenger::PlatformInfo.ruby_sudo_shell_command("-E"),
        :ruby => PhusionPassenger::PlatformInfo.ruby_command,
        :installer => "#{PhusionPassenger.bin_dir}/passenger-install-apache2-module #{ORIG_ARGV.join(' ')}"
    end
    return false
  ensure
    File.unlink("__test__.txt") rescue nil
  end

  def check_write_permission_to_web_server_config_files
    return true if !@update_config
    config_file = PlatformInfo.httpd_default_config_file
    return if !config_file || !File.exist?(config_file)

    all_config_files = PlatformInfo.httpd_included_config_files(config_file)
    if all_config_files[:unreadable_files].any?
      puts
      line
      if Process.uid == 0
        render_template 'installer_common/cannot_access_files_as_root',
          :access => "read from",
          :files => all_config_files[:unreadable_files]
      else
        render_template 'installer_common/run_installer_as_root',
          :access => "read from",
          :desc => "an Apache configuration file",
          :sudo => PhusionPassenger::PlatformInfo.ruby_sudo_command,
          :sudo_s_e => PhusionPassenger::PlatformInfo.ruby_sudo_shell_command("-E"),
          :ruby => PhusionPassenger::PlatformInfo.ruby_command,
          :installer => "#{PhusionPassenger.bin_dir}/passenger-install-apache2-module #{ORIG_ARGV.join(' ')}"
      end
      puts
      render_template 'apache2/present_choice_for_no_update_config'
      return false
    end

    unwriteable_files = []
    all_config_files[:files].each do |filename|
      if !File.writable_real?(filename)
        unwriteable_files << filename
      end
    end
    if unwriteable_files.empty?
      return true
    else
      puts
      line
      if Process.uid == 0
        render_template 'installer_common/cannot_access_files_as_root',
          :files => unwriteable_files
      else
        render_template 'installer_common/run_installer_as_root',
          :desc => "an Apache configuration file",
          :sudo => PhusionPassenger::PlatformInfo.ruby_sudo_command,
          :sudo_s_e => PhusionPassenger::PlatformInfo.ruby_sudo_shell_command("-E"),
          :ruby => PhusionPassenger::PlatformInfo.ruby_command,
          :installer => "#{PhusionPassenger.bin_dir}/passenger-install-apache2-module #{ORIG_ARGV.join(' ')}"
      end
      puts
      render_template 'apache2/present_choice_for_no_update_config'
      return false
    end
  end

  def compile_apache2_module
    puts
    line
    puts '<banner>Compiling and installing Apache 2 module...</banner>'
    if @compile
      puts "cd #{PhusionPassenger.build_system_dir}"
      if ENV['TRACE']
        rake = "#{PlatformInfo.rake_command} --trace RELEASE=yes"
      else
        rake = "#{PlatformInfo.rake_command} RELEASE=yes"
      end
      command = "env NOEXEC_DISABLE=1 #{rake} apache2:clean apache2"
      if PhusionPassenger.packaging_method == "homebrew"
        # Running apache2:clean deletes some object files needed
        # by passenger-install-nginx-module, so we ensure those
        # object files are compiled.
        command << " nginx"
      end
      return sh(command)
    else
      puts "Skipping compilation"
      return true
    end
  end

  def load_snippet
    return "LoadModule passenger_module #{PhusionPassenger.apache2_module_path}"
  end

  def config_snippet
    return "<IfModule mod_passenger.c>\n" +
      "  PassengerRoot #{PhusionPassenger.install_spec}\n" +
      "  PassengerDefaultRuby #{PlatformInfo.ruby_command}\n" +
      "</IfModule>"
  end

  def apache2_config_snippets
    return "#{load_snippet}\n#{config_snippet}\n"
  end

  def backup_config_files(config_file, all_config_files)
    now = Time.now.strftime("%Y-%m-%d-%H:%M:%S")
    archive = "#{config_file}.#{GLOBAL_NAMESPACE_DIRNAME}-backup-#{now}.tar.gz"
    backup_files = all_config_files.dup

    # Some people create a regular file in /etc/apache2/mods-enabled, for convenience
    # reasons. This file is not actually managed by a2enmod. We will remove such files
    # because we're going to use mods-eanbled ourselves, so we need to back them up.
    if (dir = PlatformInfo.httpd_mods_enabled_directory) && PlatformInfo.a2enmod
      if File.file?("#{dir}/#{APACHE2_MODULE_CONF_NAME}.load")
        backup_files << "#{dir}/#{APACHE2_MODULE_CONF_NAME}.load"
      end
      if File.file?("#{dir}/#{APACHE2_MODULE_CONF_NAME}.conf")
        backup_files << "#{dir}/#{APACHE2_MODULE_CONF_NAME}.conf"
      end
    end

    puts "Backing up existing configuration files to #{archive}..."
    backup_files.uniq!
    backup_files.map! { |x| x.sub(/^\//, '') }
    Dir.chdir("/") do
      sh! "tar", "-czf", archive, *backup_files
    end
  end

  def uninstall_or_comment_out_existing_config_snippets(all_config_files)
    files_containing_autoinstall_load_blocks = []
    files_containing_autoinstall_conf_blocks = []

    # Some people create a regular file in /etc/apache2/mods-enabled, for convenience
    # reasons. This file is not actually managed by a2enmod. We remove such files
    # because we're going to use mods-enabled ourselves. They've already been backed up.
    if (dir = PlatformInfo.httpd_mods_enabled_directory) && PlatformInfo.a2enmod
      filename = "#{dir}/#{APACHE2_MODULE_CONF_NAME}.load"
      if File.file?(filename) && !File.symlink?(filename)
        puts "Removing #{filename}"
        File.unlink(filename)
      end
      filename = "#{dir}/#{APACHE2_MODULE_CONF_NAME}.conf"
      if File.file?(filename) && !File.symlink?(filename)
        puts "Removing #{filename}"
        File.unlink(filename)
      end
    end

    # Uncomment Phusion Passenger config snippets.
    all_config_files.each do |filename|
      next if !File.exist?(filename)

      contents = File.open(filename, "rb") do |f|
        f.read
      end
      if contents =~ /#{Regexp.escape AUTOINSTALL_BEGIN_LOAD_BLOCK}.*?#{Regexp.escape AUTOINSTALL_END_LOAD_BLOCK}/m
        files_containing_autoinstall_load_blocks << filename
      end
      if contents =~ /#{Regexp.escape AUTOINSTALL_BEGIN_CONF_BLOCK}.*?#{Regexp.escape AUTOINSTALL_END_CONF_BLOCK}/m
        files_containing_autoinstall_conf_blocks << filename
      end
      subst1 = contents.gsub!(/^([ \t]*LoadModule[ \t]+passenger_module[ \t]+.*)$/i, '# \1')
      subst2 = contents.gsub!(/^([ \t]*PassengerRoot[ \t]+.*)$/i, '# \1')
      subst3 = contents.gsub!(/^([ \t]*PassengerDefaultRuby[ \t]+.*)$/i, '# \1')
      if subst1 || subst2 || subst3
        puts "Uninstalling previous #{PROGRAM_NAME} from #{filename}..."
        File.open(filename, "wb") do |f|
          f.write(contents)
        end
      end
    end

    # If there are multiple auto-install comment blocks, remove the duplicates.
    # First, we remove all comment blocks from all files besides the first one.
    if files_containing_autoinstall_load_blocks.size > 1
      files_containing_autoinstall_load_blocks[1..-1].each do |filename|
        puts "Removing duplicate load snippets from #{filename}..."
        remove_comment_blocks(filename,
          AUTOINSTALL_BEGIN_LOAD_BLOCK,
          AUTOINSTALL_END_LOAD_BLOCK)
      end
    end
    if files_containing_autoinstall_conf_blocks.size > 1
      files_containing_autoinstall_conf_blocks[1..-1].each do |filename|
        puts "Removing duplicate conf snippets from #{filename}..."
        remove_comment_blocks(filename,
          AUTOINSTALL_BEGIN_CONF_BLOCK,
          AUTOINSTALL_END_CONF_BLOCK)
      end
    end

    # Then we are left with exactly 0 or exactly 1 file with comment blocks
    # of each type. There may be duplicates inside that single file, so
    # remove duplicates there too.
    if files_containing_autoinstall_load_blocks.size > 0
      filename = files_containing_autoinstall_load_blocks[0]
      puts "Removing duplicate load snippets inside #{filename}..."
      remove_duplicate_comment_blocks(filename,
        AUTOINSTALL_BEGIN_LOAD_BLOCK,
        AUTOINSTALL_END_LOAD_BLOCK)
    end
    if files_containing_autoinstall_conf_blocks.size > 0
      filename = files_containing_autoinstall_conf_blocks[0]
      puts "Removing duplicate configuration snippets inside #{filename}..."
      remove_duplicate_comment_blocks(filename,
        AUTOINSTALL_BEGIN_CONF_BLOCK,
        AUTOINSTALL_END_CONF_BLOCK)
    end

    # One or more files may have been removed, so filter out the ones
    # that are left.
    all_config_files.reject! do |filename|
      !File.exist?(filename)
    end
  end

  def remove_comment_blocks(filename, begin_marker, end_marker)
    contents = File.open(filename, "rb") do |f|
      f.read
    end
    regexp = /#{Regexp.escape begin_marker}.*?#{Regexp.escape end_marker}\n?/m
    contents.gsub!(regexp, '')
    File.open(filename, "wb") do |f|
      f.write(contents)
    end
  end

  def remove_duplicate_comment_blocks(filename, begin_marker, end_marker)
    contents = File.open(filename, "rb") do |f|
      f.read
    end
    regexp = /#{Regexp.escape begin_marker}.*?#{Regexp.escape end_marker}\n?/m
    if m = regexp.match(contents)
      offset = m.end(0)
      rest = contents.slice!(m.end(0) .. -1)
      rest.gsub!(regexp, '')
      contents << rest
      File.open(filename, "wb") do |f|
        f.write(contents)
      end
    end
  end

  def add_new_config_snippets(all_config_files)
    # Look for the file containing the auto-install load and conf comment blocks.
    # The uninstall_or_comment_out_existing_config_snippets method has already
    # guaranteed that there is at most 1 file per comment block type, and that
    # inside each file there are no duplicate comment blocks.
    load_block_file = find_config_file_containing_comment_block(all_config_files,
      AUTOINSTALL_BEGIN_LOAD_BLOCK,
      AUTOINSTALL_END_LOAD_BLOCK)
    conf_block_file = find_config_file_containing_comment_block(all_config_files,
      AUTOINSTALL_BEGIN_CONF_BLOCK,
      AUTOINSTALL_END_CONF_BLOCK)
    if load_block_file && conf_block_file
      puts "Updating #{PROGRAM_NAME} module load snippet inside #{load_block_file}..."
      update_comment_block(load_block_file,
        AUTOINSTALL_BEGIN_LOAD_BLOCK,
        AUTOINSTALL_END_LOAD_BLOCK,
        load_snippet)
      puts "Updating #{PROGRAM_NAME} configuration snippet inside #{conf_block_file}..."
      update_comment_block(conf_block_file,
        AUTOINSTALL_BEGIN_CONF_BLOCK,
        AUTOINSTALL_END_CONF_BLOCK,
        config_snippet)
    elsif !load_block_file && !conf_block_file
      create_load_snippet_file(:maybe_in_mods_available)
      create_conf_snippet_file(:maybe_in_mods_available)
      if PlatformInfo.httpd_mods_available_directory && PlatformInfo.a2enmod
        sh! "#{PlatformInfo.a2enmod} #{APACHE2_MODULE_CONF_NAME}"
      end
    # It looks like either the load or conf file isn't available.
    # If the file that is available is inside mods-available, then it
    # means that the mods-available files are broken.
    elsif is_file_inside_mods_available?(load_block_file, "#{APACHE2_MODULE_CONF_NAME}.load") ||
          is_file_inside_mods_available?(conf_block_file, "#{APACHE2_MODULE_CONF_NAME}.conf")
      # We fix it if a2enmod is available. Otherwise, we remove the block.
      if PlatformInfo.a2enmod
        if !load_block_file
          create_load_snippet_file(:must_be_in_mods_available)
        else
          create_conf_snippet_file(:must_be_in_mods_available)
        end
      else
        if load_block_file
          puts "Removing load snippets from #{filename}..."
          remove_comment_blocks(load_block_file,
            AUTOINSTALL_BEGIN_LOAD_BLOCK,
            AUTOINSTALL_END_LOAD_BLOCK)
        else
          puts "Removing configuration snippets from #{filename}..."
          remove_comment_blocks(conf_block_file,
            AUTOINSTALL_BEGIN_CONF_BLOCK,
            AUTOINSTALL_END_CONF_BLOCK)
        end
        create_load_snippet_file(:must_be_in_mods_available)
        create_conf_snippet_file(:must_be_in_mods_available)
      end
      sh! "#{PlatformInfo.a2enmod} passenger"
    else
      if !load_block_file
        create_load_snippet_file(:not_in_mods_available)
        puts "Updating #{PROGRAM_NAME} configuration snippet inside #{conf_block_file}..."
        update_comment_block(conf_block_file,
          AUTOINSTALL_BEGIN_CONF_BLOCK,
          AUTOINSTALL_END_CONF_BLOCK,
          config_snippet)
      else
        create_conf_snippet_file(:not_in_mods_available)
        puts "Updating #{PROGRAM_NAME} module load snippet inside #{load_block_file}..."
        update_comment_block(load_block_file,
          AUTOINSTALL_BEGIN_LOAD_BLOCK,
          AUTOINSTALL_END_LOAD_BLOCK,
          load_snippet)
      end
    end
  end

  def find_config_file_containing_comment_block(all_config_files, begin_marker, end_marker)
    regexp = /#{Regexp.escape begin_marker}.*?#{Regexp.escape end_marker}/m
    all_config_files.each do |filename|
      contents = File.open(filename, "rb") do |f|
        f.read
      end
      if contents =~ regexp
        return filename
      end
    end
    return nil
  end

  def update_comment_block(filename, begin_marker, end_marker, block_contents)
    regexp = /#{Regexp.escape begin_marker}.*?#{Regexp.escape end_marker}\n?/m
    contents = File.open(filename, "rb") do |f|
      f.read
    end
    if contents.sub!(regexp, "#{begin_marker}\n#{block_contents}\n#{end_marker}\n")
      File.open(filename, "wb") do |f|
        f.write(contents)
      end
      return true
    else
      return false
    end
  end

  def remove_comment_blocks(filename, begin_marker, end_marker)
    regexp = /#{Regexp.escape begin_marker}.*?#{Regexp.escape end_marker}\n?/m
    contents = File.open(filename, "rb") do |f|
      f.read
    end
    contents.gsub!(regexp, "")
    File.open(filename, "wb") do |f|
      f.write(contents)
    end
  end

  def is_file_inside_mods_available?(filename, basename)
    if dir = PlatformInfo.httpd_mods_available_directory
      return filename == "#{dir}/#{basename}"
    else
      return false
    end
  end

  def create_load_snippet_file(where)
    case where
    when :maybe_in_mods_available
      if PlatformInfo.httpd_mods_available_directory && PlatformInfo.a2enmod
        filename = "#{PlatformInfo.httpd_mods_available_directory}/#{APACHE2_MODULE_CONF_NAME}.load"
      else
        filename = PlatformInfo.httpd_default_config_file
      end
    when :must_be_in_mods_available
      if PlatformInfo.httpd_mods_available_directory && PlatformInfo.a2enmod
        filename = "#{PlatformInfo.httpd_mods_available_directory}/#{APACHE2_MODULE_CONF_NAME}.load"
      else
        raise "Apache does not support the mods-available directory"
      end
    when :not_in_mods_available
      filename = PlatformInfo.httpd_default_config_file
    else
      raise ArgumentError
    end

    if File.exist?(filename)
      # If this is a file inside mods-available, and the file didn't have a symlink
      # in mods-enabled, then the uninstall phase did not remove duplicates from this
      # file. So here we remove duplicates again.
      puts "Removing duplicate load snippets inside #{filename}..."
      remove_duplicate_comment_blocks(filename,
        AUTOINSTALL_BEGIN_LOAD_BLOCK,
        AUTOINSTALL_END_LOAD_BLOCK)

      puts "Installing #{PROGRAM_NAME} module load snippet to #{filename}..."
      should_add = !update_comment_block(filename,
        AUTOINSTALL_BEGIN_LOAD_BLOCK,
        AUTOINSTALL_END_LOAD_BLOCK,
        load_snippet)
    else
      puts "Installing #{PROGRAM_NAME} module load snippet to #{filename}..."
      should_add = true
    end
    if should_add
      File.open(filename, "ab") do |f|
        f.write("\n#{AUTOINSTALL_BEGIN_LOAD_BLOCK}\n" +
          "#{load_snippet}\n" +
          "#{AUTOINSTALL_END_LOAD_BLOCK}\n")
      end
    end
  end

  def create_conf_snippet_file(where)
    case where
    when :maybe_in_mods_available
      if PlatformInfo.httpd_mods_available_directory && PlatformInfo.a2enmod
        filename = "#{PlatformInfo.httpd_mods_available_directory}/#{APACHE2_MODULE_CONF_NAME}.conf"
      else
        filename = PlatformInfo.httpd_default_config_file
      end
    when :must_be_in_mods_available
      if PlatformInfo.httpd_mods_available_directory && PlatformInfo.a2enmod
        filename = "#{PlatformInfo.httpd_mods_available_directory}/#{APACHE2_MODULE_CONF_NAME}.conf"
      else
        raise "Apache does not support the mods-available directory"
      end
    when :not_in_mods_available
      filename = PlatformInfo.httpd_default_config_file
    else
      raise ArgumentError
    end

    if File.exist?(filename)
      # If this is a file inside mods-available, and the file didn't have a symlink
      # in mods-enabled, then the uninstall phase did not remove duplicates from this
      # file. So here we remove duplicates again.
      puts "Removing duplicate configuration snippets inside #{filename}..."
      remove_duplicate_comment_blocks(filename,
        AUTOINSTALL_BEGIN_CONF_BLOCK,
        AUTOINSTALL_END_CONF_BLOCK)

      puts "Installing #{PROGRAM_NAME} module configuration snippet to #{filename}..."
      should_add = !update_comment_block(filename,
        AUTOINSTALL_BEGIN_CONF_BLOCK,
        AUTOINSTALL_END_CONF_BLOCK,
        config_snippet)
    else
      puts "Installing #{PROGRAM_NAME} module configuration snippet to #{filename}..."
      should_add = true
    end
    if should_add
      File.open(filename, "ab") do |f|
        f.write("\n#{AUTOINSTALL_BEGIN_CONF_BLOCK}\n" +
          "#{config_snippet}\n" +
          "#{AUTOINSTALL_END_CONF_BLOCK}\n")
      end
    end
  end

  def install_apache2_config_snippets
    if !@update_config
      show_apache2_config_snippets
      return true
    end

    config_file = PlatformInfo.httpd_default_config_file
    if config_file && File.exist?(config_file)
      puts
      line
      puts "<banner>Updating Apache configuration files...</banner>"
      config_file = PlatformInfo.httpd_default_config_file
      all_config_files = PlatformInfo.httpd_included_config_files(config_file)[:files]
      backup_config_files(config_file, all_config_files) if @backup_config
      uninstall_or_comment_out_existing_config_snippets(all_config_files)
      add_new_config_snippets(all_config_files)
      return true
    else
      show_apache2_config_snippets
      return true
    end
  end

  def show_apache2_config_snippets
    puts
    line
    render_template 'apache2/config_snippets',
      :snippet => apache2_config_snippets
    wait
  end

  def show_deployment_example
    new_screen
    render_template 'apache2/deployment_example',
      :users_guide_path => users_guide_path,
      :users_guide_url => users_guide_url,
      :phusion_website => PHUSION_WEBSITE,
      :passenger_website => PASSENGER_WEBSITE,
      :languages => @languages
  end

  def show_possible_solutions_for_compilation_and_installation_problems
    new_screen
    render_template 'apache2/possible_solutions_for_compilation_and_installation_problems',
      :users_guide_path => users_guide_path,
      :users_guide_url => users_guide_url,
      :support_url => SUPPORT_URL
  end

  def apache_module_available?
    return File.exist?(PhusionPassenger.apache2_module_path)
  end

  def install_apache_module_from_native_package
    case PhusionPassenger.packaging_method
    when 'deb'
      sh! "sudo apt-get update"
      sh! "sudo apt-get install #{DEB_APACHE_MODULE_PACKAGE}"
      return true
    when 'rpm'
      sh! "sudo yum install #{RPM_APACHE_MODULE_PACKAGE}-#{VERSION_STRING}"
      return true
    else
      puts "<red>The #{PROGRAM_NAME} Apache module package is not installed.</red>"
      puts "Please ask your packager or operating system vendor how to install it."
      return false
    end
  end

  def notify_apache_module_installed
    render_template 'apache2/notify_apache_module_installed'
    wait
  end
end

ORIG_ARGV = ARGV.dup
options = { :compile => true, :update_config => false, :backup_config => true }
parser = OptionParser.new do |opts|
  opts.banner = "Usage: passenger-install-apache2-module [options]"
  opts.separator ""

  indent = ' ' * 37
  opts.separator "Options:"
  opts.on("-a", "--auto", String, "Automatically build the Apache module,\n" <<
          "#{indent}without interactively asking for user\n" <<
          "#{indent}input.") do
    options[:auto] = true
  end
  opts.on("--apxs2-path PATH", String, "Path to 'apxs2' command.") do |value|
    ENV['APXS2'] = value
  end
  opts.on("--apr-config-path PATH", String, "Path to 'apr-config' command.") do |value|
    ENV['APR_CONFIG'] = value
  end
  opts.on("--languages NAMES", "Comma-separated list of interested\n" <<
      "#{indent}languages (e.g.\n" <<
      "#{indent}'ruby,python,nodejs,meteor')") do |value|
    options[:languages] = value.split(",")
  end
  opts.on("--no-compile", "Skip compilation.") do
    options[:compile] = false
  end
  #opts.on("--no-update-config", "Do not automatically update Apache config\n" <<
  #   "#{indent}files.") do
  # options[:update_config] = false
  #end
  #opts.on("--no-backup-config", "When updating Apache config files, do not\n" <<
  #   "#{indent}create backups of the existing files.") do
  # options[:backup_config] = false
  #end
  opts.on("--force-colors", "Display colors even if stdout is not a TTY") do
    options[:colorize] = true
  end
  opts.on("--snippet", "Show just the Apache config snippet.") do
    options[:snippet] = true
  end
end
begin
  parser.parse!
rescue OptionParser::ParseError => e
  puts e
  puts
  puts "Please see '--help' for valid options."
  exit 1
end

installer = Installer.new(options)
if options[:snippet]
  puts installer.send(:apache2_config_snippets)
else
  installer.run
end
