<?php

require('config.php');

#$dbFile  = '/home/lacraia/web_lacraia/writefreely/writefreely.db';
#$baseDir = '/home/lacraia/web_lacraia/gmi_content/lacra.ia.br';

$pdo = new PDO('sqlite:' . $dbFile, null, null, [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);


$sql = <<<SQL
SELECT
    posts.slug,
    posts.content,
    users.username,
    collections.description
FROM posts
LEFT JOIN users, collections
    ON users.id = posts.owner_id
    AND users.id = collections.owner_id
    order by users.username, posts.created desc
SQL;

$stmt = $pdo->query($sql);

$prevusername ='';
$username ='';
$index =[];
$users =[];
$descs =[];

foreach ($stmt as $row) {
    $username    = clean($row['username'] ?? 'nouser');
    $slug        = clean($row['slug'] ?? 'no-slug');
    $description = $row['description'] ?? '--';

    echo "\n-----------------------------------";
    echo "\n# Getting posts for user: $username";
    echo "\n-----------------------------------";

    $userDir = $baseDir . DIRECTORY_SEPARATOR . $username;
    
    if ($username!=$prevusername)
    {
	$users[]=$username;
	$descs[$username]=$description;
	if (count($index)>0)
	   saveOneBlogIndex($prevusername, $index);
	$index = [];
	$prevusername=$username;
    }

    $index[] = $slug;
    $txt = (string)$row['content'];
    saveBothFormats($userDir . DIRECTORY_SEPARATOR . $slug, $txt );

}

#save last index after loop
saveOneBlogIndex($username, $index);
saveUserList($users,$descs);

saveRecentPosts();
saveRootIndex();


echo "\n### Done.\n";

function saveBothFormats($path, $txt)
{
	$mdPath  = $path.'.md';
	$gmiPath = $path.'.gmi';
	
	echo "\n...Saving: $gmiPath / .md\n";
 	
	createMissingDirsForFilePath($mdPath);

	$txtSpaceHack = str_replace("\n\n", '|||', $txt);
	$txtSpaceHack = str_replace("\n", '$$$', $txtSpaceHack);
	file_put_contents($mdPath,$txtSpaceHack);

	#Convert from .md to.gmi
        shell_exec("./bin/gemgen -a below -p below  $mdPath");

	$cleanMe = file_get_contents($gmiPath);

	$cleanMe = str_replace('|||',"\n\n", $cleanMe);
	$cleanMe = str_replace('$$$',"\n", $cleanMe);

        //Cleaned up gmi with propre new lines 
	file_put_contents($gmiPath,$cleanMe);

	//Overwrite again with clean original .md file
	file_put_contents($mdPath,$txt);

}


function saveUserList($users,$descs)
{
	echo "# Generating blog list..\n";

	global $baseDir;

	#save user list in /blogs/index.gmi
	$userFile = $baseDir . DIRECTORY_SEPARATOR . 'blogs'.DIRECTORY_SEPARATOR.'index.gmi';
	$body = "# Lista dos blogs...\n\n";

	sort($users, SORT_STRING | SORT_FLAG_CASE);

	foreach ($users as $user)
   		$body .= "=> /$user"." $user :: ".$descs[$user]."\n";

        createMissingDirsForFilePath($userFile);
	file_put_contents($userFile,$body);
    
}

function saveRootIndex()
{ 
   global $baseDir;
   global $pdo;

   $outFile = $baseDir . DIRECTORY_SEPARATOR . 'index';

   createMissingDirsForFilePath($outFile);
   echo "\n## Saved Root index: $outFile"; 

    $sql = "select * from appcontent where id='about'";
    $stmt = $pdo->query($sql);
    $row  = $stmt->fetch(PDO::FETCH_ASSOC);

    #Create .md as we convert it 
    $body  = "# ".$row['title']."\n\n";
    $body .= $row['content'];

    global $titleRecentPosts;
    global $titleBlogList ;

    $body .= "[$titleRecentPosts](/read)\n";
    $body .= "[$titleBlogList](/blogs)\n\n";


    saveBothFormats($outFile,$body);

}

function saveRecentPosts()
{ 
	
   global $baseDir;
   global $pdo;

   $outFile = $baseDir . DIRECTORY_SEPARATOR . 'read'.DIRECTORY_SEPARATOR.'index';

$sql = <<<SQL
SELECT
    posts.slug,
    posts.content,
    users.username
FROM posts
LEFT JOIN users
    ON users.id = posts.owner_id
    order by posts.created desc
    limit 10
SQL;

    createMissingDirsForFilePath($outFile);

    $stmt = $pdo->query($sql);
    $row = $stmt->fetch(PDO::FETCH_ASSOC);

    $body = "# Posts recentes \n";

    foreach ($stmt as $row) {
      $username    = clean($row['username'] ?? 'nouser');
      $slug        = clean($row['slug'] ?? 'no-slug');

      $body .= "=> /$username/$slug".".gmi $slug \n";
    }

    saveBothFormats($outFile,$body);
    echo "\n## Saved Recent index: $outFile"; 

}



function saveOneBlogIndex($username,$index)
{ 
   global $baseDir;

   $prevuserDir = $baseDir . DIRECTORY_SEPARATOR . $username;
   $body = "# $username\n";

   foreach ($index as $link)
      $body .= "=> $link".".gmi $link\n";

   $outIndexFile = $prevuserDir . DIRECTORY_SEPARATOR . 'index.gmi';

   createMissingDirsForFilePath($outIndexFile);
   echo "\nSaved index: $outIndexFile"; 
   file_put_contents($outIndexFile, $body);

}

function clean($s) {
    $s = (string)$s;
    $s = trim($s);
    #cleanup
    $s = preg_replace('~[^A-Za-z0-9._-]+~', '_', $s);
    return $s === '' ? 'unknown' : $s;
};

function createMissingDirsForFilePath($filePath)
{
   $dir = dirname($filePath);

   if (!is_dir($dir)) {
      if (!mkdir($dir, 0775, true)) {
        throw new RuntimeException("Failed to create dir: $dir");
      }
  }

}


?>
