#!/usr/bin/env ruby

# TODO: Move this into a YAML config?
API_KEY = '5fcb8b7041d5c32c7e1e60dc076989ba'

require File.expand_path(
    File.join(File.dirname(__FILE__), '..', 'lib', 'pingfm'))

keyloader = ::Pingfm::Keyloader.new
unless keyloader.has_keys?
  keyloader.api_key = API_KEY
  puts 'Enter your Ping.fm User API key (http://ping.fm/key/): '
  keyloader.app_key = STDIN.gets.chomp
  keyloader.save
end

# post message from ARGV

pingfm = ::Pingfm::Client.new(keyloader.api_key, keyloader.app_key)

s = pingfm.validate
if s['status'] == 'OK'
  status = ARGV.join(' ')

  # Might be a good idea to throw an exception here, instead of just bailing.
  if status.nil? || status.empty?
    puts 'Must provide a message to send.'
    exit
  end

  post_result = pingfm.post(status)

  if s['status'] == 'FAIL'
    puts s['message']
  else
    puts 'Message sent.'
  end
else
  puts s['message']
end

# EOF
