#------------------------------------------------------------------------------
# <copyright file="EditWsdlFile.ps1" company="Microsoft">
#      Copyright (c) Microsoft Corporation.  All rights reserved.
# </copyright>
# <summary>
#      Script to edit WSDL file. See the comments in the functions for details.
# </summary>
#------------------------------------------------------------------------------


param ($servicename="CcpEchoSvc",
       $wsdl="tempuri.org.wsdl"
      )

$nerrs = 0
$xsdlist = @{}
$binding = ""

function GetXSDs()
{
    $filelist = Get-ChildItem "*.xsd"
    
    foreach ($f in $filelist) {
        [xml]$xml = get-content $f
        $schema = $xml.GetElementsByTagName("xs:schema")
        $tns = $schema.item(0).targetNamespace
        
        $xsd  = "      <xsd:import namespace=`"" + $tns + "`""
        $xsd += " schemaLocation=`"" + $f.Name + "`" />"
        $xsdlist[$tns] = $xsd
    }
        
    if ($xsdlist.Count -eq 0) {
        $nerrs++
        Write-Output "Error: no xsd is found"
        exit $script:nerrs
    }
}

# edit WSDL file
# Associate imported namespaces with xsd files. For each definitions\types\schema\import tag, add a 
# schemaLocation attribute whose value is the name of the respective xsd
#
function EditWsdlFile()
{
    if (!(Test-Path $wsdl)) {
        Write-Output "Error: wsdl file $wsdlFileName doesn't exist"
        $nerrs++
        exit $script:nerrs
    }

    $lines = get-content $wsdl
    $nlines = $lines.Count
    
    for ($i = 0; $i -lt $nlines; $i++) {
        $s = $lines[$i]
        if ($s.Contains("<xsd:import")) {
            $nsstr = "namespace=`""
            $first = $s.IndexOf($nsstr) + $nsstr.Length
            $first1 = $first + 1
            $end = $s.IndexOf("`"", $first)
            $target = $s.Substring($first, $end-$first)
            if ($xsdlist.Contains($target)) {                
                $lines[$i] = $xsdlist[$target]
            }
            else {
                Write-Output "Warning: xsd file not found for namespace '$target'"
            }
        }
        elseif ($s.Contains("<wsdl:binding name=`"")) {
            $str = "name=`""
            $first = $s.IndexOf($str) + $str.Length
            $first1 = $first + 1
            $end = $s.IndexOf("`"", $first1)
            $script:binding = $s.Substring($first, $end - $first)
        }
        elseif ($s.Contains("</wsdl:binding"))
        {
            $serviceStr = GetService $script:serviceName $script:binding
            $lines[$i] += $serviceStr
        }
    }
    $tempWsdl = "temp_" + $wsdl
    Set-Content $tempWsdl $lines
}


# Add service element as the last child element of the definitions element as follows:			
#			  </wsdl:binding>	<-- Dont add this line. For reference purposes only
#			  <wsdl:service name="EchoService">
#				<wsdl:port name="DefaultBinding_IEchoSvc" binding="tns:DefaultBinding_IEchoSvc">
#					<soap:address location="https://localhost/Broker" />
#				</wsdl:port>
#			  </wsdl:service>
#			</wsdl:definitions>  <-- Dont add this line. For reference purposes only
#
function GetService($serviceName, $binding)
{
    $s = "`n"
    $s += "  <wsdl:service name=`"$serviceName`">`n"
    $s += "      <wsdl:port name=`"" + $binding + "`" binding=`"tns:" + $binding + "`">`n"
    $s += "          <soap:address location=`"https://localhost/Broker`" />`n"
    $s += "      </wsdl:port>`n"
    $s += "  </wsdl:service>`n"
    $s
}

GetXSDs
EditWsdlFile
