#!/bin/sh

# THANKS:
# * georg (pheed) - gopher://sdf-eu.org:70/1/users/georg/

# Written by Christian Mongeau in 2009.
# This script is hereby placed in the public domain.

# This file must be placed in
# /ftp/pub/users/$USER/$LOG_DIRECTORY
# where $LOG_DIRECTORY is the first variable declared in ugo.sh

####################################################################
# Variables used in this file that are found in .ugorc:
# GOPHER_ROOT GOPHER_LOG OTHER_CHARS MAX_CHARS COMM_STRING
# DATE_FORMAT FILE_EXT PHEED
####################################################################

GOPHER_LOG=$(dirname $0)
GOPHER_ROOT=$(dirname $GOPHER_LOG)
$GOPHER_LOG/.ugorc

LOG_PATH=$(echo "$GOPHER_LOG" | sed 's@/ftp/pub@@')

file=$(echo "$1" | cut -f1)
name=$(echo "$1" | cut -f2)

error_msg() {
       echo 'i SORRY           error.host      1'
       echo 'i                 error.host      1'

       case $error_type in
               no_name)
                       echo 'i Enter your comment as           error.host      1'
                       echo 'i                 error.host      1'
                       echo 'i     YOU: Hey! Whats up??                error.host      1'
                       echo 'i                 error.host      1'
                       echo 'i where YOU is your (nick)name (case insensitive).                error.host      1'
                       echo 'i         error.host      1'
                       echo 'i         error.host      1'
                       echo 'i (Currently Firefox is not supported.)           error.host      1';;
               long)
                       echo 'i Your comment is too long.               error.host      1'
                       echo "i Please keep it below $MAX_CHARS characters.             error.host      1";;
               bad_chars)
                       echo "i         Only alphanumeric characters and $OTHER_CHARS are OK.           error.host      1";;
       esac

       echo 'i                 error.host      1'
       #echo "1Go back ${LOG_PATH}/$file.$FILE_EXT     sdf.lonestar.org        70"
       exit 1
}

pheed() {
       # gopher://sdf-eu.org:70/1/users/georg/
       tmp_str=$(date +%s)
       echo "[$comment_date] $update_type $comment" > /tmp/$tmp_str

       cat $GOPHER_LOG/$PHEED >> /tmp/$tmp_str
       cat /tmp/$tmp_str > $GOPHER_LOG/$PHEED
       rm /tmp/$tmp_str
}

shift # Removing file AND name

if [ $(echo "$@" | grep -c "[^${OTHER_CHARS} a-zA-Z0-9  ]") -ne 0 ] ; then
       error_type=bad_chars ; error_msg
else
       comment=$(echo "$@" | sed "s/^[^a-zA-Z0-9][^a-zA-Z0-9]*//g")
fi


if [ ${#comment} -gt $MAX_CHARS ] ; then
       error_type=long ; error_msg
fi

if [ $(echo "$name" | cut -c ${#name}) != ':' ] ; then
       error_type=no_name ; error_msg
else
       name=$(echo "$name" | tr -d ':')
fi

grep "^i$COMM_STRING            error.host      1" \
   ${GOPHER_LOG}/${file}.$FILE_EXT 2>&1 1>/dev/null
if [ $? -ne 0 ] ; then
       echo 'i         error.host      1' >> ${GOPHER_LOG}/${file}.$FILE_EXT
       echo "i$COMM_STRING             error.host      1" >> ${GOPHER_LOG}/${file}.$FILE_EXT
       echo 'i         error.host      1' >> ${GOPHER_LOG}/${file}.$FILE_EXT
fi

comment_date=$(date +"$DATE_FORMAT")
echo "i $name, $comment_date            error.host      1" >> ${GOPHER_LOG}/${file}.$FILE_EXT
echo "i $comment                error.host      1" >> ${GOPHER_LOG}/${file}.$FILE_EXT
       echo 'i         error.host      1' >> ${GOPHER_LOG}/${file}.$FILE_EXT

if [ -f "$GOPHER_LOG/$PHEED" ] ; then
       update_type='(Comment)'
       pheed
fi

cat <<EOB
iThanks!                error.host      1
i(Go back and refresh your browser if you don't see your comment)               error.host      1
EOB