#!/usr/bin/env bash

echo "Loading node"
. /var/www/.nvm/nvm.sh

baseurl="https://www.kitsu.red"
basepath="$(readlink -f .)"

makeblog() {
  # Generate the HTML posts
  echo "Generating HTML posts..."
  mkdir "$basepath/blog" &> /dev/null
  rm "$basepath"/blog/*.draft.html &> /dev/null
  parallel --bar --eta --jobs 4 '\
    pandoc {} -f gfm -t html \
      --standalone \
      --metadata=date:$( \
          date \
            --utc \
            --iso-8601 \
            --date=$( \
                basename {} \
                | cut -d"-" -f1-3 \
              ) \
          ) \
      --metadata=markdownfile:$( \
          echo -n {} | sed s@^\.\./@/@gm | sed s@^\./@/@gm \
        ) \
      --template ./misc/post-template.html \
      --highlight-style breezedark \
    | prettier \
      --parser html \
      --prose-wrap always \
    > ./blog/{/.}.html' \
    ::: ./md/blog/*.md

  rm "$basepath"/blog/*.draft.html &> /dev/null

  echo 'Generating RSS feed...'
  rss_temp="$(mktemp)"
  rss_path_rel="blog/rss.xml"
  rss_file="$basepath/$rss_path_rel"

  cat <<EOF > $rss_temp
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>kitsune&#8217;s blog</title>
    <link>$baseurl</link>
    <description>join me as i write about random stuff on my mind</description>
    <language>en-US</language>
    <lastBuildDate>$(date -R)</lastBuildDate>
    <atom:link href="$baseurl/$rss_path_rel" rel="self" type="application/rss+xml" />
EOF

  for f in "$basepath"/md/blog/*.md; do
    slug=$(basename "$f" .md)
    postdate=$(echo "$slug" | cut -d'-' -f1-3)

    # Skip drafts
    if [[ "$slug" == *draft* ]]; then
      continue
    fi

    # Generate the post's feed
    pandoc -f gfm -t html \
      --standalone \
      --variable url="$baseurl/blog/$slug" \
      --variable date="$(date -R --date=$postdate)" \
      --template "$basepath/misc/post-rss-template.xml" \
      "$f" >> $rss_temp
  done

  cat <<EOF >> $rss_temp
  </channel>
</rss>
EOF

  # Swap the temp file into the real file
  chmod 644 $rss_temp
  mv $rss_temp $rss_file
}

makenotes() {
  echo "Generating notes..."
  rm -rf "$basepath/notes.old" &> /dev/null
  rm -rf "$basepath/notes.tmp" &> /dev/null
  find ./md/notes -type f -name '*.md' \
  | parallel --bar --eta --jobs 4 '\
      outf="$(echo -n {.} | sed s@^\./md/notes@./notes.tmp@gm).html"; \
      mkdir -p "$(dirname "$outf")"; \
      pandoc {} -f gfm -t html \
        --standalone \
        --metadata=date:$( \
            date --iso-8601=seconds --reference={} \
          ) \
        --metadata=markdownfile:$( \
            echo -n {} | sed s@^\./@/@gm \
          ) \
        --template ./misc/note-template.html \
        --highlight-style breezedark \
      | prettier \
        --parser html \
        --prose-wrap always \
      > "$outf"'
  mv "$basepath/notes" "$basepath/notes.old" &> /dev/null
  mv "$basepath/notes.tmp" "$basepath/notes" &> /dev/null
}

makeblog
#makenotes

echo "All done!"
