#!/bin/sh

# Name: shx_checkrdv
# Type: Notification rendez-vous SherX
# Version: 1.0, Oct 2000 (Xavier Roche)

if ! test -d "/var/www/SherX"; then
	exit 0
fi
if ! test -f "/var/www/SherX/config/database/common.config.php3"; then
	exit 0
fi

DBDB=`grep DBName /var/www/SherX/config/database/common.config.php3 | sed -e 's/.*\"\(.*\)\".*/\1/'`
DBNAME=`grep Username /var/www/SherX/config/database/common.config.php3 | sed -e 's/.*\"\(.*\)\".*/\1/'`
DBPASS=`grep Password /var/www/SherX/config/database/common.config.php3 | sed -e 's/.*\"\(.*\)\".*/\1/'`
MAXPASS=7

if ! test -n "$DBDB"; then
	exit 1
elif ! test -n "$DBNAME"; then
	exit 1
fi

LISTID=`echo -e "select idapp from shx_usr_apps as us,shx_appointments as ap where us.sms_alert=1 and us.attendto=1 and us.via_grp=0 and us.idapp=ap.id and ap.app_start > now() and DATE_SUB(ap.app_start,INTERVAL us.rappel  SECOND) < now();" | mysql -B --user=$DBNAME --password=$DBPASS $DBDB | tail +2`
##LISTID=`echo -e "select idapp from shx_usr_apps as us,shx_appointments as ap where us.sms_alert=1 and us.owner=1 and us.attendto=1 and us.via_grp=0 and us.idapp=ap.id and ap.app_start > now() and DATE_SUB(ap.app_start,INTERVAL us.rappel  SECOND) < now();" | mysql -B --user=$DBNAME --password=$DBPASS $DBDB | tail +2`
##LISTID=`echo -e "select idapp from shx_usr_apps as us,shx_appointments as ap where us.sms_alert=1 and us.owner=1 and us.attendto=1 and us.via_grp=0 and us.idapp=ap.id and ap.app_start between now() and now()+INTERVAL us.rappel SECOND;" | mysql -B --user=$DBNAME --password=$DBPASS $DBDB | tail +2 | tr '#' ' ' | tr '\t' '#'`

if test -n "$LISTID"; then
	for ID in $LISTID; do
		PARAMS=-
		COUNT=0
		while test -n "$PARAMS"; do
			PARAMS=`echo -e "select idapp,u.username,u.firstname,u.lastname,email,app_start,app_stop,rappel,app_subject,description from shx_usr_apps as us,shx_appointments as ap, shx_users as u where us.sms_alert=1 and us.idapp=ap.id and us.idapp=$ID and u.username=us.username;" | mysql -B --user=$DBNAME --password=$DBPASS $DBDB | tail +2 | head -1 | tr '#' ' ' | tr '\t' '#'`
			if test "$COUNT" -gt $MAXPASS; then
				PARAMS=
			fi
			if test -n "$PARAMS"; then
				USER=`echo $PARAMS|cut -f2 -d'#'`
				FIRSTNAME=`echo $PARAMS|cut -f3 -d'#'`
				LASTNAME=`echo $PARAMS|cut -f4 -d'#'`
				MAIL=`echo $PARAMS|cut -f5 -d'#'`
				TSTART=`echo $PARAMS|cut -f6 -d'#'`
				TSTOP=`echo $PARAMS|cut -f7 -d'#'`
				TRAP=`echo $PARAMS|cut -f8 -d'#'`
				TITLE=`echo $PARAMS|cut -f9 -d'#'`
				DESC=`echo $PARAMS|cut -f10 -d'#'`
				DESC=`echo -e "$DESC"` 
				if ! test -n "$MAIL"; then
					MAIL="$USER"
				fi
				FMAIL="\"$FIRSTNAME $LASTNAME\" <$MAIL>"
				FNAME="$FIRSTNAME $LASTNAME"
				NDATE="le `echo $TSTART|cut -c5-6`/`echo $TSTART|cut -c7-8`  `echo $TSTART|cut -c9-10`:`echo $TSTART|cut -c11-12`"

				if test -n "$USER"; then
					echo "notifying $USER for $TSTART"
					cat << EOF | mail "$FMAIL" -s "Rappel de RDV: $TITLE"
Le rendez-vous '$TITLE' dbute $NDATE pour $FNAME

Description du rendez-vous:
$DESC

EOF
					echo -e "update shx_usr_apps set sms_alert=0 where idapp=$ID and username='$USER';" | mysql -B --user=$DBNAME --password=$DBPASS $DBDB
					echo -e "update shx_usr_apps set sms_alert=0 where idapp=$ID and username='$USER';"
				fi
			fi
			COUNT=$[$COUNT+1]
		done
	done
fi
