#!/usr/bin/env ruby
$KCODE='e'
# Thanks:: >>63
# $Id: new_2ch_bbsmenu.rb,v 1.6 2002/05/18 19:07:01 haruyama Exp $
# このファイルはGPLで配付されています.
# このプログラムは無保証です.

require 'connect2ch'
include GetBoardInfo2ch

BASE_DIR = ENV['HOME']+ '/.goRua_2ch/'

config = BASE_DIR + "config"

#デフォルト設定
$config={
  'proxy_addr' => nil,
  'proxy_port' => nil,
  'get_proxy_addr' => nil,
  'get_proxy_port' => nil,
  'post_proxy_addr' => nil,
  'post_proxy_port' => nil,
  'http_get_timeout' => 60,
  'http_command' => "galeon -n %u",
  'tab_string_size' => 7,
  'height' => 600,
  'select_width' => 250,
  'view_width' => 650,
  'post_width' => 600,
  'post_height' => 400,
  'hankaku_to_zenkaku' => nil,
  'thread_tooltips_delay' => 1500,
  'post_name_candidate' => [''],
  'post_mailto_candidate' => ['','sage'],
  'user_agent' => "dummy",
#  'background_get' => nil,
  'show_old_message' => nil,
  'colored_text' => true,
  'color0' => 'black',
  'color1' => 'red',
  'color2' => 'blue',
  'color3' => 'green',
  'set_last_page' => true,
}
begin
  unless File.exist?(config)
    config = "#{GORUA_DATADIR}/config"
  end
  open(config,'r'){|file|
    while line=file.gets
      line=NKF::nkf('-ex',line)
      line.chop!
      if /^\#/ =~ line
	next
      elsif /^([a-z_0-9]+)\s*=\s*([0-9\-][0-9]*)\s*$/ =~ line
	tmp1=$1
	tmp2=$2
	if $config[tmp1].is_a? Fixnum  
	  $config[tmp1]=tmp2.to_i
	elsif !$config[tmp1]
	  $config[tmp1]=tmp2.to_i
	elsif $config[tmp1].is_a? String
	  $config[tmp1]=tmp2.strip
	else
	  STDERR.print "#{$1} に正しくない値が指定されています。\n"
	end
      elsif /^([a-z_0-9]+)\s*=\s*\[(.*)\]\s*$/ =~ line
	if $config[$1].is_a? Array
	  $config[$1]=$2.split(/,/)
	else
	  STDERR.print "#{$1} に配列は指定できません。\n"
	end
      elsif /^([a-z_0-9]+)\s*=\s*(.*)\s*$/ =~ line
	tmp1=$1
	tmp2=$2.strip
	if  $config[tmp1].is_a? String
	  $config[tmp1] = tmp2
	elsif  $config[tmp1].is_a? TrueClass
	  if tmp2 == 'true'
	    $config[tmp1] = true
	  elsif tmp2 == 'false'
	    $config[tmp1] = false
	  else
	    STDERR.print "#{tmp1} には trueかfalseを指定する必要があります。\n"
	  end
	elsif !$config[tmp1]
	  $config[tmp1] = tmp2
	else
	  STDERR.print "#{tmp1} に文字列は指定できません。\n"
	end
      end
    end
  }
rescue
end


# connect2chの設定(0.12以降)
if $config['get_proxy_addr'] && $config['get_proxy_port']
  $connect2ch_config['get_proxy_addr']=$config['get_proxy_addr']
  $connect2ch_config['get_proxy_port']=$config['get_proxy_port']
elsif $config['proxy_addr'] && $config['proxy_port']
  $connect2ch_config['get_proxy_addr']=$config['proxy_addr']
  $connect2ch_config['get_proxy_port']=$config['proxy_port']
end
=begin
if $config['post_proxy_addr'] && $config['post_proxy_port']
  $connect2ch_config['post_proxy_addr']=$config['post_proxy_addr']
  $connect2ch_config['post_proxy_port']=$config['post_proxy_port']
else $config['proxy_addr'] && $config['proxy_port']
  $connect2ch_config['post_proxy_addr']=$config['proxy_addr']
  $connect2ch_config['post_proxy_port']=$config['proxy_port']
end
=end
$connect2ch_config['http_get_timeout']=$config['http_get_timeout']

host,port,path = parse_http_url(ARGV[0])
if host
  body=get_board_info(host,port,path)
  if body
    info=parse_board_info_html(body)
  else
    print "htmlファイルのGETに失敗\n"
  end
else
  info=parse_board_info_html(ARGF)
end
info.each{ |e|
#  print Kconv::kconv(e,Kconv::SJIS,Kconv::EUC)
  print NKF::nkf('-sxE',e)
}
