<?php
	function motd($wordsPath, $utf8) {
		$out = [];
		$file = file_get_contents($wordsPath);
		if(!$utf8)
			$file = utf8_encode($file);
		$words = explode("\n", $file);
		$count = rand(1, 10);
		for($i = 0; $i < $count; $i++) {
			$word = $words[array_rand($words)];

			if(rand(0, 4) == 0) { // 1 in 5 chance
				$puctuation = [',', '.', ':', ';', '...', '!', '?'];
				$word .= $puctuation[array_rand($puctuation)];
			}

			$out[] = $word;
		}

		return implode(' ', $out);
	}

	function islist($lang) {
		global $list;
		return $lang == $list ? 'selected' : '';
	}

	// Return source code
	if(isset($_GET['source'])) {
		header('Content-Type: text/plain');
		die(file_get_contents(basename($_SERVER['PHP_SELF'])));
	}

	$dicts = json_decode(file_get_contents('assets/dict/dicts.json'), true);
	$list = strtolower($_GET['list']);
	$dict = $dicts[$list];
	if(!strchr($dict['file'], '/') && !empty($dict['file']))
		$dict['file'] = "/usr/share/dict/{$dict['file']}";

	$today = floor(date('U') / 24 / 60 / 60) * 24 * 60 * 60;
	srand($today + crc32($list));

	$motd = motd(!empty($dict['file']) ? $dict['file'] : 'assets/dict/unicode.txt', !in_array($list, ['fi', 'nb', 'nn', 'sv']));

	// Plaintext
	if(isset($_GET['raw'])) {
		header('Content-Type: text/plain');
		die($motd);
	}

	$motd = htmlspecialchars($motd);

	if(isset($_GET['rss'])) {
		header('Content-Type: application/rss+xml');
?>
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title>MOTD (<?php echo $dict['name']; ?>) | ピケ.コム</title>
		<link>http://xn--rck9c.xn--tckwe/motd.php?list=<?php echo $list; ?></link>
		<atom:link href="http://bbs.xn--rck9c.xn--tckwe/rss.php" rel="self" type="application/rss+xml" />
		<description>A random message each day</description>
		<lastBuildDate><?php echo date('r', $today); ?></lastBuildDate>
		<language><?php echo $list ? : 'en'; ?></language>
		<item>
			<title><?php echo $motd; ?></title>
			<link>http://xn--rck9c.xn--tckwe/motd.php?list=<?php echo $list; ?></link>
			<description><?php echo $motd; ?></description>
			<author>Pk11</author>
			<pubDate><?php echo $post['pubDate']; ?></pubDate>
			<guid isPermaLink="false"><?php echo $today; ?></guid>
		</item>
	</channel>
</rss>
<?php } else { ?>
<!DOCTYPE html>
<html lang="<?php echo $list ? : 'en'; ?>">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title><?php echo $motd; ?></title>
	<meta name="description" content="<?php echo $motd; ?>">
	<link rel="alternate" type="application/rss+xml" title="RSS feed" href="?list=<?php echo $list; ?>&rss" />
</head>
<body>
	<h1 title="<?php echo date("Y-m-d (T)"); ?>"><?php echo $motd; ?></h1>

	<nav lang="en">
		[<a href="//xn--rck9c.xn--tckwe/">back</a>]
		<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get" style="display:inline">
			[<select name="list" aria-label="Word list" onchange="if(this.value != '<?php echo $list; ?>') this.form.submit();">
				<?php
					foreach($dicts as $key => $item)
						$dicts[$key]['id'] = $key;
					usort($dicts, function($a, $b) { return $a['name'] == 'Unicode' ? -1 : strcmp($a['name'], $b['name']); });
					foreach($dicts as $item) {
						$islist = islist($item['id']);
						echo "<option value=\"{$item['id']}\" lang=\"{$item['id']}\" $islist>{$item['name']}</option>";
					}
				?>
			</select>]
			<noscript>
				[<input type="submit">]
			</noscript>
		</form>
		[<a href="?list=<?php echo $list; ?>&raw">raw</a>]
		[<a href="?list=<?php echo $list; ?>&rss">rss</a>]
		[<a href="?source">source</a>]
	<nav>
</body>
</html>
<?php } ?>
