# /opt/sdlBasic/bin/sdlBrt
Function returnNextTag(fileStream)
	stringStart=0
	stringa=""
	count=1
	seek(fileStream,loc(fileStream)-1)
	while eof(fileStream)=0
		b=chr$(readbyte(fileStream))
		if count>1 then
			if b=">"then
				 exit while
			else
				count = count+1
				stringa=stringa&b
			end if
		else
			if b="<" then
				stringStart=loc(fileStream)
				count = count+1
			end if
		end if
	end while
	--print count
	if count>1 then
		if stringa="" then
			seek(fileStream,loc(fileStream)-1)
			return -1
		else
			seek(fileStream,loc(fileStream)-1)
			return stringa
		end if
	end if
end function

Function returnNextIntraTag(fileStream)
	stringStart=0
	stringa=""
	count=1
	seek(1,loc(fileStream)-1)
	while eof(fileStream)=0
		b=chr$(readbyte(fileStream))
		if count>1 then
			if b="<"then
				 exit while
			else
				count = count+1
				stringa=stringa&b
			end if
		else
			if b=">" then
				stringStart=loc(fileStream)
				count = count+1
			end if
		end if
	end while
	--print count
	if count>1 then
		if stringa="" then
			seek(fileStream,loc(fileStream)-1)
			return -1
		else
			return stringa
		end if
	end if
end function

function createHeader(stream,title)
	file output #stream, "<!DOCTYPE html PUBLIC "&chr$(34)&"-//W3C//DTD HTML 4.01 Transitional//EN"&chr$(34)&">"
	file output #stream, ""
	file output #stream, ""
	file output #stream,  ""
	file output #stream,  "   "
	file output #stream,  "   index"
	file output #stream,   "   "
	file output #stream, ""
	file output #stream, ""
	file output #stream, ""
	file output #stream, ""
	--file output #stream, "
"&  findDir() &"
"
	file output #stream, "
"&  title &"
"
	file output #stream, "
"
	file output #stream, "
"

end function

function findDir()
	curDir$=dir$
	 pos=rinStr ( curDir$, "/" )
	 return  right$( curDir$, len(curDir$)-pos)
end function

function createFooter(stream)
	file output #stream, ""
	file output #stream, ""
end function

function formatName(title)
	formattedName=""
	flag=0
	for n = 1 to len(title)
			c$=mid(title,n,1)
			if c$<>" " then
				if flag then
					formattedName=formattedName+uCase(c$)
					flag=0
				else
					formattedName=formattedName+c$
				end if
			else
				flag=1
			end if
	next
	return formattedName
end function

function findTag(tag, fileStream)
	while eof(fileStream)=0
		rtag=returnNextTag(fileStream)
		if tag=rtag then : exit while : end if
	wend
	--print loc(fileStream)
	return loc(fileStream)
end function

-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
--											MAIN
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------

setDisplay( 640, 300, 32, 1 )

txtFile="index.txt"
fileFlag2=0
openTextFlag=0
openFlagSpan=0

-------------------------------------------------------------------------------------------------
------------------------------------------ creating indexes of commands sorted by functionality
-------------------------------------------------------------------------------------------------
originalDir$=dir$

if isLinux then : shell("ls > dirs.txt") : end if
open "dirs.txt" for input as 5

