#!/bin/bash
Extension="soc"
InputDir="/home/pgadey/public_html/soc/"
OutputDir="/home/pgadey/public_html/soc/"
IndexWebPath="https://ctrl-c.club/~pgadey/soc/index.html"
DATE=$(date +%F)
TIME=$(date +%R)
   #echo $#; # the number of arguments?
   case "$1" in 
   -e|--edit) 
   	vim $InputDir/$DATE.soc;;
   -b|--build)
    echo "You requested build." ;;
   *)
    echo "<li><a id=\"$DATE-$TIME\" href=\"$IndexWebPath#$DATE-$TIME\">[$TIME]</a> $* </li>" >> $InputDir/$DATE.$Extension ;;
   
   esac
cat >$OutputDir/index.html <<EOF
<!DOCTYPE html>

<html>
<head>
<title>~pgadey's micro-blog</title>
<meta charset="UTF-8" />
<link rel="stylesheet" type="text/css" href="screen.css">
</head>
<body>
<h1>~pgadey's &micro;-blog</h1>
<p>
<a href="http://ctrl-c.club/~pgadey/">home</a>
<a href="#about">about</a>
<a href="https://ctrl-c.club/~pgadey/soc/index.xml">rss <img src="rss.svg" alt="rss feed icon" width="15" style="width: 15px; top: 3px; position: relative;"></a>
</p>
<hr>
EOF
cat >$OutputDir/index.xml <<EOF
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>~pgadey's micro-blog</title>
<link>http:s//ctrl-c.club/~pgadey/</link>
<description>Recent content on ~pgadey's micro-blog</description>
<generator>the updated soc.sh script</generator>
<language>en-us</language>
<atom:link href="https://ctrl-c.club/~pgadey/index.xml" rel="self" type="application/rss+xml"/>
EOF

# RSS requires dates in RFC822 format.
echo "<lastBuildDate>$(date +%a,\ %d\ %b\ %Y\ %T\ %z)</lastBuildDate>" >> $OutputDir/index.xml
for file in $(ls -r $InputDir/*.$Extension); do
#for file in $InputDir/*.$Extension; do
BASENAME=$(basename --suffix=.$Extension $file);

PostDATE=$BASENAME;
echo "<h2><a id=\"$PostDATE\" href=\"#$PostDATE\">$PostDATE</a></h2>" >> $OutputDir/index.html
echo "<ul>" >> $OutputDir/index.html
sort -r $file >> $OutputDir/index.html
echo "</ul>" >> $OutputDir/index.html
echo "<item>" >> $OutputDir/index.xml
echo "<title>micro-blog post</title>" >> $OutputDir/index.xml
echo "<pubDate>$(date --date="$PostDATE" --rfc-email)</pubDate>" >> $OutputDir/index.xml
echo "<link>" >> $OutputDir/index.xml
echo "$IndexWebPath#$PostDATE" >> $OutputDir/index.xml
echo "</link>" >> $OutputDir/index.xml
echo "<description>" >> $OutputDir/index.xml
#sed 's/<[^>]*>//g' $file >> $OutputDir/index.xml
echo "micro-blog post" >> $OutputDir/index.xml
echo  "</description>" >> $OutputDir/index.xml
echo "</item>" >> $OutputDir/index.xml
done
cat >>$OutputDir/index.xml <<EOF
</channel>
</rss>
EOF
cat >>$OutputDir/index.html <<EOF
<hr>
<p id="about">
Stats:
EOF
echo $(ls -1 $InputDir/*.$Extension | wc -l) "days with records." >> $OutputDir/index.html
echo $(wc -c $InputDir/*.$Extension | tail -n1) " characters." >> $OutputDir/index.html
cat >>$OutputDir/index.html <<EOF
This page was generated by a modified version of <a href="https://tilde.center/~papa/soc.html">soc</a> written by papa@tilde.center.
For details about the modification, check out <a href="https://ctrl-c.club/~pgadey/notes/computers/#soc">my write-up</a>.
</p>
<div id="sitelink" style="width=100%;text-align:center;">
<a href="https://ctrl-c.club/"><em>ctrl-c.club</em></a><br>
</div>
</body>
</html>
EOF
 
cp $OutputDir/index.html /home/pgadey/public_html/stream-of-consciousness.html
