#!/bin/bash
#
# Author: Roger Leszczynski - rogerles@gmail.com
#
# 11/07/2006 Fix stupid asterisk bug that doesnt delete final msg0000.txt
# when all messages actual voice .wav | .gsm  files are deleted.
#
# This program is free software, distributed under the terms of
# the GNU General Public License Version 2.


Context=`cat /etc/asterisk/voicemail.conf | grep "^\[" | grep -v "general" | sed -e "s/\[//g" | sed -e "s/\]//g"`

for CurrDir in $Context
do

        VoiceDir="/var/spool/asterisk/voicemail/$CurrDir"

        Files=`ls "$VoiceDir"`

        for CurrBox in $Files
        do

                txt=(`ls "$VoiceDir/$CurrBox/INBOX/msg0000.txt"`)
                wav=(`ls "$VoiceDir/$CurrBox/INBOX/msg0000.wav"`)
                gsm=(`ls "$VoiceDir/$CurrBox/INBOX/msg0000.gsm"`)

                if [ "$txt" = $VoiceDir/$CurrBox/INBOX/msg0000.txt ] && [ [ "$wav" != $VoiceDir/$CurrBox/INBOX/msg0000.wav ] || [ "$gsm" != $VoiceDir/$CurrBox/INBOX/msg0000.gsm ] ]
                then
                        #safely move for now
                        mv $VoiceDir/$CurrBox/INBOX/msg0000.txt $VoiceDir/$Currbox
                fi
        done
done
