require 'yaml'
require 'getoptlong'





class Rubum
    Keys = [ 'input_folder',
             'output_folder',
             'theme_folder',
             'exclude_pattern'
                ]
    def initialize args
        
        options = Array.new
        Keys.each do |key|
            options << [GetoptLong::REQUIRED_ARGUMENT, "--#{key}"]
        end
        options << [GetoptLong::OPTIONAL_ARGUMENT,'-g','--regenerate-config']
        options << [GetoptLong::OPTIONAL_ARGUMENT,'-h','--help']
        opt_parser = GetoptLong.new()
        opt_parser.set_options(*options)
        
        post_configuration = Hash.new
        
        opt_parser.each_option{ |option_name, option_argument|
            case option_name
            when /-g/
                regenerate_config option_argument
            when /-h/
                puts Rubum.usage
                exit
            else
                if option_name=~/--(.*)/
                    option = $1
                    post_configuration[option] = option_argument
                else
                    puts "Invalid parameter : #{option_name}"
                    puts Rubum.usage
                    exit
                end
            end
        }
        puts args.inspect
        config_file = ARGV.pop
        if FileTest.exists?(config_file) 
            puts "Configuration file : #{config_file}"
            @configuration = YAML.load(File.open(config_file,File::RDONLY))
            @configuration.update(post_configuration)
            puts @configuration.inspect
            check_configuration
        else
            puts "Non existent file #{config_file}"
        end
    end
    def Rubum.usage
        out =  %Q{ 
Rubum usage
===========
(c) Pierre Baillet 2004
This is Rubum, the Ruby Album Generator.
        
rubum.rb [flags] [config.yaml]
where config.yaml is a configuration file
        
--regenerate-configuration file , -g file  regenerate an empty configuration 
                                in config.yaml

--help, -h                      display this help
}
        Keys.each do |key|
            out = out + "--#{key}=value\n"
        end
        return out

    end
    def regenerate_config param
        if FileTest.exists?(param)
            puts "Cannot overwrite existing file #{param}"
            exit
        end
        puts "Generating template configuration file #{param}"
        config_hash = { "input_folder"=>"c:/put/here/path/to/pictures",
                        "output_folder"=>"d:/put/here/destination/folder",
                        "theme_folder"=>"e:/put/here/theme/folder",
                        "exclude_pattern"=>"^$",
                        "use_gtk"=>true,
                    }
        if param==""
            puts "Please give a filename as parameter."
            exit
        end
        File.open(param,File::RDWR|File::CREAT) { |f|
            f.write config_hash.to_yaml
        }
        puts "Done."
        exit
    end
    def check_configuration
        Keys.each{ |long_key|
            raise ConfigException.new("Missing Configuration key \"#{long_key}\"") if !@configuration.has_key?(long_key)
        }
        puts @configuration.inspect
        if @configuration['use_gtk'] == false
            $USE_GTK = false
        else
            $USE_GTK = true
        end
    end
    require 'find'    
    def start
        current_files = Array.new
        Find.find(@configuration['output_folder']) do |file|
            current_files << file
        end
        Image.setup_data_cache()
        begin  
            Folder.set_exclude_pattern(@configuration['exclude_pattern'])
            Folder.set_template_folder(@configuration['theme_folder'])
            Image.set_template_folder(@configuration['theme_folder'])
            
            f = Folder.new(@configuration['input_folder'])
            f.set_output_folder(@configuration['output_folder'])
            f.set_base_output_folder(@configuration['output_folder'])
            puts "Rendering"
            f.render
        ensure
            Image.save_cache()
        end
    end

    class ConfigException < Exception
        def initialize(error)
            super(%Q{Configuration Exception \n#{error}\n\n})
        end
    end
end

if __FILE__ == $0
    st = Time.now
    r = Rubum.new(ARGV)
    require 'core'
    r.start
    puts "Done, processed #{Image.access} pictures in #{Time.now - st} seconds."
end
