<!-- #include virtual="/NR/System/Access/Authenticate.inc" --><%

response.buffer = true

' Comments
' In staging mode all URLs are surrounded with <NCOMPASSSTAGINGSERVER></NCOMPASSSTAGINGSERVER> tags.

'-----------------------------------------------------------------
'HomePort Utilities

'--- Pretty-Print an Error Message and stop processing
Sub PrintError(strFunctionName)
	WriteLn( "<P>Err in Function " & strFunctionName & "<BR>" )
	WriteLn( "Err Number = " & Err.Number & "<BR>" )
	WriteLn( "Err Desc = " & Err.Description & "</P>" )
	Response.End
End Sub

Sub CheckErrorStatus(functionname)
	If Err.Number <> 0 Then
		PrintError(functionname)
	End If
End Sub

Dim nWriteCount
nWriteCount = 0

'---- Write out the given String
Sub Write( Str )
	Response.Write( Str )
	if not Response.IsClientConnected then
		Response.End
	end if

	nWriteCount = nWriteCount + 1
	if nWriteCount > 50 then
		nWriteCount = 0
		response.flush
	end if
End Sub

'---- Write out the given string with a CrLF at the end
Sub WriteLn( Str )
	'Response.Write( "<p>" & Str & "</p>" & vbCrLf )
	Response.Write( Str & "<br>" )
	if not Response.IsClientConnected then
		Response.End
	end if

	nWriteCount = nWriteCount + 1
	if nWriteCount > 50 then
		nWriteCount = 0
		response.flush
	end if
End Sub

'---- Create an indent of the specified level by writing out spaces
Sub Indent(i)
	Dim s
	s = String(i*3, " ")
	'Response.Write "<pre>" & s & "</pre>"
	Response.Write s

	if not Response.IsClientConnected then
		Response.End
	end if

	nWriteCount = nWriteCount + 1
	if nWriteCount > 50 then
		nWriteCount = 0
		response.flush
	end if
End Sub

%>
<%

'============================================================================
' Global stuff
'============================================================================

'---- Set up some Global Variables
Dim g_AutoSession
Dim g_Server
Dim g_ThisChannel

'============================================================================
' main code
'============================================================================

On Error Resume Next
Err.Clear

'Set script timeout to be 10 hours
Server.ScriptTimeout = 60 * 60 * 10

Set g_AutoSession = AutoSession

	If Err.Number <> 0 Then
		PrintError("Error: Set g_AutoSession = AutoSession")
	End If

'---- Get the Server from the g_AutoSession
Set g_Server = g_AutoSession.Server

	If Err.Number <> 0 Then
		PrintError("Error: Set g_Server = g_AutoSession.Server")
	End If

Set g_ThisChannel = g_AutoSession.RootChannel

	If Err.Number <> 0 Then
		PrintError("Error: Set g_ThisChannel = g_AutoSession.RootChannel")
	End If

Response.ContentType = "text/plain"

Response.Write("<HTML>")
Response.Write("<HEADER></HEADER>")
Response.Write("<BODY>")
Response.Write("<PRE>")

If g_ThisChannel Is Nothing Then
	PrintError("Error: g_ThisChannel Is Nothing")
Else
	Call PrintChannel(g_ThisChannel, 0)
End If

Call Indent(0)
Response.Write("</PRE>")
Response.Write("</BODY>")
Response.Write("</HTML>")

'============================================================================
' end main code
'============================================================================

