#!/bin/bash

mkdir weatherzip
cd weatherzip
wget http://nerdvittles.com/trixbox123/zipcodes.zip
unzip zipcodes.zip
rm -f zipcodes.zip
mysql -uroot -ppassw0rd < zipcodes1.sql
mysql -uroot -ppassw0rd < zipcodes2.sql

#cd /var/lib/asterisk/agi-bin
wget http://nerdvittles.com/trixbox123/nv-weather-zip.zip
unzip nv-weather-zip.zip
rm -f nv-weather-zip.zip
cp nv-weather-zip.php /var/lib/asterisk/agi-bin/nv-weather-zip.php
chmod 775 /var/lib/asterisk/agi-bin/nv-weather-zip.php
chown asterisk:asterisk /var/lib/asterisk/agi-bin/nv-weather-zip.php

mkdir /var/lib/asterisk/sounds/tts
chown asterisk:asterisk /var/lib/asterisk/sounds/tts
chmod 777 /var/lib/asterisk/sounds/tts

sed -i '
/cron.monthly/ {
a\
\
02 0 * * * root rm -f /var/lib/asterisk/sounds/tts/tts*
}' /etc/crontab

sed -i '
/\[from-internal-custom\]/ {
a\
exten => 947,1,Answer\
exten => 947,2,Wait(1)\
exten => 947,3,Set(TIMEOUT(digit)=7)\
exten => 947,4,Set(TIMEOUT(response)=10)\
exten => 947,5,Flite("At the beep enter the five digit code for the weather report you wish to retrieve.")\
;exten => 947,5,Swift("At the beep enter the five digit code for the weather report you wish to retrieve.")\
exten => 947,6,Read(ZIPCODE,beep,5)\
exten => 947,7,Flite("Please hold a moment while we contact the National Weather Service for your report.")\
;exten => 947,7,Swift("Please hold a moment while we contact the National Weather Service for your report.")\
exten => 947,8,AGI(nv-weather-zip.php|${ZIPCODE})\
exten => 947,9,NoOp(Wave file: ${TMPWAVE})\
exten => 947,10,Playback(${TMPWAVE})\
exten => 947,11,Hangup\

}' /etc/asterisk/extensions_custom.conf

echo Weather by Zip Code installation completed. To use, dial Z-I-P or 947. Before first use, execute this command: amportal restart

