const fs = require("fs")
const { exec } = require("child_process");
const serverPath = require("./serverPath.js")
const members = require("/etc/newsletter_members.js")
const blogs = require(serverPath+"/blog/blogs.js")
let finalBlogIndex = blogs.length-1
const final_blog = blogs[finalBlogIndex]
const lastBlogIndex = require(serverPath+"/node/previousBlog.js")

if (finalBlogIndex > lastBlogIndex) {
	for (let current_member = 0; current_member < members.length; current_member++) {
		let command = `echo "<h3>Date created: ${final_blog[2]}</h3><br/>${final_blog[1]}<br/><br/><a href='https://deadvey.com/blog/blogs/${finalBlogIndex}.html'>Original Post</a>" | mutt -e "set content_type=text/html" -s "DeaDvey's Blog: ${final_blog[0]}" ${members[current_member]}` 
		exec(command)
	}
}

finalBlogIndex = blogs.length-1
fs.writeFileSync(serverPath+"/node/previousBlog.js", `let lastBlog=${finalBlogIndex.toString()}; if (typeof module !== 'undefined' && module.exports) { module.exports = lastBlog; }`);
