#!/bin/bash
# gauntlet's micro blog
# by Gauntlet O. Manatee
# 2016-07-15

if [ -z "$1" ]; then
  echo "Type your new entry, then hit return."
  read typed
else
  typed=`cat $1 | perl -pe "s/\n/ /"`
fi

date=`date --utc +%Y-%m-%d\ %H:%M`
id=`date --utc +%Y%m%d%H%M%S`

entry='  <div class="ew"><span id="'"$id"'" class="date"><a href="#'"$id"'">'"$date"'</a> </span><span>'"$typed"'</span></div>'
line=`grep -n -m 1 'class="ew"' /home/gauntlet/public_html/gmb/index.html | cut -d: -f1`
sed -i "$line i $entry" /home/gauntlet/public_html/gmb/index.html

length=$(echo $typed | wc -w)
if [ "$length" -gt "5" ]
then
  title="`echo $typed | sed -e 's/<[^>]*>//g' | cut -d' ' -f-5` ..."
else
  title="`echo $typed | sed -e 's/<[^>]*>//g'`"
fi

link='http://ctrl-c.club/~gauntlet/gmb#'"$id"
rss='<item>\n<title>'"$title"'</title>\n<link>'"$link"'</link>\n<guid>'"$link"'</guid>\n<description><![CDATA['"$typed"']]></description>\n</item>'

rline=`grep -n -m 1 '<item>' /home/gauntlet/public_html/gmb/rss.xml | cut -d: -f1`
sed -i "$rline i $rss" /home/gauntlet/public_html/gmb/rss.xml
