#!/usr/bin/env nu 
# Info: Script to run Provisioning
# Author: JesusPerezLorenzo 
# Release: 1.0.4
# Date: 22-2-2024

use lib_provisioning/utils/interface.nu * 
use lib_provisioning/utils/error.nu * 
use lib_provisioning/setup/utils.nu setup_config_path

use main_provisioning/ops.nu * 
use lib_provisioning/context.nu *
use lib_provisioning/setup *
use lib_provisioning/utils *
# use ../../providers/prov_lib/create_middleware.nu make_middleware

# Load $env settings  
use env.nu *


# - > Help on Setup 
export def "main help" [
  --src: string = ""
  --notitles       # not tittles
  --out: string    #  Print Output format: json, yaml, text (default)
]: nothing -> nothing {
  if $notitles == null or not $notitles { show_titles } 
  ^$"($env.PROVISIONING_NAME)" -mod setup --help
  if ($out | is-not-empty) { $env.PROVISIONING_NO_TERMINAL = false }
  print (provisioning_setup_options)
  if not $env.PROVISIONING_DEBUG { end_run "" } 
}

def main [
  ...args: string  # Other options, use help to get info 
  --debug (-x)     # Use Debug mode
  --notitles       # not tittles
  --context        # install config context
  --helpinfo (-h)  # For more details use options "help" (no dashes) 
  --out: string    #  Print Output format: json, yaml, text (default)
]: nothing -> nothing {
  if ($out | is-not-empty) {
    $env.PROVISIONING_OUT = $out 
    $env.PROVISIONING_NO_TERMINAL = true
  }
  provisioning_init $helpinfo "setup" $args
  $env.PROVISIONING_DEBUG = if $debug { true } else { false }
  let task = if ($args | length) > 0 { ($args| get 0) } else { "" } 
  let ops = if ($args | length) > 0 { 
    ($args| skip 1) 
  } else {
     ( $"($env.PROVISIONING_ARGS? | default "") " | str replace $"($task) " ""
      | str trim | split row " ")
  } 
  let str_ops = ($ops | str join " " | str trim)
  match $task {
    "h" | "help" => { 
       exec $"($env.PROVISIONING_NAME)" -mod  help --notitles
    },
    "providers" => { 
      let name = if ($args | length) > 0 { ($ops | get -i 0 | default "") } else { "" }
      let run_args = ($ops | skip 1 | str join " ")
      if  $name == "check" {
        providers_install "" $"($name) ($run_args)"
      } else { 
        providers_install $name ($run_args)
      }
    },
    "tools" => { 
      let name = if ($args | length) > 0 { ($ops | get -i 0 | default "") } else { "" }
      let run_args = ($ops | skip 1 | str join " ")
      if  $name == "check" {
        tools_install "" $"($name) ($run_args)"
      } else { 
        tools_install $name ($run_args)
      }
    },
    "versions" => { 
      let res = (create_versions_file $str_ops)
    },
    "middleware" => { 
      make_middleware 
      print $"(_ansi green)middleware(_ansi reset) has been created in (_ansi default_dimmed)($env.PROVISIONING_PROVIDERS_PATH | path join "prov_lib")(_ansi reset)"
    },
    "context" => { 
      install_config $str_ops "provisioning" --context
    },
    "defaults" => { 
      install_config $str_ops "provisioning" 
    },
    "" => { 
      print $"\n(_ansi blue)($env.PROVISIONING_NAME) setup(_ansi reset) requires option.\nUse 'help' to see options."
    },
    _ => { 
      print $"🛑 Error option (_ansi blue)($env.PROVISIONING_NAME) setup(_ansi reset) (_ansi red_bold)($task)(_ansi reset) ($str_ops)"
    },
  } 
  if not $env.PROVISIONING_DEBUG { end_run "" } 
} 