'============================================================================
Function PrintChannel(theChannel, nIndentDepth)
	Dim strQuery
	Dim strHref
	Dim SubChannels
	Dim Postings
	Dim Child
	Dim i

	on error resume next

	'---- Generate the channel HREF
	strHref = theChannel.URL

	'---- Print out the CDF channel information
	Call Indent(nIndentDepth)
	Call WriteLn("&lt;CHANNEL HREF=""" & Server.URLEncode(strHref) & """ LASTMOD=""" & ConvertDateToISO(theChannel.LastModifiedDate) & """&gt;")
	Call WriteTitle(theChannel, nIndentDepth+1)

	'---- Print out all the users subscribed subchannels
	Set SubChannels = theChannel.VisibleSubchannels

	If Not IsEmpty(SubChannels) Then
		If SubChannels.Count > 0 Then
			Call SubChannels.SortBy("Name")
			Call SubChannels.SortBy("EffectiveDate")
			For i=0 to SubChannels.Count - 1
				Set Child = SubChannels(i)
				Call PrintChannel(Child, nIndentDepth+1)
			Next
		End If
	End If

	If Err.Number <> 0 Then
		PrintError("GenCDF.asp:PrintChannel (Before -> Set Postings = theChannel.Postings)")
	End If

	Set Postings = theChannel.Postings

	If Err.Number <> 0 Then
		PrintError("GenCDF.asp:PrintChannel (Set Postings = theChannel.Postings)")
	End If

	If Not IsEmpty(Postings) Then
		If Postings.Count > 0 Then
			Call Postings.SortBy("Name")
			Call Postings.SortBy("EffectiveDate")
			For i=0 to Postings.Count - 1
				Set Child = Postings(i)
				Call PrintPageView(Child, nIndentDepth+1)
			Next
		End If
	End If

	If Err.Number <> 0 Then
		PrintError("GenCDF.asp:PrintChannel")
	End If

	Call Indent(nIndentDepth)
	Call WriteLn("&lt;/CHANNEL&gt;")

	If Err.Number <> 0 Then
		PrintError("GenCDF.asp:PrintChannel")
	End If
End Function

'============================================================================
Function PrintPageView(thePosting, nIndentDepth)
	Dim strHref

	On Error Resume Next

	'---- Generate the query string and channel HREF
	strHref = thePosting.URL

	If Err.Number <> 0 Then
		PrintError("GenCDF.asp:PrintPosting")
	End If

	Call Indent(nIndentDepth)

	If Err.Number <> 0 Then
		PrintError("GenCDF.asp:PrintPosting")
	End If

	Call WriteLn("&lt;ITEM HREF=""" & Server.URLEncode(g_AutoSession.ResolveUrl(strHref)) & """ LASTMOD=""" & ConvertDateToISO(thePosting.LastModifiedDate) & """&gt;")

	If Err.Number <> 0 Then
		PrintError("GenCDF.asp:PrintPosting")
	End If

	Call WriteTitle(thePosting, nIndentDepth+1)

	If Err.Number <> 0 Then
		PrintError("GenCDF.asp:PrintPosting")
	End If

	Call Indent(nIndentDepth)

	If Err.Number <> 0 Then
		PrintError("GenCDF.asp:PrintPosting")
	End If

	Call WriteLn("&lt;/ITEM&gt;")

	If Err.Number <> 0 Then
		PrintError("GenCDF.asp:PrintPosting")
	End If
End Function

'============================================================================

Function WriteTitle(theObject, nIndentDepth)
	Dim strTitle
	Err.Clear

	On Error Resume Next

	strTitle = theObject.Name
	If Err.Number <> 0 Then
		PrintError("GenCDF.asp:WriteTitle (theObject.Name)")
	End If

	strTitle = Server.URLEncode(strTitle)'what is the impact of changing HTMLEncode to URLEncode?
	If Err.Number <> 0 Then
		PrintError("GenCDF.asp:WriteTitle (Server.HTMLEncode)")
	End If

	Call Indent(nIndentDepth)
	Call WriteLn("&lt;TITLE&gt;" & strTitle & "&lt;/TITLE&gt;")

	If Err.Number <> 0 Then
		PrintError("GenCDF.asp:WriteTitle")
	End If
End Function

'============================================================================
' various html utils
'============================================================================

Function TwoDigitString(str)
	' takes a string that is assumed to be either 1 or 2 chars long.
	' If it is one char long then "0" is prepended to it.

	If Len(str) = 1 Then str = "0" & str
	TwoDigitString = str
End Function

Function ConvertDateToISO(SomeDate)
	' Converts to ISO 8601:1988
	' ex: The date "14 February 1993, 13:10:30" is represented as "19930214T131030" or "1993-02-14T13:10:30"

	Dim strISO
	strISO = Year(SomeDate) & "-" & TwoDigitString(Month(SomeDate)) & "-" & TwoDigitString(Day(SomeDate)) _
		& "T" & TwoDigitString(Hour(SomeDate)) & ":" & TwoDigitString(Minute(SomeDate)) & ":" & TwoDigitString(Second(SomeDate))

	ConvertDateToISO = strISO
End Function
%>