#!/bin/bash
# note - Juno Note Script
# Copyright 2008, Sean B. Palmer, inamidst.com
# Licensed under the Eiffel Forum License 2.

cd ~/web/inamidst.com/www/whits

function doc() {
   echo "Usage: $0 [options] [ <name> [ <title> ] ]"
   echo "Create a new note, with optional filename title"
   echo "   -e   Edit: change existing, don't create a new note"
   echo "   -h   Help: print out this message"
}

function error() { 
   echo "Error: $@. Try '$0 -h' for help" >&2
   exit 1
}

Options=$(getopt eh $* 2>&1) || error "$Options"
set -- $Options

while true
do test ! -z "$1" || break
   case $1 in
      -h) doc; exit;; 
      -e) Edit=1; shift;;
      --) shift; break;; 
      -*) error "Unknown argument";; 
   esac
done

function ordinal() {
   case $1 in
      *11|*12|*13) echo ${1}th;; 
      *1) echo ${1}st;; 
      *2) echo ${1}nd;; 
      *3) echo ${1}rd;; 
      *) echo ${1}th;;
   esac
}

Year=$(date -u +%Y)
Month=$(date -u +%m)
Day=$(date -u +%d)
Date="$(ordinal ${Day#0}) $(date -u +%B) $Year"

function major() {
   Name=$1; shift
   Title=${@:--}
   Filepath=$Year/$Name.html

   if [[ ! -d $Year ]]
   then mkdir $Year || error "Couldn't make $Year/"
        cp code/juno/style.css $Year/style.css
        cp code/juno/contents.cgi.txt $Year/index.cgi
        chmod 755 $Year/index.cgi
   fi

   if [[ ! -f $Filepath ]]
   then echo "<!DOCTYPE html>
<title>$Title</title>
<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">
<link rel=\"feed\" type=\"application/atom+xml\" href=\"../feed\">

<section>


</section>

<address>
<a href=\"http://inamidst.com/sbp/\">Sean B. Palmer</a>, $Date
</address>

<nav>
<p><a href=\"..\" title=\"Gallimaufry of Whits\">Whits</a> · <a 
href=\"./#$Name\" title=\"Whits Contents\">Contents</a></p>
</nav>" > $Filepath
      echo Created $Filepath
   fi

   Lines=$(echo $(wc -l $Filepath) | cut -d ' ' -f 1)
   e +$(($Lines - 11)) $Filepath

   echo Post-processing $Filepath
   code/juno/process $Filepath
   chmod 644 $Filepath
}

function minor() {
   Minutes=$(date -u +%H%M)
   Name=${1:-$Minutes}
   Filepath=$Year/$Month/$Day.html

   if [[ ! -d $Year/$Month ]]
   then mkdir -p $Year/$Month || error "Couldn't make $Year/$Month/"
   fi

   if [[ ! -f $Filepath ]]
   then echo "<!DOCTYPE html>
<title>$Date</title>
<link rel=\"stylesheet\" type=\"text/css\" href=\"../style.css\">
<link rel=\"feed\" type=\"application/atom+xml\" href=\"../../feed\">

<section>
</section>

<address>
<a href=\"http://inamidst.com/sbp/\">Sean B. Palmer</a>, $Date
</address>

<nav>
<p><a href=\"../..\" title=\"Gallimaufry of Whits\">Whits</a> · <a 
href=\"../#$Month$Day\" title=\"Whits Contents\">Contents</a></p>
</nav>" > $Filepath
      echo Created $Filepath
   fi

   # This gets a bit messy here
   Base="http://inamidst.com/whits/$Year/$Month/$Day"
   if [[ -z $Edit ]]
   then Now=$'\\\n'
        Link="<a href=\"$Base#$Name\">$Now$(date -u +'%H:%M UTC')</a>"
        Now="$Now<p id=\"$(date -u +%H%M)\" class=\"utc\">$Link:</p>"
        Breaks=$'\\\n\\\n\\\n\\\n'
        sed -i '' "s!</section>!$Now$Breaks</section>!" $Filepath
   fi

   Lines=$(echo $(wc -l $Filepath) | cut -d ' ' -f 1)
   e +$(($Lines - 11)) $Filepath

   echo Post-processing $Filepath
   code/juno/process $Filepath
   chmod 644 $Filepath
}

# @@
# ContentBase=
# ContentTitle=
# ContentName=
# AuthorName=
# AuthorHomepage=
# JunoPath=

if (( $# > 1 ))
then major $@
else minor $@
fi

# EOF
