#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../config/environment.rb'
require 'rmagick'
require 'optparse'

options = {}
options[:date_max] = Time.now
options[:med_size] = 1200
options[:thumb_size] = 320

OptionParser.new do |opts|
  opts.banner = "
  Usage: resize_med_thumb [options]
  Default date_max is now, format must be YYYY-MM-DD
  Default med_size is 1200
  Default thumb_size is 320
  "

  opts.on("--date_max string", String, "Date maximum for the selection, is now by default. Format must be YYYY-MM-DD") do |l|
    options[:date_max] = l
  end

  opts.on("--med_size string", String, "size to resize medium pictures, is 1200 by default") do |l|
    options[:med_size] = l
  end

  opts.on("--thumb_size string", String, "size to resize thumb pictures, is 320 by default") do |l|
    options[:thumb_size] = l
  end

end.parse!

nb_processed = 0
med_resized = 0
thumb_resized = 0
nb_errors = 0
nb_images = 0

puts "*** BEGIN - #{Time.now.to_s} : medium_size = #{options[:med_size]}, thumb_size = #{options[:thumb_size]}, date_max = #{options[:date_max]}"
nb_images = Image.where("updated_at < '#{options[:date_max]}'").select(:id).count
puts "#{Time.now.to_s} : nb_images = #{nb_images}"
Image.where("updated_at < '#{options[:date_max]}'").select(:id,:medium_location,:thumb_location,:file_name).find_each do |i|
  error = 0
  # medium resizing
  begin
    img_medium = Magick::Image.read("#{Rails.root}/public#{i.medium_location}").first
    #puts "#{Time.now.strftime('%Y-%m-%d-%Hh%Mm%Ss%L')} : #{i.medium_location} : #{img_medium.columns} x #{img_medium.rows}"
    unless img_medium.rows <= options[:med_size].to_i and img_medium.columns <= options[:med_size].to_i
      img_medium.resize_to_fit!(options[:med_size].to_i,options[:med_size].to_i)
      img_medium.write("#{Rails.root}/public#{i.medium_location}"){ self.quality = 80 }
      #img_medium.destroy!
      puts "#{Time.now.to_s} : #{i.file_name} resized"
      med_resized += 1
    end
  rescue Exception => e
    puts "#{Time.now.to_s} : [ERROR] while resizing #{i.medium_location}. Error : #{e} "
    error += 1
    nb_errors += 1
  end
  # thumb resizing
  begin
    img_thumb = Magick::Image.read("#{Rails.root}/public#{i.thumb_location}").first
    #puts "#{Time.now.strftime('%Y-%m-%d-%Hh%Mm%Ss%L')} : #{i.thumb_location} is #{img_thumb.columns} x #{img_thumb.rows}"
    unless img_thumb.rows <= options[:thumb_size].to_i and img_thumb.columns <= options[:thumb_size].to_i
      img_thumb.resize_to_fit!(options[:thumb_size].to_i,options[:thumb_size].to_i)
      img_thumb.write("#{Rails.root}/public#{i.thumb_location}"){ self.quality = 80 }
      #img_thumb.destroy!
      puts "#{Time.now.to_s} : #{i.thumb_location} resized"
      thumb_resized += 1
    end
  rescue Exception => e
    puts "#{Time.now.to_s} : [ERROR] while resizing #{i.thumb_location}. Error : #{e} "
    error += 1
    nb_errors += 1
  end
=begin
  # save image
  begin
    i.save unless error == 2
    nb_processed += 1
  rescue Exception => e
    puts "#{Time.now.to_s} : [ERROR] while saving #{i.file_name}. Error : #{e} "
    error += 1
    nb_errors += 1
  end
=end
  nb_processed += 1 unless error == 2
  #puts "#{Time.now.to_s} : #{nb_processed} pictures processed / #{nb_images} selected pictures."
end

puts "*** END - #{Time.now.to_s} : #{nb_processed} pictures processed / #{nb_images} selected pictures. #{med_resized} medium pictures resized, #{thumb_resized} thumb pictures resized, #{nb_errors} errors."