#use ../lib_provisioning/defs/lists.nu providers_list

export def setup_config_path [
    provisioning_cfg_name: string = "provisioning"
]: nothing -> string {
   ($nu.default-config-dir) | path dirname | path join $provisioning_cfg_name
}
export def tools_install [
  tool_name?: string 
  run_args?: string
]: nothing -> bool {
  print $"(_ansi cyan)($env.PROVISIONING_NAME)(_ansi reset) (_ansi yellow_bold)tools(_ansi reset) check:\n"
  let bin_install = ($env.PROVISIONING | path join "core" | path join "bin" | path join "tools-install")
  if not ($bin_install | path exists) { 
    print $"🛑 Error running (_ansi yellow)tools_install(_ansi reset) not found (_ansi red_bold)($bin_install | path basename)(_ansi reset)"
    if $env.PROVISIONING_DEBUG { print $"($bin_install)" }
    return false
  } 
  let res = (^$"($bin_install)" $run_args $tool_name | complete)
  if ($res.exit_code == 0 ) { 
    print $res.stdout
    true
  } else {
    print $"🛑 Error running (_ansi yellow)tools-install(_ansi reset) (_ansi red_bold)($bin_install | path basename)(_ansi reset)\n($res.stdout)"
    if $env.PROVISIONING_DEBUG { print $"($bin_install)" }
    false
  }
}
export def providers_install [
  prov_name?: string 
  run_args?: string
]: nothing -> list {
  if not ($env.PROVISIONING_PROVIDERS_PATH | path exists) { return } 
  providers_list "full" | each {|prov|
    let name = ($prov | get -o name | default "")
    if ($prov_name | is-not-empty ) and $prov_name != $name { continue }
    let bin_install = ($env.PROVISIONING_PROVIDERS_PATH | path join $name | path join "bin" | path join "install.sh" )
    if not ($bin_install | path exists) { continue } 
    let res = (^$"($bin_install)" $run_args | complete)
    if ($res.exit_code != 0 ) { 
      print ($"🛑 Error running (_ansi yellow)($name)(_ansi reset) (_ansi red_bold)($bin_install | path basename)(_ansi reset)\n($res.stdout)")
      if $env.PROVISIONING_DEBUG { print $"($bin_install)" }
      continue
    }
    print -n $"(_ansi green)($name)(_ansi reset) tools:"
    $prov |  get -o tools | default [] | transpose key value | each {|item| print -n $" (_ansi yellow)($item | get -o key | default "")(_ansi reset)" }
    print ""
    if ($res.exit_code == 0 ) { 
      _print $res.stdout
    }
  }
}
export def create_versions_file [
  targetname: string = "versions" 
]: nothing -> bool {
  let target_name = if ($targetname | is-empty) { "versions" } else { $targetname }
  if ($env.PROVISIONING_PROVIDERS_PATH | path exists) {
    providers_list "full" | each {|prov|
      let name = ($prov | get -o name | default "")
      let prov_versions = ($env.PROVISIONING_PROVIDERS_PATH | path join $name | path join $target_name )
      mut $line = ""
      print -n $"\n(_ansi blue)($name)(_ansi reset) => "
      for item in ($prov | get -o tools | default [] | transpose key value) {
        let tool_name = ($item | get -o key |  default "")
        for data in ($item | get -o value | default {} | transpose ky val) { 
          let sub_name = ($data.ky | str upcase)
          $line += $"($name | str upcase)_($tool_name | str upcase)_($sub_name)=\"($data | get -o val | default "")\"\n" 
        } 
        print -n $"(_ansi yellow)($tool_name)(_ansi reset)"
      }
      $line | save --force $prov_versions
      print $"\n(_ansi blue)($name)(_ansi reset) versions file (_ansi green_bold)($target_name)(_ansi reset) generated"
      if $env.PROVISIONING_DEBUG { _print $"($prov_versions)" }
    }
    _print ""
  } 
  if not ($env.PROVISIONING_REQ_VERSIONS | path exists ) { return false }
  let versions_source = open $env.PROVISIONING_REQ_VERSIONS
  let versions_target = ($env.PROVISIONING_REQ_VERSIONS | path dirname | path join $target_name) 
  if ( $versions_target | path exists) { rm -f $versions_target } 
  $versions_source | transpose key value | each {|it| 
    let name = ($it.key | str upcase)
    mut $line = ""
    for data in ($it.value | transpose ky val) { 
      let sub_name = ($data.ky | str upcase)
      $line += $"($name)_($sub_name)=\"($data.val | default "")\"\n" 
    }
    $line | save -a $versions_target
  }
  print ( 
    $"(_ansi cyan)($env.PROVISIONING_NAME)(_ansi reset) (_ansi blue)core versions(_ansi reset) file " +
    $"(_ansi green_bold)($target_name)(_ansi reset) generated" 
  )
  if $env.PROVISIONING_DEBUG { print ($env.PROVISIONING_REQ_VERSIONS) }
  true
}
