carriba - 2017-03-28

The following script allows you to download and update the core and extra sound files form your existing Asterisk installation.

It did work fine for me during at least the last 9 months and tested it intensively on my Raspberry Pi, and thought to make this tool available to the community who like to support a multi-lingual Asterisk installation as I have in place today.

The script can only break if the source files are not available; see the "url_target" variable used in the script.

The script is silent (does not generate any text output), and can be setup in a crontab to run periodically (once a month):

#!/bin/bash
atest[0]='test' || { echo >&2 "Failure: arrays not supported in this version of bash."; exit 1; }
source_pattern=(en de es-ES fr-FR it)
target_pattern=(en_GB de_DE es_ES fr_FR it_IT)
target_dir=/var/lib/asterisk/sounds
if [ -x "$(command -v stat)" ]; then
  file_owner=`stat -c '%U' $target_dir`:`stat -c '%G' $target_dir`
else
  file_owner=`ls -ld $target_dir | awk '{print $3}'`:`ls -ld $target_dir | awk '{print $4}'`
fi
temp_file=/tmp/`basename $0 .sh`.tmp
for i in "${!target_pattern[@]}"; do
   target_subdir=$target_dir/${target_pattern[$i]}
   [ -d $target_subdir ] || { mkdir $target_subdir ; chown $file_owner $target_subdir 2> /dev/null ; }
   if [ -d $target_subdir ]; then
     for a in core extra ; do
        declare -l lower_case
        lower_case=${source_pattern[$i]}
        url_target=https://www.asterisksounds.org/$lower_case/download/asterisk-sounds-$a-${source_pattern[$i]}-sln16.zip
        local_file=sounds-$a-${target_pattern[$i]}-sln16.zip
        old_file="`basename $local_file .zip`.old"
        if [ -f $local_file ]; then
          wget -S --spider $url_target -o $temp_file
          if [ $? -gt 0 ]; then
            if [ $temp_file -a "`cat $temp_file | grep '500 Service unavailable'`" ]; then
              wget -S --spider $url_target -o $temp_file
            fi
          fi
          if [ $? -eq 0 -a -f $temp_file -a "`cat $temp_file | grep '200 OK'`" ]; then
            if [ "`cat $temp_file | grep '^Remote file exists.$'`" ]; then
              time_stamp_remote=`cat $temp_file | awk '/Last-Modified/{ date=""; for(x=2;x<=NF;++x) date=(date " " $x); print date;}' | xargs -I{} date -d {} +"%s"`
              [[ $time_stamp_remote =~ ^[0-9]+$ ]] || time_stamp_remote=0
            else
              time_stamp_remote=0
            fi
          else
            time_stamp_remote=0
          fi
          time_stamp_local=`date +"%s" -u -r $local_file`
          [[ $time_stamp_local =~ ^[0-9]+$ ]] || time_stamp_local=0
        else
          time_stamp_remote=1
          time_stamp_local=0
        fi
        if [ "$time_stamp_remote" -gt "$time_stamp_local" ]; then
          [ -f $local_file ] && cp -pn $local_file $old_file 2> /dev/null
          wget -qN $url_target -pO $local_file
          unzip -t $local_file &> $temp_file
          if [ $? -gt 0 -o "`cat $temp_file | grep 'End-of-central-directory signature not found.'`" ];  then
            rm -f $local_file 2> /dev/null
            mv -f $old_file $local_file 2> /dev/null
            unzip -t $local_file &> $temp_file
            [ $? -eq 0 -a "`cat $temp_file | grep '^No errors detected in compressed data'`" ] || rm -f $local_file 2> $temp_file
          fi
          [ -f $old_file ] && rm -f $old_file 2> /dev/null
        fi
        if [ -f $local_file ]; then
          if [ ! -f "$target_subdir/.version-$a" -o $local_file -nt $target_subdir/.version-$a ]; then
            unzip -t $local_file &> $temp_file
            [ $? -eq 0 -a "`cat $temp_file | grep '^No errors detected in compressed data'`" ] && unzip -uoq $local_file -x LICENSE.txt .language -d $target_subdir
            if [ -f "$target_subdir/.version-$a" ]; then
              chown $file_owner $target_subdir/.version-$a
              touch -r $local_file $target_subdir/.version-$a
            fi
            source_options="-t raw -e signed-integer -b 16 -c 1 -r 16k"
            for b in $(unzip -Z1 $local_file '*.sln16'); do
               source_file=$target_subdir/$b
               if [ -f $source_file ]; then
                 target_file=`echo $source_file | sed "s/.sln16/.gsm/"`
                 if [ ! -f $target_file -o $source_file -nt $target_file ]; then
                   sox $source_options $source_file -t gsm -r 8k $target_file
                   [ -f $target_file ] && { chown $file_owner $target_file ; touch -r $source_file $target_file ; }
                 fi
                 target_file=`echo $source_file | sed "s/.sln16/.alaw/"`
                 if [ ! -f $target_file -o $source_file -nt $target_file ]; then
                   sox $source_options $source_file -t raw -r 8k -e a-law $target_file
                   [ -f $target_file ] && { chown $file_owner $target_file ; touch -r $source_file $target_file ; }
                 fi
                 target_file=`echo $source_file | sed "s/.sln16/.ulaw/"`
                 if [ ! -f $target_file -o $source_file -nt $target_file ]; then
                   sox $source_options $source_file -t raw -r 8k -e mu-law $target_file
                   [ -f $target_file ] && { chown $file_owner $target_file ; touch -r $source_file $target_file ; }
                 fi
               fi
            done
          fi
        fi
     done
   fi
   if [ -d $target_subdir ]; then
     if [ "`find $target_subdir -maxdepth 1 -name '.version-*' -print -quit`" ]; then
       find $target_subdir -name '*.sln16' -exec chown $file_owner {} \;
       [ "`find $target_subdir -mindepth 1 -type d -print -quit`" ] && find $target_subdir -mindepth 1 -type d -exec chown $file_owner {} \;
     else
       rm -f $target_subdir 2> /dev/null
     fi
   fi
done
rm -f $temp_file 2> /dev/null
exit 0

Hope you find this little tool useful ;-)

 

Last edit: carriba 2017-03-28