# = TITLE:
#
#   ShellUtils
#
# = COPYING:
#
#   Copyright (c) 2007,2008 Tiger Ops
#
#   This file is part of the Reap program.
#
#   Reap is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   Reap 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 General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with Reap.  If not, see <http://www.gnu.org/licenses/>.

require 'reap/extensions'

module Reap
module Utilities

  module OptionUtils
    attr_accessor :dryrun
    attr_accessor :trace
    attr_accessor :force
    attr_accessor :verbose

    def dryrun?  ; @dryrun  ; end
    def trace?   ; @trace   ; end
    def force?   ; @force   ; end
    def verbose? ; @verbose ; end
  end

  #

  module ShellUtils
    include OptionUtils

    # Internal status report.
    # Only output if dryrun or trace mode.

    def status(message)
      puts message if dryrun? or trace?
    end

    # Shell runner.

    def sh(cmd)
      if dryrun?
        puts cmd
        true
      else
        puts "--> system call: #{cmd}" if trace?
        system(cmd)
      end
    end

    # Convenient method to get simple console reply.

    def ask(question, answers=nil)
      print "#{question}"
      print " [#{answers}] " if answers
      until inp = $stdin.gets ; sleep 1 ; end
      inp.strip
    end

    # Ask for a password. (FIXME: only for unix so far)

    def password(prompt=nil)
      msg ||= "Enter Password: "
      inp = ''

      print "#{prompt} "

      begin
        #system "stty -echo"
        #inp = gets.chomp
        until inp = $stdin.gets
          sleep 1
        end
      ensure
        #system "stty echo"
      end

      return inp.chomp
    end

#     # No fuss access to ARGV. This shows up as #commandline in the reap api.
#     #
#     # Ratch uses '=' for parameterized flags b/c this make parsing stupid simple
#     # and that's a good thing!!! However you can use value! if need be.
#
#     def commandline
#       @commandline ||= ArgVector.new(ARGV)
#     end
#
#     alias_method :argument_vector, :commandline
#
#     # Duplicate of ARGV.
#
#     def argv
#       @argv ||= ARGV.dup
#     end
#
#     # Convert command line argv to args.
#     #
#     # TODO Is this implmented as expected?
#
#     def command_parameters
#       argv.to_params
#     end

#     # Debug mode.
#
#     def debug?
#       @debug ||= %w{--debug}.any?{|a| argv.delete(a)}
#     end
#
#     #
#
#     def verbose?
#       @verbose ||= %w{--verbose}.any?{|a| argv.delete(a)}
#     end
#
#     #
#
#     def verbose!
#       @verbose = true
#     end
#
#     #
#
#     def trace?
#       @trace ||= %w{--trace}.any?{|a| argv.delete(a)}
#     end
#
#     #
#
#     def trace!
#       @trace = true
#     end
#
#     #
#
#     def noharm?
#       @noharm ||= %w{--dryrun --dry-run --noharm}.any?{|a| argv.delete(a)}
#     end
#     alias_method :dryrun?, :noharm? ; module_function :dryrun?
#
#    #
#
#     def noharm!
#       @noharm = true
#     end
#     alias_method :dryrun!, :noharm! ; module_function :dryrun!
#
#     # Force mode.
#
#     def force?
#       @force ||= %w{--force}.any?{|a| argv.delete(a)}
#     end
#
#     def force! ; @force = true ; end

  end

end
end