#!/usr/bin/env ruby

require 'rubygems'
require 'clamp'

begin
  require 'prm'
rescue LoadError
  $: << File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
  require 'prm'
end

class Main < Clamp::Command
  option ["-v", "--version"], :flag, "Version information"
  option ["-t", "--type"], "TYPE", "Type of repo to create"
  option ["-p", "--path"], "PATH", "Path to repo location"
  option ["-r", "--release"], "RELEASE", "OS version to create"
  option ["-a", "--arch"], "ARCH", "Architecture of repo contents"
  option ["-c", "--component"], "COMPONENT", "Component to create [DEB ONLY]"
  option ["-l", "--label"], "LABEL", "Label for generated repository [DEB ONLY]"
  option ["-o", "--origin"], "ORIGIN", "Origin for generated repository [DEB ONLY]"
  option ["-u", "--upload"], "UPLOAD", "Upload your repository to a S3 compatible object store [DEB ONLY]"
  option ["--nocache"], :flag, "Don't cache md5 sums [DEB ONLY]"
  option ["--accesskey"], "ACCESS KEY", "DHO/S3 Access Key", :default => false
  option ["--secretkey"], "SECRET KEY", "DHO/S3 Secret Key", :default => false
  option ["-d", "--directory"], "DIRECTORY", "Move packages from directory to target", :default => false
  option ["-s", "--snapshot"], "COMPONENT", "Creates a snapshot of a component", :default => false
  option ["-e", "--recent"], :flag, "Snapshot the most recent unique packages", :default => false
  option ["-g", "--generate"], :flag , "[DEPRECATED 0.2.4]" do
	  warn "WARNING: Generate option (--generate) is deprecated\n" +
	  "This option will be removed in next stable release (0.3.0)\n"
  end
  option ["-k", "--gpg"], "GPG KEY",  "Sign release files with this GPG key", :default => false

  def execute
	if version?
		puts "PRM version: 0.2.13\n"
		exit
	end

	unless type && path && release && arch
		puts "Type, Path, Release and Arch are required arguements"
		exit
	end

	if recent? && !snapshot
		raise "--snapshot is required for --recent\n"
	end

	if type == "deb" && !component
		raise "--deb option required for the --component option!"
	end

    r = PRM::Repo.new
    r.release = release
    r.arch = arch
    r.type = type
    r.path = path
    r.label = label
    r.origin = origin
	unless upload.nil?
		r.upload = upload
	end
    unless gpg.nil?
      r.gpg = gpg
    end
    unless accesskey.nil?
      r.accesskey = accesskey
    end
    unless secretkey.nil?
      r.secretkey = secretkey
    end
    unless snapshot.nil?
      r.snapshot = snapshot
    end
	unless component.nil?
	  r.component = component
	end
	if recent?
	  r.recent = recent?
	end
    unless directory.nil?
      r.directory = directory
    end
	if nocache?
		r.nocache = nocache?
	end

    r.create
  end
end

Main.run
