#!/bin/sh

if [ "$REQUEST_METHOD" != "POST" ]; then
	echo "405 Method Not Allowed"
	echo "Content-Type: text/plain"
	echo
	echo "there isn't."
	exit
fi

question="$(lib/querystring question | dos2unix)"

if [ -z "$question" ]; then
	echo "400 Bad Request"
	echo "Content-Type: text/plain"
	echo
	echo "bad request."
	exit
fi

echo "Content-Type: text/html"
echo

echo '<!doctype html>
<html>
	<head>
		<link rel="stylesheet" href="/a/static/amethyst.css" />
		<meta charset="utf-8"/>
		<title>question status - citrons.xyz</title>
		<meta name="viewport" content="width=device-width, initial-scale=1">
	</head>
	<body>
		<main>
			<h1>question status</h1>
			<p>question submitted. <a href="/a/question">return</a></p>
		</main>
	</body>
</html>'


format_question() {
	printf '### %s\n' "$(date +'%Y %m %d %H:%M:%S')"
	grep -E '.+' \
		| sed -e 's/\&/\&amp\;/g' -e 's/</\&lt\;/g' -e 's/>/\&gt\;/g' \
		| sed -e 's/^/> <p>/g' -e 's/$/<\/p>/g'
	echo
	echo
}

printf '%s' "$question" | format_question >> /opt/questions.txt
