#!/usr/bin/env ruby
# frozen_string_literal: true

# Ruby internal
# Project internal
require 'aspisec'
# External
require 'docopt'
require 'pastel'

paint = Pastel.new(eachline: "\n")

doc = <<~DOCOPT
  #{paint.decorate('AspiSec', :red)} v#{paint.decorate(Aspisec::VERSION, :bold)}

  #{paint.decorate('Usage:', :red)}
    aspisec [options] clean
    aspisec [options] list
    aspisec -h | --help
    aspisec --version

  #{paint.decorate('Commands:', :red)}
    clean                   Removes the traces left by offensive security tools
    list                    List available modules, locations and their status

  #{paint.decorate('Options:', :red)}
    --debug                 Display arguments
    -v, --verbose <level>   Set verbosity level (see documentation) [default: 2]
    -h, --help              Show this screen
    --version               Show version

  #{paint.decorate('Examples:', :red)}
    aspisec clean

  #{paint.decorate('Project:', :red)}
    #{paint.decorate('source', :underline)} (https://github.com/noraj/aspisec)
    #{paint.decorate('documentation', :underline)} (https://noraj.github.io/aspisec)
DOCOPT

begin
  args = Docopt.docopt(doc, version: Aspisec::VERSION)
  puts args if args['--debug']
  log_level = 2
  log_level = args['--verbose'].to_i if args['--verbose']
  if args['clean']
    logger = Aspisec::Logger.new(log_level).logger
    conf = Aspisec::Config.new(logger).conf
    cl = Aspisec::Clean.new(conf:, logger:)
    cl.clean
  elsif args['list']
    Aspisec::Modules.modules.each do |mod|
      enabled = mod.enabled? ? '✅' : '❌'
      print "#{enabled} : #{paint.decorate(mod.name, :red, :on_black)}".ljust(42)
      last_index = mod.locations.size - 1
      mod.locations.each_with_index do |loc, i|
        enabled = loc.enabled? ? '✅' : '❌'
        print "  #{enabled} #{paint.decorate(loc.name, :white, :on_black)}".ljust(27)
        puts if i == last_index
      end
    end
  end
rescue Docopt::Exit => e
  puts e.message
end
