#!/usr/bin/env ruby
#
# Script:      docpath
# Description: Displays the path for a given document
#
# Usage:       docpath --help
#
require 'pathname'
require 'optparse'
BASE = Pathname.new(__FILE__).realpath.parent.parent
$: << BASE + 'lib'
require 'colore'

app = nil
doc_id = nil
storage_dir = nil
config_file = BASE + 'config' + 'app.yml'

OptionParser.new do |opts|
  opts.on( '-a', '--app APP', 'Name of the app' ) { |a| app = a }
  opts.on( '-d', '--doc-id DOCID', 'Document ID' ) { |d| doc_id = d }
  opts.on( '-s', '--storage-dir DIR', 'Storage directory' ) { |d| storage_dir = d }
  opts.on( '-h', '--help', 'This message' ) { puts opts; exit }
  opts.separator ''
  opts.separator 'Examples:'
  opts.separator '    docpath -a myapp -d 123456 -s /opt/storage'
  opts.separator ''
  opts.separator '    docpath -a myapp -d 123456 # will look in config/app.yml for storage dir'
end.parse!

abort 'Specify an app and a doc_id' if app.nil? || doc_id.nil?

unless storage_dir
  abort 'Cannot find configuration file' unless File.exist? config_file
  storage_dir = Colore::C_.storage_directory
end

doc_key = Colore::DocKey.new(app, doc_id)
puts Colore::Document.directory storage_dir, doc_key
