#!/usr/bin/perl
#
# saytime.pl  - D. Crompton, WA3DSP 11/30/2013
#
# Perl program to emulate and replace the app_rpt time of day 
# function call. This allows for easy changes and also volume and 
# tempo modifications.
#
# Call this program from a cron job and/or rpt.conf when you want to 
# hear the time on your local node
#
# Example Cron job to say the time on the hour every hour:
#   Change directory and times to your liking
#
# 00 0-23 * * * cd /etc/asterisk/wa3dsp; perl saytime.pl > /dev/null
#
# Note in this program all sound files must be .gsm
# All combined soundfiile formats need to be the same. 
# This could be changed if necessary. To use this with the
# stock Acid release you will need to convert a couple
# of the ulaw files in the /sounds/rpt directory to .gsm
# using sox or another conversion program and place them
# in the sounds directory for use by this program.
# The good-xxx.gsm files and the-time-is.gsm were created
# from ulaw files in the /sounds/rpt directory.
# An example sox command to do this is -
#
#  sox -t ul -r 8000 /var/lib/asterisk/sounds/rpt/thetimeis.ulaw /var/lib/asterisk/sounds/the-time-is.gsm
#
select (STDOUT);
$| = 1;
select (STDERR);
$| = 1;
#
# Replace with your output directory
#$outdir = "/etc/asterisk/wa3dsp";
$outdir = "/tmp";
#
$ampm = "PM";
$base = "/var/lib/asterisk/sounds";
#
# Replace with your node - add more nodes if desired
$mynode = "40638";
#$mynode = "";
#$mynode2 = "";
#$mynode3 = "";
#
@list = ($sec,$min,$hour,$day,$mon,$year_1900,$wday,$yday,$isdst)=localtime;
#
#
if ($hour < 12) { 
  $greet = "Good Morning"; 
  $ampm = "AM";
  $FNAME = $base . "/good-morning.gsm ";
 }
elsif ($hour >= 12 && $hour < 18) { 
  $greet = "Good Afternoon";
  $FNAME = $base . "/good-afternoon.gsm ";
 }
else { 
  $greet = "Good Evening";
  $FNAME = $base . "/good-evening.gsm ";
}

#if ($hour > 12) { $hour = $hour-12 };
#if ($hour == 0) { $hour = 12 };
$FNAME = $FNAME . $base . "/the-time-is.gsm ";
$FNAME = $FNAME . $base . "/digits/" . $hour . ".gsm ";

if ($min == 0) { 
  $FNAME = $FNAME . $base . "/digits/hundred.gsm ";
} else
{
  if ($min < 10) {
    $FNAME = $FNAME . $base . "/digits/oh.gsm ";
    $FNAME = $FNAME . $base . "/digits/" . $min . ".gsm ";
  }
  elsif ($min < 20) {
    $FNAME = $FNAME . $base . "/digits/" . $min . ".gsm ";
  } else {
    $min10 = substr ($min,0,1) . "0";
    $FNAME = $FNAME . $base . "/digits/" . $min10 . ".gsm ";
    $min1 = substr ($min,1,1);
    if ($min1 > 0) {
      $FNAME = $FNAME . $base . "/digits/" . $min1 . ".gsm ";
    }
  }
} 
$FNAME = $FNAME . $base . "/hours.gsm";

#if ($ampm =~ "AM") {
#  $FNAME = $FNAME . $base . "/digits/a-m.gsm";
#} else {
#  $FNAME = $FNAME . $base . "/digits/p-m.gsm";
#}
#
# Following lines concatenate all of the files to one output file
#
@proglist = ("cat " . $FNAME . " > " . $outdir . "/temp.gsm");
system(@proglist);
#
# Following lines process the output file with sox to lower the volume
# negative numbers lower than -1 reduce the volume - see Sox man page
# Other processing could be done if necessary
#
@proglist = ("sox --temp /tmp " . $outdir . "/temp.gsm " . $outdir . "/current-time.gsm vol -0.35");
system(@proglist);
#
# Say the time on the local node
#
@proglist = ("/usr/sbin/asterisk -rx \"rpt localplay " . $mynode . " " . $outdir . "/current-time\"");
system(@proglist);

#@proglist = ("/usr/sbin/asterisk -rx \"rpt localplay " . $mynode2 . " " . $outdir . "/current-time\"");
#system(@proglist);

#@proglist = ("/usr/sbin/asterisk -rx \"rpt localplay " . $mynode3 . " " . $outdir . "/current-time\"");
#system(@proglist);

# end of saytime.pl

