#!/usr/bin/env ruby
=begin

RubyGo, a client to play Go on the Internet
Copyright (C) 2002  Jean-François Menon
<jeanfrancois.menon@free.fr>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

=end

def quit
  begin
    $console.quit if $console
    @gowin.close if @gowin

    TkDefaults.save_top_options('go',$root )
    $root.destroy
    Defaults.save_entry
    puts "bye bye"
    exit 0
  rescue
    $stderr.puts $!
    $stderr.puts $@
    exit -1
  end
end

def warn_lang
   #   Misc.warning( "You must restart RubyGo to make the change take effect")
   Misc.warning( TkDefaults::TR["warning.restart.#$language"])
end

$current_path= File.dirname(File.expand_path((__FILE__.gsub(/\\\\/,'/')) ))

puts "RubyGo path is  #$current_path"

$LOAD_PATH.push $current_path

start_command= $0+ ' ' + ARGV.join(' ')



require 'utils/options'
$options = Options.new(
                       "Usage: ruby go.rb
           [--edit -e <filename>]
           [--problem -p <filename>]
           [--connect -c]
           [--gnugo -g]
           [--wallpaper -w]
           [--help -h]
           filename/URL\n",
  [ "--edit", "-e", GetoptLong::OPTIONAL_ARGUMENT ],
  "edit a SGF file",
  [ "--problem", "-p", GetoptLong::OPTIONAL_ARGUMENT ],
  "open a SGF problem",
  [ "--connect","-c", GetoptLong::NO_ARGUMENT],
  "connect directly to the default server",
  [ "--gnugo","-g", GetoptLong::NO_ARGUMENT],
  "play with Gnu Go",
  [ "--wallpaper","-w", GetoptLong::NO_ARGUMENT],
  "use as wallpaper (unix only)",
  [ "--help", "-h", GetoptLong::NO_ARGUMENT],
  "this message")

if $options['--help']
  $options.usage
  exit
end

require 'prolog.rb'

if $options['--wallpaper']
  begin
    require('X11/Display')
    require 'client/wallpaper'
  rescue LoadError
    $stderr.puts("Library X11 not installed on this sytem")
    exit -1
  end
end


$root = TkRoot.new




@lang_menu= Menu.new
@lang_menu.add('radiobutton','label'=> 'Default','variable'=>$language,'value'=>'C','command'=>proc{warn_lang})
@lang_menu.add('radiobutton','label'=> 'Français','variable'=>$language,'value'=>'fr','command'=>proc{warn_lang})
@lang_menu.add('radiobutton','label'=> 'Italiano','variable'=>$language,'value'=>'it','command'=>proc{warn_lang})


@file_menu= Menu.new
@file_menu.add('checkbutton', 'label' => 'Show help balloons' ,
               'variable' => @show_balloons , 'offvalue' => '0' , 'onvalue' => '1',
               'command'=>proc {TkWindow.show_balloons= (@show_balloons.value == '0' ? false : true) })
@file_menu.add('checkbutton', 'label' => 'Sounds' ,
               'variable' => @play_sounds , 'offvalue' => '0' , 'onvalue' => '1',
               'command'=>proc {
  if @play_sounds.value == '1'
    open_sound
  else
    close_sound
  end
  })

@file_menu.add('cascade','menu'=>@lang_menu, 'label' => 'Language')

if Object.const_defined?(:WallPaper)
  @file_menu.add('command', 'label' => 'Hide','command'=> proc {
                 if WallPaper.is_open
                   GoWindow.withdraw_all
                   $root.withdraw
                 else
                   Misc.warning("Hiding all the windows is useful only when RubyGo is used as a  wallpaper")

                 end
  })

end
@file_menu.add('separator')
@file_menu.add('command','label'=>'Quit', 'command'=> proc {quit})


@menubar= Menu.new


@help_menu= TkSysMenu_Help.new(@menubar)
@help_menu.add('command','label'=>'about','command'=>proc { TkHtml.new("file://#{$current_path}/../help/index.html") })
@help_menu.add('command','label'=>'manual','command'=>proc { TkHtml.new("file://#{$current_path}/../help/manual.html") })





@menubar.add('cascade','menu' => @file_menu, 'label' => 'Preferences')
@menubar.add('cascade','menu' => @help_menu, 'label' => 'Help')
$root.configure('menu'=> @menubar)



pane= TexturedPane.new($root,Defaults::DEFS['CommonBackground'],15,15,5,2)


frame1 = TkFrame.new(pane,TkDefaults::GUI['go.frame.frame1'])
frame2 = TkFrame.new(pane,TkDefaults::GUI['go.frame.frame2'])
frame3 = TkFrame.new(pane,TkDefaults::GUI['go.frame.frame3'])
frame4 = TkFrame.new(pane,TkDefaults::GUI['go.frame.frame4'])
frame5 = TkFrame.new(pane,TkDefaults::GUI['go.frame.frame5'])


def create_btn(frame, name,&action)
  attrs=TkDefaults::GUI['go.image.'+name]
  opts= attrs.update('command' => action)
  btn= TkButton.new(frame,opts).pack('fill'=> 'both', 'expand'=>'yes')
  btn.bind_append('Enter',proc{ $root.title(attrs['text'])})
  btn.bind_append('Leave',proc{ $root.title(TkDefaults::GUI['go.top']['title'])})
end

create_btn(frame1,'client'){GoClient.new}
create_btn(frame2,'editor'){GoEditor.new}
create_btn(frame3,'replay'){GoReplay.new}
create_btn(frame4,'gnugo') {GnuGo.new}
create_btn(frame5,'goprobs'){GoProbs.new }


Tk.update

frame1.grid('column'=>0,'row'=>0,'padx'=>5,'pady'=>5)
frame2.grid('column'=>1,'row'=>0,'padx'=>5,'pady'=>5)
frame3.grid('column'=>1,'row'=>1,'padx'=>5,'pady'=>5)
frame4.grid('column'=>0,'row'=>1,'padx'=>5,'pady'=>5)
frame5.grid('column'=>2,'row'=>0,'rowspan'=>2)


pane.pack('fill'=>'both','expand'=>'yes')

TkDefaults.set_top_options('go',$root )
$root.protocol('WM_DELETE_WINDOW', proc{ quit })
$root.wm_command(start_command)


if $options['--edit']
  @gowin =GoEditor.new
  @gowin.open_file($options['--edit']) unless $options['--edit'].empty?
elsif $options['--problem']
  @goprob =GoProbs.new
  @goprob.open_file($options['--problem']) unless $options['--problem'].empty?
elsif $options['--connect']
  gc = GoClient.new(true)
elsif $options['--gnugo']
  $gg = GnuGo.new(ARGV[0])
elsif $options['--wallpaper']
  gc = GoClient.new(true)
  GoWindow.withdraw_all
  $root.withdraw
  WallPaper.new

  Defaults::DEFS['connection_keep_alive']= true

  $wallpaper_mode = true

elsif ARGV[0]
  @gowin= GoReplay.new
  @gowin.open_file(ARGV[0]) unless ARGV[0].empty?

end


Tk.mainloop


