#Required -RunAsAdministrator
#Requires -Version 5
<#
.SYNOPSIS
    This Script will Install Yggdrasil at Windows Computers
.DESCRIPTION
    This Script will Install Yggdrasil at Windows Computers
.NOTES
    File name: NIT.Install-YGGWindows.ps1
    VERSION: 1.0.0a
    AUTHOR: New Internet Technologies Inc.
    Created:  2026-07-22
    Licensed under the BSD license.
    Please credit me if you find this script useful and do some cool things with it.
.VERSION HISTORY:
    1.0.0 - (2026-07-22) Script created
    1.0.1 - 
#>


###########################################################
# NIT.Install-YGGWindows.ps1
# This Script will Install Yggdrasil at Windows Computers
# Designed specially for a Windows 11
# 
# The Script has no arguments
# Script must be run with Elevated Privileges and needs
# Powershell 5.0 or higher.
#
###########################################################

# Set Metadata

$PRODUCTNAME="nit-incendiary"
$SUBPRODUCTNAME="YGG-WINDOWS"
$FIRMNAME="NIT"

# Set Variables

# Set Directories
$ZLOVREDDIR="C:\pub1\Distrib\Zlovred"
$CHOCODIR= Join-Path $env:ALLUSERSPROFILE -ChildPath "chocolatey\bin"
$YGGCONFDIR = Join-Path -Path $env:ALLUSERSPROFILE -ChildPath "Yggdrasil"

$scriptPath = $MyInvocation.MyCommand.Path
$scriptDirectory = Split-Path -Path $scriptPath -Parent


if( -not (Test-Path -Path $ZLOVREDDIR -PathType Container )) {
    Write-Host "Error! Directory $ZLOVREDDIR is not found"
    # Exit 1
}

if( -not (Test-Path -Path $CHOCODIR -PathType Container )) {
    Write-Host "Error! Directory $CHOCODIR is not found"
    Exit 1
}

$CHOCOEXE = Join-Path -Path $CHOCODIR -ChildPath "choco.exe"

if( -not (Test-Path -Path $CHOCOEXE -PathType Leaf )) {
    Write-Host "Error! The File $CHOCOEXE is not found"
    Exit 1
}

# Run Payloads
& $CHOCOEXE install -y yggdrasil

if( -not (Test-Path -Path $YGGCONFDIR -PathType Container )) {
    Write-Host "The Directory $YGGCONFDIR is not found!"
    Exit 1
}
else {

    $YGGKEYFILE = Join-Path -Path $YGGCONFDIR -ChildPath "yggdrasil.key"

    $wc = New-Object System.Net.WebClient
    ## $YGGCONFURL="https://raspi4.technic.netcraze.link/payloads/yggdrasil/yggdrasil.conf.template"
    $YGGPEERURL="http://win.netip4.ru/Apps/Zlovred/yggdrasil/DownloadFile.ashx?file=peers.template"

    $newSubStringPath = Join-Path $YGGCONFDIR -ChildPath "peers.template"
    $YGGCONFIGOLDFILE = Join-Path -Path $YGGCONFDIR -ChildPath "yggdrasil.conf.initial"
    $YGGCONFIGFILE = Join-Path -Path $YGGCONFDIR -ChildPath "yggdrasil.conf"
    $oldString = "  Peers: []"



    # Stop Yggdrasil Service
    & net.exe stop yggdrasil

    & yggdrasil -genconf > $YGGCONFIGFILE
    if( -not (Test-Path -Path $YGGCONFIGOLDFILE -PathType Leaf )) {
        Copy-Item $YGGCONFIGFILE $YGGCONFIGOLDFILE
    }

    $wc.DownloadFile($YGGPEERURL, $newSubStringPath)

    $newSubString = Get-Content -Path $newSubStringPath -Raw
    (Get-Content -Path $YGGCONFIGOLDFILE) -replace [regex]::Escape($oldString), $newSubString | Set-Content -Path $YGGCONFIGFILE

    # Run Service
    & net.exe start yggdrasil
}


# The End
Write-Host "Script $scriptPath is terminated with success!"
Exit 0
