#!/usr/bin/ruby
# vim:set foldmethod=indent:
# vim:set sts=4:set sw=4:set ts=4:
# vim:set et:


require 'jfif'
require 'exif'
require 'cgi'

include JFIF
include EXIF

# switch to UTF-8
$KCODE="U"

require 'windowutils.rb'

module Utils
    # give the new size of the image with correct ratio
    # ow, oh are the original dimensions of the picture
    # nw, nh, the maximum available
    def Utils.new_size( ow, oh, nw, nh)
        if nw/nh < ow/oh
            out_w = nw
            out_h = nw*oh/ow
        else
            out_h = nh
            out_w = ow*nh/oh
        end
        [out_w.to_i, out_h.to_i]
    end
end

# extra input methods for the configuration
module InputHelper
    class Textarea
        attr_reader :text
        def initialize(content)
            @text = content
        end
    end
end


# first read the configuration
begin
    require "conf-#{File.basename(__FILE__)}"
rescue SyntaxError=>e
    puts "Sorry configuration file is buggy:\n"+e
    puts "Starting with defaults"
    module Conf
        REDRAW_INTERVAL = 0
    end
end


class PhotoRDFApplication
    def initialize arguments
        @set_value = Hash.new
        @images = Array.new
        dump = false
        
        command = false
        arguments.each do |argument|
            case argument
            when /--help/
                display_help
                exit
            when /--view/
                dump = true
                command = true
            when /--set\(([^=]+)\=([^)]+)\)/
                command = true
                @set_value[$1] = $2
            else
                @images << argument        
            end
        end
        # no command, output the help...
        if command == false
            display_help
            exit
        end
        if dump
            puts "Dumping file content:"
            @images.each do |image|
                puts "Image: #{image}"
            end
        end
    end
    def display_help
            puts help
    end
    def help
        return <<EOHELP
    Usage:
            ruby photordf.rb [options] [files]
    Option can be:
            --help           summon this help
            --set properties=value set a propertie to value value
            --list namespace list available properties for this namespace
EOHELP
    end
    
    def PhotoRDFApplication.newPhotoRdf(args)
        if args.length > 0
            PhotoRDFApplication.new(ARGV)
        else
            require 'gtk2'
            require 'glib2'
            require 'gtk_photordf'
        
            Gtk.init
            window = PhotoRDF.new nil
            window.set_title("Photo RDF Editor").set_default_size(640,480)
            window.show_all
        
            Gtk.main
        end
    end
end


if $0 == __FILE__
    PhotoRDFApplication.newPhotoRdf(ARGV)
end


