#!/usr/bin/ruby
#
#  Copyright (C) 2008 Red Hat Inc.
#  
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2.1 of the License, or (at your option) any later version.
#  
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.
#  
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
#
# Author: Bryan Kearney <bkearney@redhat.com>
require "ace"
require "getoptlong"

def usage
	puts("Build Usage: ace OPTIONS ACTION applianceName")
	puts("  Where ACTION is:")
	puts("	  install the initial appliance")		
	puts("	  update the appliance with new packages and recipies")			
	puts("	  refresh the appliance with new context information")			
	puts("	  graph the relationships for the recipe")		
	puts("	  parse the recipe to check for syntax errors")		
	puts("  Where OPTIONS is:")
	puts("	  -d Turn on debug level logging")
	puts("	  -h print help text")
	puts("	  -l logfile Log the output to a given file")	
	puts("	  -v Turn on verbose logging")	
	puts("")
	puts("Query Usage: ace list ITEM")
	puts("  Where ITEM is:")
	puts("    appliances ")		
	puts("    modules")		
	puts("    templates")		
	puts("    resources")		
	puts("    single_execs")			
end

options = [
    [ "--debug",	   "-d",			GetoptLong::NO_ARGUMENT ],
    [ "--help",		   "-h",			GetoptLong::NO_ARGUMENT ],         
    [ "--logfile",	   "-l",			GetoptLong::REQUIRED_ARGUMENT ],   
    [ "--verbose",	   "-v",			GetoptLong::NO_ARGUMENT ],     
]

debug = false
verbose = false
logfile = nil
contextDir = nil

# Add all of the config parameters as valid options
# Store them off
result = GetoptLong.new(*options)

begin
	result.each{ |opt, arg|
		case opt
			when '--debug'
				debug = true
                verbose = true
			when '--verbose'
				verbose = true			
			when '--logfile'
				logfile = arg		
			when '--help'
				usage
				exit(0)			
		end 
	}
rescue GetoptLong::InvalidOption => detail
    $stderr.puts "Invalid parameter!"
	usage    
    exit(1)
rescue GetoptLong::MissingArgument => detail
	usage    
    exit(1)    
end

if ARGV.length != 2 
	puts "Incorrect number of arguments. Execute ace --help for usage"
	exit(1)
end

# Make sure we are root
if  Process.uid != 0 then
	puts "You must be root to execute this script"
	exit(1)
end

ace = ACE.new()
ace.debug = debug
ace.verbose = verbose
ace.log_file = logfile

action=ARGV.shift
modifier=ARGV.shift

case action
	when 'install'
		ace.appliance_name=modifier
		status = ace.install_appliance()
	when 'update'
		ace.appliance_name=modifier
		status = ace.update_appliance()				
	when 'graph'
		ace.appliance_name=modifier
		status = ace.generate_graph()	
	when 'parse'
	    ace.debug = true
		ace.appliance_name=modifier
		status = ace.parse()		
	when 'refresh'
		ace.appliance_name=modifier
		status = ace.refresh_appliance()	
	when 'list'
		if (modifier == "appliances")
			status = ace.list_appliances() 
		elsif (modifier == "modules")
			status = ace.list_modules() 
		elsif (modifier == "updates")
			status = ace.list_updates() 			
		elsif (modifier == "templates")
			status = ace.list_templates() 	
		elsif (modifier == "resources")
			status = ace.list_resources() 		
		elsif (modifier == "single_execs")
			status = ace.list_single_execs() 				
		else 
			puts "You can only list appliances, modules, resources, templates, and single_execs"
			status = 1
		end
end

if (status != 0)
    puts("Command Failed with return code #{status}")
end
exit(status)


