#!/bin/bash
echo "Content-type: text/html"
echo ""

xml_next () {
       local IFS='>'
       read -d '<' TAG VALUE
}

cat <<EOT
<!DOCTYPE html>
<html>
<head>
       <meta name="viewport" content="width=device-width, initial-scale=1">
       <title>Blog post list</title>
       <link rel="stylesheet" type="text/css" href="/style/style.css">

</head>
<body>
   <div class="container">
               <div class="links">
                       <a href="https://teodozjusz.tilde.team/">Main page</a>
                       <a href="https://teodozjusz.tilde.team/cgi-bin/list.sh">CGI Scripts</a>
                       <a href="mailto:[email protected]">Mail me</a>
               </div>
               <hr>
               <div class="content">
                       <h1>Blog</h1>
EOT

for post in $(ls posts | tac | grep "post$"); do
       echo '<div class="post_element">'
       cat posts/$post | while xml_next; do
               if [ $TAG = "h1" ]; then
                       echo '<h3><a href="posts/post.sh?'$post'">'$VALUE'</a></h3>'
               fi
               if [ $TAG = "p" ]; then
                       echo '<p>'$VALUE'</p>'
                       echo '<hr>'
                       break
               fi
       done
       echo '</div>'

done

cat <<EOT

               </div>

       </div>

</body>
</html>
EOT