param([string]$JoinNetwork = '')

## If $JoinNetwork is not specified at the command line, use the ZTNetworkID customer custom field if it exists
If ( ($JoinNetwork = '') -and ($ZeroTierNetworkID) ) { $JoinNetwork = $ZeroTierNetworkID }

## Make sure we're running at least Windows 7
If ( IsWindowsVersion -ge "6.1" )
{
    ## Specify the ZeroTier One MSI installer URL
    $URL = 'https://download.zerotier.com/dist/ZeroTier%20One.msi'

    ## Install ZeroTier One
    Install-Program "msiexec.exe" -Arguments "/i ${URL} /qn /norestart"

    ## Make sure there's a network to join before attempting to start the ZeroTierOneService or join a network
    If ( $JoinNetwork )
    {
        ## This will be used to determine whether or not the service was successfully started
        $ServiceRunning = $false

        ## Check to make sure the ZeroTierOneService has started
        ## We'll only loop through this for a few seconds before failing
        LogMsg "Attempting to start the ZeroTierOneService service..."
        For ( $i = 1 ; $i -le 5 ; $i++ )
        {
            ## Check to see if the service is running
            If ( (Get-Service -Name ZeroTierOneService).Status -ne "Running" )
            {    
                ## Attempt to stop/start the service
                Try { Restart-Service -Name ZeroTierOneService -Force }
                Catch { LogErr $_.Exception.Message }

                ## Wait a few seconds before checking again
                Start-Sleep -Seconds 5
            }
            
            ## If the service is running, break the loop
            Else
            {
                LogMsg "    ZeroTierOneService service is started"
                $ServiceRunning = $true ; break
            }
        }

        ## Make sure the service is running before attempting to join a network
        If ( $ServiceRunning )
        {
            ## Set the path to the ZeroTier installation directory
            If ( Is64bit ) { $ZTPath = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\WOW6432Node\ZeroTier, Inc.\ZeroTier One').Path }
            Else           { $ZTPath = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\ZeroTier, Inc.\ZeroTier One').Path }

            ## Make sure the path actually exists
            $ZTcli = "${ZTPath}\zerotier-cli.bat"
            If ( Test-Path "${ZTcli}" )
            {
                ## Join the network, if $ZTcli exists
                LogMsg "Joining ZeroTier network ${JoinNetwork}"
                Try { Start-Process $ZTcli -ArgumentList "join ${JoinNetwork}" -Wait }
                Catch { LogErr $_.Exception.Message }
            }
            Else { LogMsg "Could not find ""${ZTcli}""" }
        }

        ## If the service still isn't running, just error out
        Else { LogMsg "    ZeroTierOneService could not be started in a timely fashion" }

    }
    Else { LogMsg "A ZeroTier network ID was not specified, will not join any network" }

    ## Remove the ZeroTier One Start Menu shortcut
    $ShortcutPath = "${ProgramData}\Microsoft\Windows\Start Menu\Programs\ZeroTier One.lnk"
    If ( Test-Path $ShortcutPath )
    {
        LogMsg "Deleting Start Menu shortcut from ""${ShortcutPath}"""
        Try { Remove-Item $ShortcutPath -Force }
        Catch { LogErr $_.Exception.Message }
    }

}
Else { LogMsg "ZeroTier can only be installed on Windows 7 or higher operating systems" }
