#!/bin/sh

run_user=$(id -u)
if [ "$run_user" -eq 0 ]; then
  file_rss="/var/www/html/rss.xml"
  gopher_rss="/var/gopher/rss.xml"

  ###################################################################### 
  ############################# HTML VERSION ########################### 
  ###################################################################### 

  # Add header info to xml output
  {
    printf '<?xml version="1.0"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel>'
    printf '<atom:link href="https://cosmic.voyage/rss.xml" rel="self" type="application/rss+xml" />'
    printf "\\n<title>Cosmic Voyage</title>\\n"
    printf "<link>https://cosmic.voyage</link>\\n"
    printf "<description>Messages from the human stellar diaspora</description>\\n"
  } > "${file_rss}"

  # Loop through listings gophermap
  loop=0
  while read -r line; do
    loop=$((loop+1))
    if [ "$loop" -lt 50 ]; then
      log=$(printf "%s" "$line" | awk -F'\t' '{print $2}')
      title=$(printf "%s" "$line" | awk -F'\t' '{print $1}' | sed 's|^.||')
      owner=$(stat -c %U "/var/gopher${log}")

      # print item entry for each log
      {
        printf "<item>\\n"
        printf "  <title>%s</title>\\n" "$title"
        printf "  <author>%s@cosmic.voyage (%s)</author>\\n" "$owner" "$owner"
        printf "  <link>https://cosmic.voyage%s</link>\\n" "$(printf "%s" "$log" | sed 's|.txt$|.html|' | sed 's|\ |%20|g')"
        printf "  <guid>https://cosmic.voyage%s</guid>\\n" "$(printf "%s" "$log" | sed 's|.txt$|.html|' | sed 's|\ |%20|g')"
        printf "  <pubDate>%s GMT</pubDate>\\n" "$(date -d "$(stat -c %y "/var/gopher${log}")" +'%a, %d %b %Y %H:%M:%S')"
        printf "  <description><![CDATA[<pre>\\n"
        sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g; s/"/\&quot;/g; s/'"'"'/\&#39;/g' "/var/gopher${log}"
        printf "</pre>]]></description>\\n"
        printf "</item>\\n"
      } >> "${file_rss}"
    fi
  done < "/var/gopher/listing.gophermap"

  # close up the footer
  {
    printf "</channel>\\n"
    printf "</rss>\\n"
  } >> "${file_rss}"


  ###################################################################### 
  ########################## GOPHER  VERSION ########################### 
  ###################################################################### 

  # Add header info to xml output
  {
    printf '<?xml version="1.0"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel>'
    printf '<atom:link href="gopher://cosmic.voyage/0/rss.xml" rel="self" type="application/rss+xml" />'
    printf "\\n<title>Cosmic Voyage</title>\\n"
    printf "<link>gopher://cosmic.voyage</link>\\n"
    printf "<description>Messages from the human stellar diaspora</description>\\n"
  } > "${gopher_rss}"

  # Loop through listings gophermap
  while read -r line; do
    log=$(printf "%s" "$line" | awk -F'\t' '{print $2}')
    title=$(printf "%s" "$line" | awk -F'\t' '{print $1}' | sed 's|^.||')
    owner=$(stat -c %U "/var/gopher${log}")

    # print item entry for each log
    {
      printf "<item>\\n"
      printf "  <title>%s</title>\\n" "$title"
      printf "  <author>%s@cosmic.voyage (%s)</author>\\n" "$owner" "$owner"
      printf "  <link>gopher://cosmic.voyage/0%s</link>\\n" "$log"
      printf "  <guid>gopher://cosmic.voyage/0%s</guid>\\n" "$log"
      printf "  <pubDate>%s GMT</pubDate>\\n" "$(date -d "$(stat -c %y "/var/gopher${log}")" +'%a, %d %b %Y %H:%M:%S')"
      printf "  <description><![CDATA[<pre>\\n"
      cat "/var/gopher${log}"
      printf "</pre>]]></description>\\n"
      printf "</item>\\n"
    } >> "${gopher_rss}"
  done < "/var/gopher/listing.gophermap"

  # close up the footer
  {
    printf "</channel>\\n"
    printf "</rss>\\n"
  } >> "${gopher_rss}"

else
  exec sudo "$0" "$@"
fi
