#Requires -Version 3.0 -Modules NetAdapter

. $PSScriptRoot\GetICS.ps1

function Set-ICS {
	[CmdletBinding()]
	param (
		[Parameter(Mandatory,
				ValueFromPipeline,
				ValueFromPipelineByPropertyName)]
		[ValidateScript({
			If ((Get-NetAdapter -Name $_ -ErrorAction SilentlyContinue -OutVariable INetNIC) -and (($INetNIC).Status -ne 'Disabled' -or ($INetNIC).Status -ne 'Not Present')) {
				$True
			}
			else {
				Throw "$_ is either not a valid network adapter of it's currently disabled."
			}
		})]
		[Alias('Name')]
		[string]$InternetInterfaceName,

		[ValidateScript({
			If ((Get-NetAdapter -Name $_ -ErrorAction SilentlyContinue -OutVariable LocalNIC) -and (($LocalNIC).Status -ne 'Disabled' -or ($INetNIC).Status -ne 'Not Present')) {
				$True
			}
			else {
				Throw "$_ is either not a valid network adapter of it's currently disabled."
			}
		})]
		[string]$LocalInterfaceName,

		[Parameter(Mandatory)]
		[bool]$Enabled
	)

	BEGIN {
		if ((Get-ICS $InternetInterfaceName).SharingEnabled -and $Enabled) {
			if ((Get-ICS $LocalInterfaceName).SharingEnabled) {
				Break
			} else {
				Throw 'Unable to continue due to Internet connection sharing already being enabled with another network adapter.'
			}
		}

		regsvr32.exe /s hnetcfg.dll
		$netShare = New-Object -ComObject HNetCfg.HNetShare
	}
	
	PROCESS {
		
		$publicConnection = $netShare.EnumEveryConnection |
		Where-Object {
			$netShare.NetConnectionProps.Invoke($_).Name -eq $InternetInterfaceName
		}

		$publicConfig = $netShare.INetSharingConfigurationForINetConnection.Invoke($publicConnection)

		if ($PSBoundParameters.LocalInterfaceName) {
			$privateConnection = $netShare.EnumEveryConnection |
			Where-Object {
				$netShare.NetConnectionProps.Invoke($_).Name -eq $LocalInterfaceName
			}

			$privateConfig = $netShare.INetSharingConfigurationForINetConnection.Invoke($privateConnection)
		} 
		
		if ($Enabled) {
			$publicConfig.EnableSharing(0)
			if ($PSBoundParameters.LocalInterfaceName) {
				$privateConfig.EnableSharing(1)
			}
		}
		else {
			$publicConfig.DisableSharing()
			if ($PSBoundParameters.LocalInterfaceName) {
				$privateConfig.DisableSharing()
			}
		}

	}

}