#!/bin/bash
main () {
    BLOG=$(zenity --entry \
        --width 500 \
        --title "BlogIT!" \
        --text "Enter the title of the Blog" \
        --entry-text "");
    if [ $? = 1 ] ; then
		exit
	fi

    DESC=$(zenity --entry \
        --width 500 \
        --title "BlogIT!" \
        --text "Enter a description" \
        --entry-text "");
    if [ $? = 0 ] ; then
        html
	elif [ $? = 1 ] ; then
		exit
	fi
}

html () {
    # Creates HTML file
    sed -e "s/BLOGTITLE/$BLOG/g; s/BLOGDATE/$(date '+%Y-%m-%d')/g; s/BLOGDESC/$DESC/g" ../blogs/template.html > ../blogs/$(date '+%Y-%m-%d').html
    # Adds link to ./blog.html page
    sed -i "/<!-- Future Blog posts go here -->/a  \\\t\t\t\t<li>$(date '+%Y-%m-%d') - <a href=\"blogs/$(date '+%Y-%m-%d').html\">$BLOG</a></li>" ../blog.html
       
    zenity --question \
        --width 300 \
        --title "BlogIT!" \
        --text "Would you like to add this to RSS?";

    if [ $? = 0 ] ; then
        rss
	elif [ $? = 1 ] ; then
		exit
	fi
}

rss () {
    # Adds an RSS Link to dvb.xml
    sed -i "/<!-- Future RSS posts go here-->/a \\\t<item>\n\t\t<title>$BLOG</title>\n\t\t<pubDate>$(LC_TIME=en_US date '+%d %b %Y %H:%M:%S %z')</pubDate>\n\t\t<guid>https://digitalvoid.xyz/blogs/$(date '+%Y-%m-%d').html</guid>\n\t\t<link>https://digitalvoid.xyz/blogs/$(date '+%Y-%m-%d').html</link>\n\t\t<description>$DESC</description>\n\t\t<author>lexian@digitalvoid.xyz</author>\n\t</item>" ../blogs/feed.xml
    exit
}

main
