#!/bin/sh 
#$Id: cm-dispatch,v 1.1.1.1 1996/04/16 15:12:04 leon Exp $

#do this before anything else or die
TMP="/tmp/cm-dispatch$$"
cat >$TMP






# extract some usefull fields from the mail

APPLICATION=`cat $TMP | grep CmApplicationName | cut -d: -f2-`
GROUP=`cat $TMP | grep CmGroupName | cut -d: -f2-`
MESSAGE=`cat $TMP | grep CmMessage | cut -d' ' -f2-`

# discard these fucking trailing blanks 
APPLICATION=`echo $APPLICATION`
GROUP=`echo $GROUP`
MESSAGE=`echo $MESSAGE`




APPLICATIONDIR="$HOME/.$APPLICATION"
if [ ! -d $APPLICATIONDIR ]
then
	mkdir $APPLICATIONDIR
fi

CMDIR="$APPLICATIONDIR/.cmstore"
if [ ! -d $CMDIR ]
then
	mkdir $CMDIR
fi

GROUPDIR="$CMDIR/$GROUP"
if [ ! -d $GROUPDIR ]
then
	mkdir $GROUPDIR
fi



NUMBER="`tail -1 $GROUPDIR/last-arrived 2>/dev/null`"

if [ "$NUMBER" = "" ]
then
    NUMBER=0
fi

NEWFILE="`expr $NUMBER + 1`"
echo "$NEWFILE" >$GROUPDIR/last-arrived
mv $TMP $GROUPDIR/$NEWFILE


# notify arrival to the k-lendars process
KLENDARS="`cat $APPLICATIONDIR/.protocol-notify`"
if [ "$KLENDARS" != "" ]
then
	$KLENDARS
fi

