#?replace(sub = "\t", by ="    ")
import std/os
import std/parsecfg
import std/strutils


let rootPath = getAppFilename().splitPath().head.splitPath().head & DirSep 
let cfgFile = rootPath & "build.nimble"
let modPath = rootPath & "mods" & DirSep

let config = loadConfig(cfgFile)

echo "cfgFile:	", cfgFile
echo "backend:	", config.getSectionValue("","backend")
echo "cfgFile:	", ":\r\n" & $config


proc writeFile(path: string): bool=
	return false

proc forPathAsFiles(path: string, post: proc(filepath: string): void): void=
	for k, i in walkDir(path):
		if $k == "pcDir" or $k == "pcLinkToDir":
			forPathAsFiles(i, post)
		else: #// $i.kind == "pcFile"or  $i.kind =="pcLinkToFile":
			post(i)
	return

proc forFiles(path: string): void=
	let (filePath, fileName, fileExt) = splitFile(path)
	let tail = fileName & fileExt
	if fileExt != ".lua" and fileExt != ".conf":
		return
	let contents = readFile(path)
	let packedmodname = filePath.splitPath().head.splitPath().tail & DirSep & filePath.splitPath().tail
	if tail == "init.lua": # and ( "depend" notin packedmodname ):
		if contents.count("\n") >= 3 or contents.count("\n") == 17 :
			echo "skipping" & '<' & packedmodname & '>' & "reason: line length " & $(contents.count("\n")+1) & " lines long!"
		else:
			#todo:do not hard code this, it will break and you will not remember why!
			echo """overwrite file data as --force:'""" & contents & "'"
			writeFile(path, "dependency.init()\n")
	if tail == "mod.conf":
		let modName = filePath.splitPath().tail
		let comment = "#name is a uid that only allows characters [a-z0-9_]\r\n"
		let name = "name = " & modName & "\r\n"
		var description = ""
		
		if fileExists(filePath & DirSep & "about.txt" ): 
			description = readFile( filePath & DirSep & "about.txt" )
			if description.strip().count("\n") != 0 and false:
				echo "line count was " & $description.strip().count("\n") & " at file " & path & " skiping description"
				description = ""
		writeFile(path, comment & name & description)
		#echo  comment & name & description
forPathAsFiles(modPath, forFiles)


