#!/usr/bin/ruby

require 'mcollective'

include MCollective::RPC

args = {}

options = rpcoptions do |parser, options|
   parser.define_head "Generic File Manager Client"
   parser.banner = "Usage: mc-filemgr [--file FILE] [touch|remove|status]"

   parser.on('-f', '--file MSG', 'file to manage') do |v|
      args[:file] = v
   end

   parser.on('-d', '--details', 'show full file details') do
      options[:details] = true
   end
end

if ARGV.length == 1
    command = ARGV.shift
else
    command = "touch"
end

mc = rpcclient("filemgr", :options => options)

if command == "remove"
    printrpc mc.remove(args)

elsif command == "touch"
    printrpc mc.touch(args)

elsif command == "status"
    if options[:details]
        printrpc mc.status(args)
    else
        mc.status(args).each do |resp|
            printf("%-40s: %s\n", resp[:sender], resp[:data][:output] || resp[:statusmsg] )
        end
    end

else
    puts "Valid commands are 'touch', 'status', and 'remove'"
    exit 1
end

printrpcstats
