#!/bin/sh
# phlogit-1.3 created by beaker@simple  Dec 29 01:00:08 PST 2003
#
###################################################################
# 'phlogit' creates a text file named "mm-dd-year" in the TARGETDIR
# directory, changes the permissions to "644" and opens it with 'vi'.
# After file is closed, it runs  'ispell -x' and 'fmt' and prints
# file to screen for review.  If anything other than "N" or "n" is
# entered the file is reopened for corrections, otherwise the script
# exits.
#
# ----------Revision history----------
# phlogit-1.3   Dec 29 01:00:08 PST 2003
#       added assignable EDITOR & SPELLER variables
#       reinstigated temp file for 'fmt' option (bug fix)
# phlogit-1.2   Dec 24 22:57:57 PST 2003
#       added "do you want 'fmt'?" option
#       elliminated temp file when running 'fmt' option
#       added blank line offsets from header date
#       added "vi -c \$" to place cursor correctly
#       improved how append and overwrite are executed
# phlogit-1.1   Dec 4 16:54:26 PST 2003
#       changed target dir from current to TARGETDIR
# phlogit-1.0   Oct 4 01:50:53 PDT 2003
#       initial release
#
####################################################################
# Set target directory:
TARGETDIR=`/bin/pwd`

# Assign date-based file naming structure
ENTRY=`/bin/date +%m-%d-%Y`

# Set desired editor:
EDITOR='/usr/bin/vi -c $'

# Set disired spellchecker:
SPELLER='/usr/local/bin/ispell -x'

# ***** Don't modify below this line *******

read -p "Want to create a Phlog entry for today ($ENTRY)? (y/n):" do


CUR_DIR=`/bin/pwd`


case "$do" in
       [N,n])  /bin/echo "Fine - be that way."
               exit 0 ;;

       *)      cd $TARGETDIR
               /bin/echo "" ;;
esac

if [ -f ./${ENTRY} ]; then
       echo "A $ENTRY entry already exists. What do you want to do?"
       echo -n "[a]ppend, [o]verwrite, [c]ancel (a/o/c): "

       read what

       if [ $what = "c" ]; then
               cd $CUR_DIR
               exit 0

       elif [ $what = "o" ]; then
               /bin/rm -i ./$ENTRY

       elif [ $what = "a" ]; then
               /bin/echo "" >> ./$ENTRY
               /bin/echo "" >> ./$ENTRY
       fi
fi

if [ ! -f ./${ENTRY} ]; then
       /bin/date > ./$ENTRY
       /bin/echo "" >> ./$ENTRY
       /bin/echo "" >> ./$ENTRY
       /bin/chmod 644 ./$ENTRY
fi

while :
do

$EDITOR ./$ENTRY
$SPELLER ./$ENTRY

read -p "Want 'fmt'ing? <WARNING: will wrap code samples> (y/n): " fmtanswer
       case "$fmtanswer" in
               y)      /bin/cp ./$ENTRY ./tmp
                       /usr/bin/fmt ./tmp > ./$ENTRY
                       /bin/rm ./tmp
                       ;;

               *)      ;;
       esac

/usr/bin/clear
/bin/echo ------------ your `/bin/date +%m-%d-%Y` Phlog entry ------------
/bin/echo
/bin/cat ./$ENTRY
/bin/echo -----------------------------------------------------
/bin/echo ""
read -p "Is this OK? (y/n): " answer
       case "$answer" in
               y)      echo "Your phlog ENTRY has been written"
                       echo "Bye!"
                       cd $CUR_DIR
                       break ;;

               *)      /usr/bin/clear ;;
       esac
done