#!/usr/bin/env ruby

require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))

require 'slop'

opts = Slop.parse do |o|
  o.on '-f', '--filename=', 'the file to import'
  o.on '-p', '--profile=', 'the market profile to use. '
  #o.on '-e', '--error_audit=', 'the file to which the errors will be directed'

  # Use profile instead of these. Profile is the market subdomain name (e.g. birite, greenleaf)

  # o.on '-m', '--market_id=', 'the market id these products are imported to'
  # o.on '-o', '--organization_id=', 'the organization to associate the products with'
end

opts = opts.to_hash
vendor_file = "/tmp/temp_vendor_data_#{opts[:profile]}"
#vendor_file = "#{opts[:filename]}"

if ARGV.empty?
  puts "USAGE: import_products import_type [options] 2> errors.yml > out.lodex.csv"
  exit 1
end

if opts[:filename] =~/^(http)/
	if system("curl -L -o #{vendor_file} '#{opts[:filename]}' ")
		opts[:filename] = vendor_file
	else
		puts "This file upload failed."
		exit 1
	end
end

import_type = ARGV.first
importer = ProductImport::FileImporters.instantiate(import_type, opts)

importer.load_products

