#!/bin/bash
#########################################################
## Basic settings, not spaces alowed in dir/file names
##
##
## MYSQL query to detect cdrs you want to download audio from
query=`mysql voipmonitor -e "select id from cdr where calldate > '2018-12-16' and calldate < '2018-12-24' and caller_domain = '192.168.88.232' and connect_duration>0 order by id\G"|grep id|cut -d ':' -f 2`
##
## Maximum count of simultaneous cdr api calls
simmax=4
##
## Where the script will do the audio
workdir=/root/vm-scripts/manual-download
##
## Where is voipmonitor GUI located
guidir=/var/www/voipmonitor
##
## GUI user with audio download privilege
user=admin
password=adminecek
##
## End common settings
##########################################################
#audio files will be in:
audiodir=$workdir/audio
#log
day=`date "+%m-%d-%Y"`
logfile=$workdir/log/${day}.txt
#where to exec calls to voipmonitor
tmprunfile=$workdir/run/tmp.sh
#DEBUG - Test run? do (nothing) just logs =1 and dirs
dryrun=0
#make dirs
mkdir -p $workdir
mkdir -p $audiodir
mkdir -p `dirname $logfile`
mkdir -p `dirname $tmprunfile`
atonce=1
getcmd=""
tmpgetcmd=""
for id in ${query[*]}
do
getcmd="$tmpgetcmd echo '{\"task\": \"getVoiceRecording\", \"user\": \"$user\", \"password\": \"$password\", \"params\": {\"cdrId\": \"$id\", \"ogg\":\"true\"}}' | php api.php > $audiodir/$id.ogg"
if [ "1$atonce" = "1$simmax" ]; then
atonce=1
echo "`date "+%H:%M:%S"`" >> $logfile
echo $getcmd >> $logfile
#run command
if [ "1$dryrun" == '11' ]; then
echo $getcmd
else
echo "#/bin/bash" > $tmprunfile
echo "cd $guidir/php" >> $tmprunfile
echo "$getcmd" >> $tmprunfile
chmod +x $tmprunfile
$tmprunfile
fi
echo "`date "+%H:%M:%S"`" >> $logfile
echo >> $logfile
getcmd=""
else
((atonce+=1))
tmpgetcmd="$getcmd&"
fi
echo >> $logfile
done
if [ "11" != "1$atonce" ]; then
echo "`date "+%H:%M:%S"`" >> $logfile
echo $getcmd >> $logfile
if [ "1$dryrun" == '11' ]; then
echo $getcmd
else
echo "#/bin/bash" > $tmprunfile
echo "cd $guidir/php" >> $tmprunfile
echo "$getcmd" >> $tmprunfile
chmod +x $tmprunfile
$tmprunfile
fi
echo "`date "+%H:%M:%S"`" >> $logfile
echo >> $logfile
fi
echo >> $logfile