#!/usr/bin/env ruby

# Pgm: cui_deactivate_user_acct
# Execution:  [RAILS_ENV=environment] rails r lib/cui_deactivate_user_acct.rb

# [JIRA] (DOM-268) Deactivating User CUI Accounts per VA policy
#         Track users who have not used the system and deactivate accounts.
#         Per VA 6500hbk15.pdf handbook, inactive accounts need to be disabled
#         automatically after 90 days, this OI&T SOP.

root = File.expand_path('../../../../', __FILE__)           # Rails Root
script_root = File.expand_path('../../', __FILE__)          # This scripts Root
common_root = File.expand_path('../../../common', __FILE__) # Shared Common Root

require 'optparse'
require 'fileutils'

require File.join(script_root, %w[lib cui_deactivate_user_account.rb])

require File.join(common_root, %w[lib colorize])
require File.join(common_root, %w[lib message_types])
require File.join(common_root, %w[lib environments_list])

# Put your code here
# --------------------------------------------------------------------------- #
# -------------------------------  Main Line -------------------------------- #
# --------------------------------------------------------------------------- #

current_script = File.basename(__FILE__)          # This script
pid = Process.pid

msg = "#{current_script} #{pid} started"
Rails.logger.info("#{msg}")                       # Log the fact that we started
puts std_msg msg

# Setup some sensible default values for the app
options = { environment: ENV['RAILS_ENV'],
            max_inactive_period: 90,
            current_script: current_script,
            pid: pid
          }

OptionParser.new do |opts|
  msg = "Usage: [RAILS_ENV=environment] rails r #{File.basename($0)} "  \
        "[options]\n       or rails r #{File.basename($0)} [options]."
  opts.banner = "#{msg}"

  opts.on('-x', '--max_inactive_period number_of_days',
          'Maximum number of days of inactivity') do |x|
    options[:max_inactive_period] = x;
  end

  opts.on('-?', '-h', '--help', 'Displays Help') do
    msg = "#{current_script} #{pid} Display help"
    Rails.logger.info("#{msg}")
		puts opts
    msg = "#{current_script} #{pid}  Terminating status with exit code 1"
    Rails.logger.info("#{msg}")
    puts std_msg msg
		exit 1
	end
end.parse!

# CuiDeavtivateUserAccount.new(options).ident

Rails.logger.debug("#{current_script} #{pid} options: #{options.inspect}")
  # begin the process
if CuiDeavtivateUserAccount.new(options).run
  # success
  msg = "#{current_script} #{pid} Terminating status with exit code 0"
  Rails.logger.info(msg)
  puts std_msg msg
  exit 0 # exit let's the system koow the status (Zero is considered success)
else
  # something unexpected happened
  msg = "#{current_script} #{pid} Terminating status with exit code 99"
  Rails.logger.error(msg)
  puts error_msg msg
  exit 99
end
