#!/bin/sh
#ident "@(#) Calendar, Rev 1.4, 97/03/18"
#
#      Copyright (c) 1995-1997, Sun Microsystems, Inc.
#      All rights reserved.

##BEGIN TEMPLATE WRAPPER SHELL SCRIPT

# A variable to hold arguments after we strip OpenStep specific information
args=

# If you are debugging this script you might uncomment the following
# two lines.
#set -x
#echo input=$*

# Loop until argv ($#) is zero, each iteration will decrement (shift)
# argv/argc.
while [ $# != 0 ] ; do
  case $1 in
    # -NSOpen is an OpenStep standard command line flag which
    # lets the NSApplication know what file to open.
    -NSOpen)
        args="$args $2"
        shift             
        ;;
    # -NSOpenTemp is an OpenStep standard command line flag which
    # lets the NSApplication know what file to open and that it is
	# a temporary file and should be resaved somewhere else if edited.
    -NSOpenTemp)
        args="$args $2"
        shift
        ;;
    # -_NSMachLaunch is an undocumented additional flag to NSApplication.
    # We just ignore it here.
    -_NSMachLaunch)
        shift
        ;;
    *)
        args="$args $1"
        ;;
  esac
  shift
done

##END OF TEMPLATE WRAPPER SHELL SCRIPT

# If cm isn't running, start it.
# if there is an arg, it must be the name of a file containing
# cm appointments.  Add them to cm.
# /usr/openwin/bin/cm is used unless dtlogin is currently executing
# in which case /usr/dt/bin/dtcm is used.
# You can override this default behaviour by setting the
# environment variable CALENDAR_APP_CMD to the pathname of the
# desired command.
#    Note that if /usr/dt/bin/rpc.cmsd is used,
#    then dtcm should be used because cm might not
#    be compatible.
OWHOME=${OPENWINHOME:-/usr/openwin}
DTHOME=${CDEHOME:-/usr/dt}

# Use dtcm if dtlogin is running, else use cm

if [ -z "$CALENDAR_APP_CMD" ] ; then
  running=`/usr/bin/ps -e | /usr/bin/fgrep dtlogin`
  if [ -z "$running" ] ; then
    # use cm instead of dtcm
    CALENDAR_APP_CMD=$OWHOME/bin/cm
  else
    CALENDAR_APP_CMD=$DTHOME/bin/dtcm
  fi
fi

lookFor=`/usr/bin/basename $CALENDAR_APP_CMD`\$
running=`/usr/bin/ps -e | grep "$lookFor" | awk '{print $1}'`

if [ -z "$running" ] ; then
  $CALENDAR_APP_CMD &
  /usr/bin/sleep 2
fi

if [ -z "$args" ] ; then
   exit
fi

if [ -x $DTHOME/bin/dtcm_insert ] ; then
  # Do it the easy way
  for ii in $args ; do
    $DTHOME/bin/dtcm_insert -a $ii > /dev/null
  done
  exit 0
fi

# Else, do it the hard way.  Parse the
# input file using awk to get the 
# params that have to be passed to cm_insert.
# Note that in this mode, the repeat info
# is lost because cm_insert doesn't accept it.

tmpfile=/tmp/calendar.$$
#set -x
cat >$tmpfile <<EOF

function mymatch(arg1, arg2 ) {
  match( arg1, arg2)
  if ( RLENGTH <= 0)
    return 0
  return 1
}

function getWhat( ) {
  # $0 contains a What: line
  sub( "[ 	]*What:[ 	]*", "")
  what = \$0
  # Add following lines to the 'what' param
  # with \n dividers

  while( getline) {
    # break on an empty line, or any other keyword
    if ( length( \$0) == 0)       break
    if ( mymatch( \$0, datePat))  break
    if ( mymatch( \$0, startPat)) break
    if ( mymatch( \$0, endPat))   break
    if ( mymatch( \$0, apptPat))  break
      
    gsub( "^[ 	]*", "")
    what = what "\\\" "\\\" "n" \$0
  }
  gsub( ":[ 	]*", ": ", what)
  return what
}


function getAppt( ) {
  # $0 contains 1st non blank line of a
  # calendar appt.  Gather the What: Date: Start:
  # and End: params into a cmd line string and return
  # it.
  args = ""
  do {
    if ( length( \$0) == 0)     break
    if (mymatch( \$0, apptPat))  break

    if ( mymatch( \$0, whatPat)) {
      what = getWhat( )
      # $0 now contains the next line
      if ( length( what) > 0)
        args = args " -w " "\"" what "\""
      continue
    }

    if ( mymatch( \$0, datePat)) {
      sub("Date:", "")
      # Put the date into a form liked by the cm_insert cmd
      # nuke Monday, Tuesday, ...
      sub( "[MTWTFS].*day ", "")

      sub( "January",   "1/")
      sub( "February",  "2/")
      sub( "March",     "3/")
      sub( "April",     "4/")
      sub( "May",       "5/")
      sub( "June",      "6/")
      sub( "July",      "7/")
      sub( "August",    "8/")
      sub( "September", "9/")
      sub( "October",   "10/")
      sub( "November",  "11/")
      sub( "December",  "12/")
      sub( ", *19", "/")
      sub( ", *20", "/")

      args = args " -d " \$0
    }
    else if ( mymatch( \$0, startPat)) {
      if ( length( \$2 \$3) != 0) {
        args = args " -s " \$2 \$3
      }
    }
    else if ( mymatch( \$0, endPat)) {
      if ( length( \$2 \$3) != 0) {
        args = args " -e " \$2 \$3
      }
    }
    else {
      # Some unknown line.  Skip it.
    }
    if ( getline <= 0)
      break
  } while( 1)
  return args
}
  
BEGIN {
  apptPat =   "[ 	]*\**[ 	]Calendar Appointment"
  startPat = "^[ 	]*Start:"
  endPat =   "^[ 	]*End:"
  datePat =  "^[ 	]*Date:"
  whatPat =  "^[ 	]*What:"
  getline
  do {
    if( mymatch( \$0,  apptPat)) {
      # skip blank lines
      while( getline) {
        if ( length( \$0) > 0)
          break
      }
      cmd = getAppt( )
      # $0 contains the next line
      if ( length( cmd) > 0) {
        system( "$OWHOME/bin/cm_insert" cmd)
      }
    }
    else
      if ( getline <= 0)
        break
  } while(1)
}
EOF

for ii in $args ; do
  nawk -f $tmpfile $ii > /dev/null
done
rm -f $tmpfile
