#!/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
#
case $1 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"
#       CALARCHIVEPATH=${HOME}/old_calendars_test
       CALARCHIVEPATH=${HOME}/old_calendars
       CALCURRENT=${CALARCHIVEPATH}/`date "+%y%m%d_%H%M"`
       cp ${CALFILE} ${CALCURRENT}
       case $1 in
          "vi"   ) (vi ${CALFILE})  ;;
          "text" ) xedit ${CALFILE} ;;
          "emacs" ) emacs ${CALFILE}  ;;
          "emacsnw" ) emacs -nw ${CALFILE}  ;;
       esac
       CALDIFF=${CALPATH}/calendar.diff
       diff ${CALCURRENT} ${CALFILE} > ${CALDIFF}
       if [ $? -eq 1 ]; then
	   less ${CALDIFF}
	   echo "Send the change list above to "${CALEMAIL}" (y/n)?"
	   read YESNO
	   if [[ ${YESNO} = "y" ]]; then
	       echo "E-mailing calendar changes to"
	       echo ${CALEMAIL}
	       mailx -s "SOHO calendar changed" ${CALEMAIL} < ${CALDIFF}
	   fi
       else
	   rm ${CALDIFF}
	   echo "No changes to "${CALFILE}
       fi
   fi
#
#	REMOVE CALENDAR.LOCK FILE
#
   echo "Removing calendar.lock file..."
   rm calendar.lock

