#!/bin/sh
# Script for new posts
FILE = /tmp/sufyaan-website-post$( date +%d) .md
ROOT = ~/docs/sys/gits/personal/sufyaan-website
echo -ne "- [ ] Linux\n- [ ] Software\n- [ ] Technology\n- [ ] Design\n- [ ] Guides\n- [ ] Cars\n- [ ] Literary\n- [ ] Updates\n- [ ] Other" > $FILE
nvim $FILE
CATPG = " $( grep 'X' $FILE | cut -d' ' -f3) "
CATPGL = " $( echo $CATPG | tr ' ' '\n' | sed 's/[A-Z]/\L&/g' ) "
CATSTR = " $( echo $CATPG | tr -d \\ n | sed 's/[A-Z]/ \&\& &/g' ) "
CATFIN = " $( echo $CATSTR | cut --complement -d' ' -f1 | sed 's/\(Linux\|Software\|Technology\|Design\|Guides\|Literary\|Other\)/<a style="color:#ff79c6" href="..\/blog\/&\/"><strong>&<\/strong><\/a>/g' | sed 's/\/blog\/\(Linux\|Software\|Technology\|Design\|Guides\|Literary\|Other\)/\L&/g' | sed 's/\//\\\//g' | sed 's/\&\&/\\\&\\\&/g' ) "
POSTED = $( date +'%d %B %Y' )
read -p "Enter post title: " TITLE
read -p "Enter post relative URL: " URL
read -p "Enter reading time: " READ
read -p "Enter previous post: " PREVPOST
EMAIL = $( echo $TITLE | sed 's/ /%20/g' )
cd $ROOT
mkdir -p $URL
cp template.html $URL /index.html
# Replacing placeholders with values in new post
sed -i " s/POST TITLE/ $TITLE /g " $URL /index.html
sed -i " s/POST DATE/ $POSTED /g " $URL /index.html
sed -i " s/READING/ $READ /g " $URL /index.html
sed -i " s/MAILSUBJECT/ $EMAIL /g " $URL /index.html
sed -i " s/URL/ $URL /g " $URL /index.html
sed -i " s/PREVIOUSPOST/ $PREVPOST /g " $URL /index.html
sed -i " s/POSTCATEGORIES/ $CATFIN /g " $URL /index.html
ORIGINALFOOTER = " - <a.*terminal.*First Post.*><\/svg>"
NEWFOOTER = " - <a style='color:#bd93f9' href=\"..\/ $URL \/\">Next Post<\/a> <svg class='footer-posts' xmlns='http:\/\/www.w3.org\/2000\/svg' viewBox='0 0 48 48'><mask id='ipSRightOne0'><path fill='#fff' stroke='#fff' stroke-linejoin='round' stroke-width='4' d='m20 12l12 12l-12 12V12Z'\/><\/mask><path fill='#bd93f9' d='M0 0h48v48H0z' mask='url\(\#ipSRightOne0\)'\/><\/svg><\/p> "
sed -i " s/ $ORIGINALFOOTER / $NEWFOOTER /g " $PREVPOST /index.html
# Adding to categories
DATE = $( date +%d/%m/%y)
CATREP = " \t\t<li><code class='code-posts'> $DATE <\/code> - <a style='color:#bd93f9' href='\.\.\/\.\.\/ $URL \/'> $TITLE <\/a><\/li> "
CATREPBLOG = " \t\t<li><code class='code-posts'> $DATE <\/code> - <a style='color:#bd93f9' href='\.\.\/ $URL \/'> $TITLE <\/a><\/li> "
CATREPSITEMAP = " \t\t<li><a style='color:#bd93f9' href='../ $URL /'> $TITLE </a></li> "
#for LINE in $CATPGL
#do
#FILE=$ROOT/blog/$LINE/index.html
#sed -i '/<ul class="posts">/{G;}' $FILE
#sed -i 's/^\s*$/EMPTYEMPTYEMPTY/g' $FILE
#sed -i "s,EMPTYEMPTYEMPTY,$CATREP,g" $FILE
#done
#
#sed -i '/<ul class="posts">/{G;}' blog/index.html
#sed -i 's/^\s*$/EMPTYEMPTYEMPTY/g' blog/index.html
#sed -i "s,EMPTYEMPTYEMPTY,$CATREPBLOG,g" blog/index.html
#
#sed -i '/<ul class="posts">/{G;}' sitemap/index.html
#sed -i 's/^\s*$/EMPTYEMPTYEMPTY/g' sitemap/index.html
#sed -i "s,EMPTYEMPTYEMPTY,$CATREPSITEMAP,g" sitemap/index.html
#
#sed -i '/<ul class="pane">/{G;}' /index.html
#sed -i 's/^\s*$/EMPTYEMPTYEMPTY/g' sitemap/index.html
#sed -i "s,EMPTYEMPTYEMPTY,$CATREPSITEMAP,g" sitemap/index.html
# Removing temporary file
rm $FILE