#!/usr/bin/ruby


THIS_FILE = File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__
require File.expand_path(File.join(File.dirname(THIS_FILE), '..', 'lib', 's3_html_indexer'))

raise "ERROR: you must define AWS_ACCESS_KEY_ID in you environment. " unless ENV["AWS_ACCESS_KEY_ID"]
raise "ERROR: you must define AWS_SECRET_ACCESS_KEY in you environment. " unless ENV["AWS_SECRET_ACCESS_KEY"]

aws_image_bucket = ARGV.shift || ENV["AWS_IMAGE_BUCKET"]
raise "ERROR: you must pass in the bucket to generate an index for. pass in all to generate index for all buckets" unless aws_image_bucket

if aws_image_bucket.downcase == "all"
  buckets = %w(
    rightscale-rightimage-base
    rightscale-rightimage-base-dev
    rightscale-cloudstack
    rightscale-cloudstack-dev
    rightscale-openstack
    rightscale-openstack-dev
    rightscale-eucalyptus
    rightscale-eucalyptus-dev
    rightscale-ec2
    rightscale-ec2-dev
    rightscale-google
    rightscale-google-dev
    rightscale-azure
    rightscale-azure-dev
  )
else
  buckets = [aws_image_bucket]
end

buckets.each do |b|
  indexer = RightImageTools::S3HtmlIndexer.new(b,  ENV["AWS_ACCESS_KEY_ID"], ENV["AWS_SECRET_ACCESS_KEY"])
  indexer.to_html("index.html")
  indexer.upload_index("index.html")
end