while not eof( 5 )
	file input #5, directory$
	locate (1,4) : prints("working on "&directory$&"                                       ")
	if directory$<>"dirs.txt" and directory$<>"index.html" and directory$<>"CVS" and directory$<>"indexCreator.sdlbas" and directory$<>"alphabetical"then
		chDir(originalDir$&"/"&directory$)

		if isLinux then : shell("ls > index.txt") : end if

		open txtFile for input as 1
		open "index.html" for output as 2
		------------------------------------------------ writing header
		open "sectionName.txt" for input as 6
		file input #6, sectionName$
		close 6
		createHeader(2,sectionName$)

		while not eof( 1 )
			' read a line from the file and print it
			file input #1, path$
			if path$<>"index.txt" and path$<>"index.html" and path$<>"CVS" and path$<>"sectionName.txt" then
				if  left$( path$, 1 )<>"_" then
					--link$=left$(path$, length(path$)-5)
					------------------------------------------ searching link title
					loc1=loc(1) --------- seek bug? store actual stram 1 loc
					--print loc(1)
					--print path$
					open  path$ for input as 3
					findTag("span class="&chr$(34)&"subTitle"&chr$(34), 3)
					link$=returnNextIntraTag(3)
					--print link$
					close 3
					seek(1,loc1) ------------------- restore original stream 1 loc
					------------------------------------------
					if inStr( path$, "!" )<>0  then
						file output #2, "<div style="&chr$(34)&"text-align: left; background-color: rgb(255, 0, 0);"&chr$(34)&">"
						file output #2, "<a class="&chr$(34)&"link"&chr$(34)&" target="&chr$(34)&"content"&chr$(34)&" href="&chr$(34)&path$&chr$(34)&">"&link$&"</a><br>"
						file output #2, "</div>"
					else
						file output #2, "<a class="&chr$(34)&"link"&chr$(34)&" target="&chr$(34)&"content"&chr$(34)&" href="&chr$(34)&path$&chr$(34)&">"&link$&"</a><br>"
					end if
				end if
			end if
		end while

		createFooter(2)

		close 1
		close 2
	end if
end while

-------------------------------------------------------------------------------------------------
------------------------------------------ creating indexes of commands sorted alphabetically
-------------------------------------------------------------------------------------------------

chDir(originalDir$)
seek(5,1) ------reset dir.txt
open "alphabetical/index.txt" for output as 2
--createHeader(2,"index")
while not eof( 5 )
	file input #5, directory$
	locate (1,4) : prints("working on "&directory$&"                                       ")
	if directory$<>"dirs.txt" and directory$<>"alphabetical" and directory$<>"CVS" and directory$<>"sortedIndex.txt" and directory$<>"indexCreator.sdlbas" then
		chDir(originalDir$&"/"&directory$)
		open txtFile for input as 1
		while not eof( 1 )
			' read a line from the file and print it
			file input #1, path$
			if path$<>"index.txt" and path$<>"index.html" and path$<>"CVS " and path$<>"indexCreator.sdlbas" and path$<>"sectionName.txt" then
				if  left$( path$, 1 )<>"_" then
					-------------------------------------- searching link title
					loc1=loc(1) --------- seek bug? store actual stram 1 loc
					--print loc(1)
					--print path$
					open  path$ for input as 3
					findTag("span class="&chr$(34)&"subTitle"&chr$(34), 3)
					link$=returnNextIntraTag(3)
					--print asc(left$(link$,1))
					if left$(link$,1)=chr(10) then
						link$=right$(link$,len(link$)-1)
					end if
					--print link$
					close 3
					seek(1,loc1) ------------------- restore original stream 1 loc
					--------------------------------------
					file output #2, link$&":"&directory$&"/"&path$
				end if
			end if
		end while
		close 1
	end if
end while
close 2
close 5

chDir(originalDir$&"/alphabetical")
if isLinux then : shell("sort index.txt --output=sortedIndex.txt") : end if

data( "a - b", "c","d","e - f","g - i", "j - l", "m - n", "o - q", "r", "s", "t - z")
chunk$=read()

open "sortedIndex.txt" for input as 1
open chunk$&".html" for output as 2
createHeader(2,chunk$)

while not eof( 1 )
	file input #1, lin$
	n= inStr( lin$, ":" )
	link$=left$(lin$, n-1)
	path$=right$(lin$,len(lin$)-n)
	locate (1,4) : prints("working on "&link$&"                                       ")
	if left$( link$, 1 )>right(chunk$,1) then
		createFooter(2)
		close 2
		chunk$=read()
		open chunk$&".html" for output as 2
		createHeader(2,chunk$)
	end if
	if inStr( path$, "!" )<>0  then
		file output #2, "
"
		file output #2, ""&link$&"
"
		file output #2, "
"
	else
		file output #2, ""&link$&"
"
	end if
end while
createFooter(2)
close 1
close 2
