#!/bin/bash
# ;Version("2.2.1.0")
# ;InformationalVersion("2.2.1.0")

MOHCONFIG=/etc/asterisk/musiconhold_nixxis.conf
LOCAL_ROOT=/home/soundsv2
MOH_FOLDER=Moh

echo -e "; Do NOT edit this file by hand, it is re-generated automatically\r" > $MOHCONFIG
echo -e "; Last generation at: `date '+%b %d %H:%M:%S'` \r" >> $MOHCONFIG

for f in `nice -n 19 find $LOCAL_ROOT/*/$MOH_FOLDER/* -type d`
do
        # Extract the name of the subfolder from the full path
        CLASSGUID=`echo $f|awk -F'/' '{ print $NF}'`

        # Output class to moh config file
        echo -e "\r" >> $MOHCONFIG
        echo -e "[$CLASSGUID]\r" >> $MOHCONFIG
        echo -e "mode = files\r" >> $MOHCONFIG
        echo -e "directory = $f\r" >> $MOHCONFIG
        echo -e "\r" >> $MOHCONFIG
done


# sudo can be needed 
# sudo /usr/sbin/asterisk -rx "moh reload"

/usr/sbin/asterisk -rx "moh reload"

echo "Reload completed"

