#!/bin/sh

#####################################################################
# CheckMailD-Event ($1=0): new mail                                 #
#####################################################################
if [ "$1" = "0" ]
then
    sleep 1
fi

#####################################################################
# CheckMailD-Event ($1=1): mail-status (every interval)             #
#####################################################################
if [ "$1" = "1" ]
then
    #################################################################################
    ###                                                                          ####
    ###   notifyhandy                                                            ####
    ###                                                                          ####
    ###   Läßt Telefon kurz klingeln. Es wird nc benötigt!                       ####
    ###                                                                          ####
    ###   (Mein Dank geht an meimi039)                                           ####
    ###                                                                          ####
    #################################################################################

    TEMPFILE=/var/tmp/atcommands
    TEMPFILE2=/var/tmp/lastmailcnt
    NUMBER=01601234567
    FRITZIP=127.0.0.1
    SIPENTRY=*122#
    FON=3
    
    # Mail-Count Datei öffnen
    touch $TEMPFILE2
    exec 3< $TEMPFILE2
    
    # Die letzte Anzahl der eMail(s) auslesen
    read <&3 lastmailcnt
    
    if [ "$lastmailcnt" = "" ]
    then
	$lastmailcnt = "0"
    fi

    # Prüfen, ob letzte eMail Anzahl 0 war    
    if [ "$lastmailcnt" = "0" ]
    then

      # Prüfen, ob aktuelle eMail Anzahl größer als 0 ist
      if [ "$2" -gt "$lastmailcnt" ]
      then
      	  echo ATP${FON}>$TEMPFILE
	  echo ATD${SIPENTRY}${NUMBER}>>$TEMPFILE
	  
	  cat $TEMPFILE | ./nc $FRITZIP 1011
	  
	  echo ath | ./nc $FRITZIP 1011
	  rm -f $TEMPFILE 
        fi
    fi

    # Aktuelle eMail Anzahl in Datei schreiben	 
    echo $2 > $TEMPFILE2
    sleep 1
fi
