#!/usr/pkg/bin/bash
# This is a simple script developed by [email protected]
# to facilitate easy phlog authoring.  For used primarily
# on sdf.org. Minor updates may be needed for your environment.
# Feel free to improve upon this script and use it if you feel
# it will help in your phlogging.
#
# Comments and shout outs are always welcome. I can be
# reached at : [email protected]

cd $HOME/scripts/phlogwriter

# Setup some variables
filedate=`date +%Y-%m-%d-%H%M`
date=`date +%Y.%m.%d`
time=`date +%H:%M`
author="[email protected]"

# Setup details about Gopher/Phlog Location
dir="${HOME}/gopher/phlog/"
gophermap=${dir}gophermap

# Welcome/Info
echo " "
echo "Welcome to the Snowcrash Phlogwriter"
echo "-----------------------------------------------------------------"
# Get some input
read -p "Enter phlog title: " title
read -p "Enter current mood: " mood
read -p "Continue? (Y/N): " confirm && [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]] || exit 1

# Convert input to upper case for consistency
titlefile=`echo $title | tr " " "_" | tr [a-z] [A-Z]`
moodupper=`echo $mood | tr [a-z] [A-Z]`
titleupper=`echo $title | tr [a-z] [A-Z]`

# Setup out file name with date/title info
filename=${filedate}_${titlefile}.txt

# Setup the phlog file for editing
# If you want to use a custom header, create a file called head.txt with what
# you want to use. Same for footer below.
#
cat head.txt > $dir$filename
#echo "============================================================" >> $dir$filename
echo "  DATE : ${date}" >> $dir$filename
echo "  TIME : ${time}" >> $dir$filename
echo "AUTHOR : ${author}" >> $dir$filename
echo "  MOOD : ${moodupper}" >> $dir$filename
echo " TITLE : ${titleupper}" >> $dir$filename
echo " " >> $dir$filename
#echo "============================================================" >> $dir$filename
cat head.txt >> $dir$filename

# Check if gophermap in phlog directory does/does not exist and append to top
# the new phlog being authored
#
if [ -f $gophermap ]; then
          # If phlog gophermap exists, add entry to top with necessary tab character
      sed -i '1,9d' $dir/gophermap
          echo -e "0[${date} ${time}] ${titleupper}\t${filename}" | cat - $gophermap > temp && mv temp $gophermap
          cat head-phlog.txt | cat - $gophermap > temp && mv temp $gophermap
  else
          # If phlog gophermap does not exist, let's create one
      sed -i '1,9d' $dir/gophermap
      echo -e "0[${date} ${time}] ${titleupper}\t${filename}" > $gophermap
          cat head-phlog.txt | cat - $gophermap > temp && mv temp $gophermap
fi

# Update the Last Updated field in my main gophermap file
sed -i "s/LAST UPDATED.*/LAST UPDATED: ${date}/g" $HOME/gopher/gophermap

# Open temporary post body with editor of choice
#vi temp-write < `tty` > `tty`
nano temp-write

# Format temporary poste body to 67 characters
gfmt -w 67 -g 67 -u temp-write > temp-write-gfmt

# Add Footer
cat foot.txt >> temp-write-gfmt

# Append formated body with footer to the header file
cat temp-write-gfmt >> $dir$filename


# Cleanup
rm temp-write*

# Make sure everything is permissioned correctly
mkgopher -p

# End