# TODO: Add comment
# 
# Author: E.Korsching Sep 11, 2014, 2026
###############################################################################


pubRsource <- function(spath="/home/user/eclipseR", output.name="codelist.tt2"){
	# this function should support the publication of R source code files
	#  and generates a template file which conforms with the tt2 and html based standard
	#  of the CCSR group website
	# spath: preferably the base 0function folder (but also also any other suitable folder)
	# output.name: relative path and file name for output (without leading slash)
	#  (see also  all_web_code.pdf )
	
	# ini
	if(spath==""){ stop("\n Provide an absolute path to the main R folder\n") }
	if(is.na(file.info(spath)$isdir)){ stop("\n R folder not existing\n") }
	# file.exists(paste(getwd(),"/",output.name,sep=""))) , file.access(names, mode={0124})
	
	# define html code snippets 'p.'			&nbsp;
	p.tab.x <- function(x){
		# x: number of tabs
		out <- ""
		for(i in 1:x){
			out <- paste(out,"\t",sep="")
		}
		return(out)
	}
	p.n <- "\n"
	p.br <- "<br>"
	p.hr <- "<hr>"
	
	p.a.a <- paste(p.tab.x(4), "<a href=\"./download/rcode", sep="")		# link to file and file name
	p.a.m <- "\" class=\"whiteT\" target=\"_self\" title=\"\">"
	p.a.e <- "</a>"
	
	# define page modules
	top.p <- paste(
			"[% PROCESS aHtml.tt2 %]", p.n,
			"[% PROCESS cBodyHeader.tt2 %]", p.n,
			p.tab.x(1), "<main>", p.tab.x(8), "<!-- main - all container, all the boxes, all items  or  instead/inside main tbox3-3 -->", p.n,
			
			p.tab.x(2), "<div class=\"tbox2-3solo\">", p.n,
			p.tab.x(3), "<div class=\"tbox1-1soloI\">", p.n,
			p.tab.x(4), "<img src=\"images/spot28_28.png\" alt=\"\">", p.n,
			p.tab.x(3), "</div>", p.n,
			p.tab.x(3), "<div class=\"tbox1-1soloT\">", p.n,
			p.tab.x(4), "Cite this web site in case of usage", p.n,
			p.tab.x(3), "</div>", p.n,
			p.tab.x(3), "<div class=\"tbox1-1soloT\">", p.n,
			p.tab.x(4), "GPLv3 licence", p.n,
			p.tab.x(3), "</div>", p.n,
			p.tab.x(2), "</div>", p.n,
			
			p.tab.x(2), "<div class=\"tbox2-3\">", p.n,
			p.tab.x(3), "<div class=\"tb2-3head\">", p.n,
			p.tab.x(4), "R", p.n,
			p.tab.x(3), "</div>", p.n,
			p.tab.x(3), "<div class=\"tb-info\">", p.n,
			p.n,
#			p.tab.x(4), "<h3>Collection of R functions grouped by folder names &nbsp;&nbsp;&nbsp; [ not functional ]</h3>", p.n,
			p.tab.x(4), "<h3>Collection of R functions grouped by folder names</h3>", p.n,
			p.tab.x(4), "The entire codebase is evidently build on top of numerous code contributions from other developers - including those outside the R-Core Team community - that are subject to their respective open-source licenses.", p.n,
			p.tab.x(4), p.br, p.n,
			sep="")
	
	bottom.p <- paste(
			p.tab.x(4), p.n,
			p.tab.x(4), "<img src=\"images/736_1t.png\" width=\"736\" alt=\"\">", p.n,
			p.tab.x(4), p.n,
			p.tab.x(3), "</div>", p.n,
			p.tab.x(2), "</div>", p.n,
			p.n,
			p.tab.x(1), "</main>", p.tab.x(8), "<!-- main end -->", p.n,
			p.n,
			sep="")
	
	
	# start: count files found
	f.count <- 0
	# open file
	fileConn <- file(paste(getwd(),"/",output.name,sep=""), open="w")
	# read in folder structure
	proj.dirs <- list.dirs(path=spath, full.names=T, recursive=T)
	# remove dot folders
	proj.dirs <- proj.dirs[ !grepl(pattern="/.", x=proj.dirs, fixed=T) ]
	proj.dirs.len <- length(proj.dirs)
	
	# make local header entry
	writeLines(top.p, con=fileConn)
	
	# analyse project folders
	for(i in 1:proj.dirs.len){
		
		# filter on files with *.R or *.r
		all.r.source <- list.files(path=proj.dirs[i], pattern="(\\.R$|\\.r$)", all.files=F, full.names=T, recursive=F, ignore.case=F, include.dirs=F) #, no..=F)
		all.r.source.len <- length(all.r.source)
		f.count <- f.count + all.r.source.len
		
		if(all.r.source.len>0){
			# strip path from project dir
			proj <- unlist(strsplit(x=proj.dirs[i], split="/", fixed=T))
			proj <- proj[length(proj)]	# take last entry
			cat("\n\n -- folder -- ",proj," --")
			
			# make folder title
			writeLines(paste(p.tab.x(4), "<h4>", proj, "</h4>", p.n, sep=""), con=fileConn)
			# make list start
			writeLines(paste(p.tab.x(4), "<p><span class=\"fontcode\">", sep=""), con=fileConn)
			
			# entry per file
			for(j in 1:all.r.source.len){
				# strip path from file
				f.n <- unlist(strsplit(x=all.r.source[j], split="/", fixed=T))
				f.n <- f.n[length(f.n)]	# take last entry
				cat("\n  ",f.n)
				# delete 'spath' portion from file path = the relative path fragment
				p.rel <- strsplit(x=all.r.source[j], split=spath, fixed=T)[[1]][2]
				# make file entry
				writeLines(paste( p.a.a, p.rel, p.a.m, f.n, p.a.e, p.br, sep=""), con=fileConn)
			}
			# make list end entry
			writeLines(paste(p.tab.x(4), "</span></p>", p.n, sep=""), con=fileConn)
			cat("\n # ",all.r.source.len)
		}
	}
	
	# make local footer entry
	writeLines(bottom.p, con=fileConn)
	
	close(fileConn)
	cat("\n\n Total number of files : ",f.count,"\n\n")
	return()
}


# pubRsource(spath="/home/korschi/on3/eclipseR/0functions", output.name="../source20260508.tt2")
# a <- pubRsource(spath="/home/korschi/on3/eclipseR/0functions", output.name="../rcode.tt2")



