const serverPath = require("./serverPath.js")
const boards = require(serverPath+"/board/boards.js");
const fs = require("fs");
const { pageTop, pageBottom } = require(serverPath+"/html.js")
const { exec } = require("child_process");

console.log(boards);

for (let board = 0; board < boards.length; board++) {
    let boardPath = `${serverPath}/board/${boards[board]}/index.html`;

    fs.readFile(boardPath, (err, data) => {
	if (err) {
		console.log(`File /${boards[board]}/index.html doesn't exist... Creating`)
		fs.writeFile(`${boardPath}`,`<!DOCTYPE html><div id="header"></div><!--header--><div id="main"><form action="/board/submit-comment" id="commentForm" method="post"><input name="name" class="form-control" id="name" placeholder="Enter your name"><br/><textarea height="40px" name="comment" class="form-control input-comment" id="comment" placeholder="Enter your Comment..."></textarea><input type="hidden" name="pageID" value="${boards[board]}"><button type="submit">Submit</button></form><div id="comments" class="${boards[board]}" style="word-wrap: break-word"></div><!--comments--></div><!--main--><div id="footer"></div><!--footer-->`, function (err) {
			if (err) throw err;
			console.log('Created page');
		});
		exec('node write_comments.js', (error, stdout, stderr) => {
			if (error) {
			   console.error(`Error: ${error.message}`);
			   return;
			 }
			 console.log(`Child script output: ${stdout}`);
		});
		exec('node template.js', (error, stdout, stderr) => {
			if (error) {
			   console.error(`Error: ${error.message}`);
			   return;
			 }
			 console.log(`Child script output: ${stdout}`);
		});
	};
	if (!err && data) {
		console.log(`File exists: ${boards[board]}`) 
	}
    }); 
}
