#!/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
       CALDIFF=${CALPATH}/calendar.diff
       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}
       if [ $? -eq 1 ]; then
# The grep statements are used to filter out the names of the compared files from the beginning.
	   mv ${CALDIFF} ${CALDIFF}.bak
	   grep -v ^--- ${CALDIFF}.bak | grep -v ^+++ > ${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 "--------------------" > ${CALCOMMENTSHELP}
	       echo "Removed and changed original lines are marked by a '-' while" >> ${CALCOMMENTSHELP}
	       echo "added lines and changed new 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} ${CALCOMMENTSHELP} ${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

