#!/bin/ksh
#	
#	CALEDIT
#	Last Modification:  February 2000
#                           August 2007/TS
#                           - added mailing of diffs to a list
##################################################################
#
#	Check that the proper calling sequence was supplied
#

THEEDITOR=$1
case ${THEEDITOR} in
   "vi"   ) echo " "
	    echo "Valid option supplied..."
	    echo " ";;
   "text" ) echo " "
	    echo "Valid option supplied..." 
	    echo " " ;;
   "emacs" ) echo " "
	    echo "Valid option supplied..."
	    echo " " ;;
   "emacsnw" ) echo " "
	    echo "Valid option supplied..."
	    echo " " ;;
    *     ) echo " "
	    echo "WARNING:"
	    echo "           Calling sequence is:  caledit vi (vi editor)"
	    echo "                           OR :  caledit text (text editor)"
	    echo "                           OR :  caledit emacs (emacs editor)"
            echo "  " 
	    exit ;;
esac

#
#       CHECK IF a calendar.lock file exists, if it does, throw out 
#	the user immediately.  OTHERWISE, create a calendar.lock file
#	to prevent other people from editing.
#

CALPATH=${HOME}/public_html
CALFILE=${CALPATH}/calendartest.html
#CALFILE=${CALPATH}/calendar.html
CALLOCKFILE=${CALPATH}/calendar.lock
   if [[ -a calendar.lock ]]; then
       echo " "
       echo "WARNING:   File is being edited by another user !!!! "
       echo "           CALENDAR.LOCK file exists... "
       echo "           Try again later...!! "
       echo " "
       exit
   else
       touch ${CALLOCKFILE}

#       CALEMAIL="soc@soc.nascom.nasa.gov"
	CALEMAIL="tsiili@esa.nascom.nasa.gov"
        CALARCHIVEPATH=${HOME}/old_calendars_test
#       CALARCHIVEPATH=${HOME}/old_calendars
        CALCURRENT=${CALARCHIVEPATH}/`date "+%y%m%d_%H%M"`
       cp ${CALFILE} ${CALCURRENT}

#        case ${THEEDITOR} in
#           "vi"   ) (vi ${CALFILE})  ;;
#           "text" ) xedit ${CALFILE} ;;
#           "emacs" ) emacs ${CALFILE}  ;;
#           "emacsnw" ) emacs -nw ${CALFILE}  ;;
#        esac
       case ${THEEDITOR} in
          "vi"   ) THEEDITOR2="vi" ;;
          "text" ) THEEDITOR2="xedit" ;;
          "emacs" ) THEEDITOR2="emacs" ;;
          "emacsnw" ) THEEDITOR2="emacs -nw" ;;
       esac
       THEEDITOR=${THEEDITOR2}
       ${THEEDITOR} ${CALFILE}

       CALDIFF=${CALPATH}/calendar.diff
       CALDIFFDELOLD=${CALPATH}/calendar_del_old.txt
       CALDIFFADDNEW=${CALPATH}/calendar_add_new.txt
       CALCOMMENTS=${CALPATH}/calendar_comments.txt
       CALCOMMENTSHELP=${CALPATH}/calendar_comments_help.txt

# Create the diff file and test the return code of the process
#      diff -i -U 5 ${CALCURRENT} ${CALFILE} > ${CALDIFF}
       diff -i ${CALCURRENT} ${CALFILE} > ${CALDIFF}
       if [ $? -eq 1 ]; then

# The grep statements are used to filter out the names of the compared files from the beginning.
#	   cp -p ${CALDIFF} ${CALDIFF}.bak
	   awk 'BEGIN {print "Deleted lines or old versions of changed lines:\n"}{if ($1 == "<") print} END {print ""}' < ${CALDIFF} > ${CALDIFFDELOLD}
	   awk 'BEGIN {print "Added lines or new versions of changed lines:\n"}{if ($1 == ">") print} END {print ""}' < ${CALDIFF} > ${CALDIFFADDNEW}
#	   grep -v ^--- ${CALDIFF}.bak | grep -v ^+++ | grep -v ^@@ > ${CALDIFF}
	   cat ${CALDIFFDELOLD} ${CALDIFFADDNEW} > ${CALDIFF}
	   less ${CALDIFF}
	   echo "Send the change list above to "${CALEMAIL}" (y/n)?"
	   read YESNO
	   if [[ ${YESNO} = "y" ]]; then
	       echo "Replace this text with your comments." > ${CALCOMMENTS}
	       echo "--------------------" >> ${CALCOMMENTS}
#  	       echo "--------------------" > ${CALCOMMENTSHELP}
#  	       echo "NOTE: removed and changed original lines are marked by a '-' while" >> ${CALCOMMENTSHELP}
#  	       echo "added lines and new versions of changed lines are marked by  a  '+'." >> ${CALCOMMENTSHELP}
#  	       echo "Both versions of changed lines appear in the output." >> ${CALCOMMENTSHELP}
# 	       echo "Line with @@ indicates the line numbers of changed lines." >> ${CALCOMMENTSHELP}
#	       echo "--------------------" >> ${CALCOMMENTSHELP}
#	       echo ""  >> ${CALCOMMENTSHELP}
	       ${THEEDITOR} ${CALCOMMENTS}
	       echo "E-mailing calendar changes to"
	       echo ${CALEMAIL}
#	       mailx -s "SOHO calendar changed" ${CALEMAIL} < ${CALDIFF}
	       cat ${CALCOMMENTS} ${CALDIFF} | mailx -s "SOHO calendar changed" ${CALEMAIL}
	   fi
       else
	   rm ${CALDIFF} ${CALCOMMENTS}
	   echo "No changes to "${CALFILE}
       fi
   fi
#
#	REMOVE CALENDAR.LOCK FILE
#
   echo "Removing calendar.lock file..."
   rm calendar.lock

