# File lib/rhc/commands/app.rb, line 70
    def create(name, cartridges)
      check_config!

      check_name!(name)

      arg_envs, cartridges = cartridges.partition{|item| item.match(env_var_regex_pattern)}

      rest_domain = check_domain!
      rest_app = nil
      repo_dir = nil

      if options.from_app
        raise RHC::AppCloneNotSupportedException, "The server does not support creating apps based on others (rhc create-app --from-app)." if (!rest_domain.has_param?('ADD_APPLICATION', 'cartridges[][name]') || !rest_domain.has_param?('ADD_APPLICATION', 'cartridges[][url]'))
        raise ArgumentError, "Option --from-code is incompatible with --from-app. When creating an app based on another resource you can either specify a Git repository URL with --from-code or an existing app name with --from-app." if options.from_code     
        raise ArgumentError, "Option --no-dns is incompatible with --from-app. We need to propagate the new app DNS to be able to configure it." if options.dns == false
        raise ArgumentError, "Do not specify cartridges when creating an app based on another one. All cartridges will be copied from the original app." if !(cartridges || []).empty?

        from_app = find_app(:app => options.from_app)

        arg_envs = from_app.environment_variables.collect {|env| "#{env.name}=#{env.value}"} + arg_envs
        cartridges = from_app.cartridges.reject{|c| c.tags.include?('web_proxy')}.collect{|c| c.custom? ? c.url : c.name}
      end

      cartridges = check_cartridges(cartridges, &require_one_web_cart)

      options.default \
        :dns => true,
        :git => true

      raise ArgumentError, "You have named both your main application and your Jenkins application '#{name}'. In order to continue you'll need to specify a different name with --enable-jenkins or choose a different application name." if jenkins_app_name == name && enable_jenkins?

      cart_names = cartridges.collect do |c|
        c.usage_rate? ? "#{c.short_name} (addtl. costs may apply)" : c.short_name
      end.join(', ')

      env = collect_env_vars(arg_envs.concat(Array(options.env)))
      if options.env && env.empty?
        raise RHC::EnvironmentVariableNotProvidedException.new(
        "Environment variable(s) not provided.\n" +
        "Please provide at least one environment variable using the syntax VARIABLE=VALUE. VARIABLE can only contain letters, digits and underscore ('_') and can't begin with a digit.")
      end

      if env.present? && !rest_domain.supports_add_application_with_env_vars?
        env = []
        warn "Server does not support environment variables."
      end

      scaling = options.scaling
      region = options.region
      gear_profile = options.gear_size
      ha = nil

      raise RHC::RegionsAndZonesNotSupportedException if region.present? && !rest_client.supports_regions_and_zones?

      if from_app
        scaling = from_app.scalable if scaling.nil?
        region = from_app.region if region.nil?
        gear_profile = from_app.gear_profile if gear_profile.nil?
        ha = from_app.ha? if !from_app.ha.nil?

        if region.present? && !rest_client.allows_region_selection?
          region = nil
          warn 'Server does not allow selecting regions.  Region is being ignored.'
        end

        cartridges = from_app.cartridges.reject{|c| c.tags.include?('web_proxy')}.collect do |cartridge|
          {
            :name => (cartridge.name if !cartridge.custom?), 
            :url => (cartridge.url if cartridge.custom?),
            :gear_size => options.gear_size || cartridge.gear_profile,
            :additional_gear_storage => (cartridge.additional_gear_storage if cartridge.additional_gear_storage > 0),
            :scales_from => (cartridge.scales_from if scaling && cartridge.scalable?),
            :scales_to => (cartridge.scales_to if scaling && cartridge.scalable?)
          }.reject{|k,v| v.nil? }
        end
      end

      paragraph do
        header "Application Options"
        say table([["Domain:", options.namespace],
               ["Cartridges:", cart_names],
              (["Source Code:", options.from_code] if options.from_code),
              (["From app:", from_app.name] if from_app),
               ["Gear Size:", options.gear_size || (from_app ? "Copied from '#{from_app.name}'" : "default")],
               ["Scaling:", (scaling ? "yes" : "no") + (from_app && options.scaling.nil? ? " (copied from '#{from_app.name}')" : '')],
              (["HA:", (ha ? "yes" : "no") + (from_app ? " (copied from '#{from_app.name}')" : '')] if ha.present?),
              (["Environment Variables:", env.map{|item| "#{item.name}=#{item.value}"}.join(', ')] if env.present?),
              (["Region:", region + (from_app && options.region.nil? ? " (copied from '#{from_app.name}')" : '')] if region),
              ].compact
             )
      end

      paragraph do
        say "Creating application '#{name}' ... "

        # create the main app
        rest_app = create_app(name, cartridges, rest_domain, gear_profile, scaling, options.from_code, env, options.auto_deploy, options.keep_deployments, options.deployment_branch, options.deployment_type, region, ha)
        success "done"

        paragraph{ indent{ success rest_app.messages.map(&:strip) } }
      end

      build_app_exists = rest_app.building_app

      if enable_jenkins?

        unless build_app_exists
          paragraph do
            say "Setting up a Jenkins application ... "

            begin
              build_app_exists = add_jenkins_app(rest_domain)

              success "done"
              paragraph{ indent{ success build_app_exists.messages.map(&:strip) } }

            rescue Exception => e
              warn "not complete"
              add_issue("Jenkins failed to install - #{e}",
                        "Installing jenkins and jenkins-client",
                        "rhc create-app jenkins jenkins-1",
                        "rhc add-cartridge jenkins-client -a #{rest_app.name}")
            end
          end
        end

        paragraph do
          messages = []
          add_jenkins_client_to(rest_app, messages)
          paragraph{ indent{ success messages.map(&:strip) } }
        end if build_app_exists
      end

      debug "Checking SSH keys through the wizard"
      check_sshkeys! unless options.no_keys

      if options.dns
        paragraph do
          say "Waiting for your DNS name to be available ... "
          if dns_propagated? rest_app.host
            success "done"
          else
            warn "failure"
            add_issue("We were unable to lookup your hostname (#{rest_app.host}) in a reasonable amount of time and can not clone your application.",
                      "Clone your git repo",
                      "rhc git-clone #{rest_app.name}")

            output_issues(rest_app)
            return 0
          end
        end
      end

      if from_app
        say "Setting deployment configuration ... "
        rest_app.configure({:auto_deploy => from_app.auto_deploy, :keep_deployments => from_app.keep_deployments , :deployment_branch => from_app.deployment_branch, :deployment_type => from_app.deployment_type})
        success 'done'

        snapshot_filename = temporary_snapshot_filename(from_app.name)
        save_snapshot(from_app, snapshot_filename)
        restore_snapshot(rest_app, snapshot_filename)
        File.delete(snapshot_filename) if File.exist?(snapshot_filename)

        paragraph { warn "The application '#{from_app.name}' has aliases set which were not copied. Please configure the aliases of your new application manually." } unless from_app.aliases.empty?
      end

      if options.git
        section(:now => true, :top => 1, :bottom => 1) do
          begin
            if has_git?
              repo_dir = git_clone_application(rest_app)
            else
              warn "You do not have git installed, so your application's git repo will not be cloned"
            end
          rescue RHC::GitException => e
            warn "#{e}"
            unless RHC::Helpers.windows? and windows_nslookup_bug?(rest_app)
              add_issue("We were unable to clone your application's git repo - #{e}",
                        "Clone your git repo",
                        "rhc git-clone #{rest_app.name}")
            end
          end
        end
      end

      output_issues(rest_app) if issues?

      paragraph do
        say "Your application '#{rest_app.name}' is now available."
        paragraph do
          indent do
            say table [
                ['URL:', rest_app.app_url],
                ['SSH to:', rest_app.ssh_string],
                ['Git remote:', rest_app.git_url],
                (['Cloned to:', repo_dir] if repo_dir)
              ].compact
          end
        end
      end
      paragraph{ say "Run 'rhc show-app #{name}' for more details about your app." }

      0
    end