#!/bin/bash
# Kenny Phloggins v0.0002
#
# ratfactor's phlog create/edit/preview/push script
#
#      "Out along the edges / Always where I burn to be
#    The further on the edge / The hotter the intensity
#              Highway to the Danger Zone
#              Right into the Danger Zone"
#                         -- Kenny Loggins, Danger Zone
#

echo
echo "Kenny Phloggins says, \"Welcome to the Danger Zone!\""
echo

SRC_MAINMAP=src/gophermap
PUB_MAINMAP=publish/gophermap
SRC_PHLOGDIR=src/phlog
PUB_PHLOGDIR=publish/phlog
FNAME="NONE" # selected phlog

function list {
       local map=$SRC_PHLOGDIR/gophermap
       echo "Phlogs read from $map:"
       # get list from phlog gophermap!
       local phlog_list=$(awk 'BEGIN {ORS=" "} /^0/ {print $NF}' $map)
       PS3="Select a phlog to edit: "
       select selected in $phlog_list
       do
               FNAME=$selected
               break
       done
}


function new_phlog {
       printf "(Short) title for new phlog: "
       read title
       local date=$(date +%Y-%m-%d)
       local day=$(date +%d)
       FNAME="$date-$(echo "$title" | tr '[:upper:] ' '[:lower:]-')"

       echo
       echo "Title: $title"
       echo "Fname: $FNAME"
       echo "Hit return to fly into the Danger Zone!"
       read nothing

       # append title to phlog and phlog map
       echo "$title" >> $SRC_PHLOGDIR/$FNAME
       echo "======" >> $SRC_PHLOGDIR/$FNAME
       local phlogmap=$SRC_PHLOGDIR/gophermap
       echo "0$day $title      $FNAME" >> $phlogmap
       vim $phlogmap
       cp $phlogmap $PUB_PHLOGDIR/gophermap
       less $PUB_PHLOGDIR/gophermap

       # update main site gophermap and review
       sed "s/<phlog_updated>/$date/" $SRC_MAINMAP > $PUB_MAINMAP
       less $PUB_MAINMAP

       # first time in phlog club, you *have* to phlog!
       edit
}

function edit {
       if [[ $FNAME = 'NONE' ]]
       then
               echo "You must first select a phlog to edit!"
               return
       fi

       local src=$SRC_PHLOGDIR/$FNAME
       local pub=$PUB_PHLOGDIR/$FNAME
       vim $src
       # make immediate backup!
       cp $src "${src}_bak$(date +%s)"
       # format and review
       #fmt -w67 $src > $pub
       ttt $src > $pub
       less $pub
       echo
       echo "$FNAME edited and formatted."
}

#function push_helper {
       # this function re-tries scp copy thrice
       #for i in 1 2 3
       #do
       #       echo "Pushing $1 to $2..."
       #       scp $1 $2 && break || sleep 5
       #done
#}

function push {
       if [[ $FNAME = 'NONE' ]]
       then
               echo "You must first select a phlog to push!"
               return
       fi

       # This is totally specific to the author (Ratfactor)
       # You will definitely need to edit this to make it
       # work for you!
       # As best I understand it, updating the root gophermap
       # will cause the SDF phlogroll to detect the update.
       echo "put $PUB_PHLOGDIR/$FNAME gopher/phlog/
       put $PUB_PHLOGDIR/gophermap gopher/phlog/
       put $PUB_MAINMAP gopher/" | sftp sdf
}

while true
do
       [[ $FNAME != NONE ]] && echo -e "Current entry: \e[33m$FNAME\e[0m"
       prompt="(n)ew (l)ist (v)iew (e)dit (p)ush (s)pellcheck (q)uit: "
       [[ $FNAME = NONE ]] && prompt="(n)ew (l)ist (q)uit: "
       echo -en "\e[32m$prompt\e[0m "
       read action
       case $action in
               n  ) new_phlog ;;
               l ) list ;;
               v ) less $PUB_PHLOGDIR/$FNAME ;;
               e ) edit ;;
               p ) push ;;
               s ) ispell $SRC_PHLOGDIR/$FNAME ;;
               q ) echo "Leaving the Danger Zone!"; exit ;;
               * ) echo "What? ";;
       esac
done