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

#use std  # assert
use std log

use lib_provisioning * 

use env.nu *

#Load all main defs  
use clusters *

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

# >  Cluster services
def main [
  ...args: string  # Other options, use help to get info 
  -v               # Show version
  -i               # Show Info 
  --version (-V)   # Show version with title
  --info (-I)      # Show Info  with title
  --about (-a)     # Show About
  --infra (-i): string  # Infra directory 
  --settings (-s): string # Settings path 
  --serverpos (-p): int   # Server position in settings 
  --yes (-y)       # Confirm task
  --check (-c)     # Only check mode no servers will be created 
  --wait (-w)      # Wait servers to be created 
  --select: string # Select with cluster as option 
  --onsel: string  # On selection: e (edit) | v (view) | l (list)
  --debug (-x)     # Use Debug mode
  --xm             # Debug with PROVISIONING_METADATA 
  --xc             # Debuc for cluster and services locally PROVISIONING_DEBUG_CHECK 
  --xr             # Debug for remote servers PROVISIONING_DEBUG_REMOTE 
  --xld            # Log level with DEBUG PROVISIONING_LOG_LEVEL=debug 
  --nc             # Not clean working settings 
  --metadata       # Error with metadata (-xm)
  --notitles       # Do not show banner titles
  --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 "cluster" $args
  if $version or $v { ^$env.PROVISIONING_NAME -v ; exit }
  if $info or $i { ^$env.PROVISIONING_NAME -i ; exit }
  if $about { 
    #use defs/about.nu [ about_info ]
    _print (get_about_info)  
    exit 
  }
  if $debug { $env.PROVISIONING_DEBUG = true } 
  if $metadata { $env.PROVISIONING_METADATA = true } 
  # for $arg in $args { print $arg  }
  let task = if ($args | length) > 0 { ($args| get 0) } else { "" } 
  let ops = $"($env.PROVISIONING_ARGS? | default "") " | str replace $"($task) " "" | str trim
  match $task {
    "h" => { 
      exec ($env.PROVISIONING_NAME) "-mod" "cluster" "help" "--notitles"
    },
    "ssh" => {
      #use servers/ssh.nu *
      #use utils/settings.nu *
      #let curr_settings = (find_get_settings --infra $infra --settings $settings)
      #server_ssh $curr_settings "" "pub"
      exec ($env.PROVISIONING_NAME) "-mod" "server" "status" ...($ops | split row " ") --notitles
    }
    "sed" => { 
      if $ops == "" {
        (throw-error $"🛑 No file found" $"for (_ansi yellow_bold)sops(_ansi reset) edit")
        exit 1
      } else if ($ops | path exists) == false {
        (throw-error $"🛑 No file (_ansi green_italic)($ops)(_ansi reset) found" $"for (_ansi yellow_bold)sops(_ansi reset) edit")
        exit 1
      }
      if $env.PROVISIONING_SOPS? == null  {
        let curr_settings = (find_get_settings --infra $infra --settings $settings)
        $env.CURRENT_INFRA_PATH = $"($curr_settings.infra_path)/($curr_settings.infra)"
        use sops_env.nu 
      }
      #use sops on_sops
      on_sops "sed" $ops
    },
    "c" | "create" => { 
      exec ($env.PROVISIONING_NAME) "-mod" "cluster" "create" ...($ops | split row " ") --notitles
    }
    "d" | "delete"  => { 
      exec ($env.PROVISIONING_NAME) "-mod" "cluster" "delete" ...($ops | split row " ") --notitles
    }
    "g" | "generate"  => { 
      exec ($env.PROVISIONING_NAME) "-mod" "cluster" "generate" ...($ops | split row " ") --notitles
    }
    "list" => { 
      #use defs/lists.nu on_list
      on_list "clusters" ($onsel | default "") ""
    },
    "qr" => { 
      #use utils/qr.nu *
      make_qr
    },
    _ => { 
      invalid_task "cluster" $task --end
    },
  } 
  if not $env.PROVISIONING_DEBUG { end_run "" } 
}