def main
  Raggle::HTML::init_tagset
  
  opts = Raggle::Engine::load_config
  
  
  Raggle::Engine::create_cache_lock if $config['use_cache_lock']
  
  
  $config['raggle_mode'] = opts['mode']
  Raggle::CLI::handle_mode(opts)
  
  puts $config['msg_load_theme']
  if $config['load_theme'] && test(?e, $config['theme_path'])
    $config['theme'] = YAML::load(File::open($config['theme_path']))
  end
  $config['run_http_server'] = true unless $HAVE_LIB['ncurses'] ||
                                           $HAVE_LIB['drb']
  
  $view_source = false
  $update_wins = false
  $old_win = [0]
  
  
  Raggle::Interfaces::NcursesInterface::init unless \
    $config['run_http_server'] || $config['run_drb_server']
  
  
  Raggle::Engine::load_feed_cache if $config['load_feed_cache'] &&
                                     test(?e, $config['feed_cache_path'])
  
  
  Net::HTTP.version_1_1
  
  $feed_thread = Raggle::Engine::start_feed_thread
  
  $save_thread = Raggle::Engine::start_save_thread
  
  if $HAVE_LIB['webrick'] && $config['run_http_server']
    $http_thread = Raggle::Interfaces::HTTPServerInterface::init
  elsif $HAVE_LIB['drb'] && $config['run_drb_server']
    $drb_thread = Raggle::Interfaces::DRbServerInterface::init
  end
  
  Raggle::Engine::set_thread_priorities
  begin
    if $config['run_http_server']
      Raggle::Interfaces::HTTPServerInterface::main_loop
    elsif $config['run_drb_server']
      Raggle::Interfaces::DRbServerInterface::main_loop
    else 
      Raggle::Interfaces::NcursesInterface::main_loop
    end
    
    
    Ncurses::endwin unless $config['run_http_server'] ||
                           $config['run_drb_server']
    
    $feed_thread.exit if $feed_thread.alive?
    
    $save_thread.exit if $save_thread.alive?
    
    Raggle::Engine::save_config
  ensure
    unless $done 
      
      if $config['run_http_server']
        $http_server.shutdown
      elsif $config['run_drb_server']
        nil
      else
        Ncurses::endwin 
      end
      
      
      Raggle::Engine::save_config if $config['save_on_crash']
    end
      
    
    Raggle::Engine::destroy_cache_lock if $config['use_cache_lock'] &&
                                          $config['cache_lock']
    
    $stdout.puts $config['msg_thanks']
  end
end