#!/bin/sh

meta="/var/cosmic/files/metadata.yml"
ebook="$HOME/cosmic.txt"
epub="$HOME/cosmic.epub"
intro="/var/gopher/intro.gophermap"

filelist=$(tac /var/gopher/listing.gophermap) 

if [ -f "$ebook" ]; then
  rm "$ebook"
fi


printf "# Introduction\\n\\n" >> "$ebook"
sed 's/^/    /' "$intro" >> "$ebook"

IFS='
'
for log in $filelist; do
  file=$(printf "%s" "$log" | awk -F"\\t" '{print "/var/gopher" $2}')
  title=$(printf "%s" "$log" | awk -F"\\t" '{print $1}' | sed 's/^.//')

  {
    printf "# %s\\n\\n" "$title"
    sed 's/^/    /' "$file" 
  } >> "$ebook"
done

pandoc -f markdown+hard_line_breaks -o "$epub" "$meta" --wrap=none --toc "$ebook"
rm "$ebook"
