#!/usr/bin/perl
#
# dtmfhelper -- quick script to filter firefox requests through to dtmfdial
#
# 3/26/2006 - David Bushong <david@bushong.net>
#

use strict;

## Change this, probably
my $LOCAL_AREA_CODE = '510';

## Grab number from file passed by Firefox
my $file = $ARGV[0];
my $num = <>;
chomp $num;

## Get rid of the tmp file
unlink($file);

## Clear out non-digits
$num =~ s/\D//g;

## Cache the length
my $len = length($num);

## If has an area code
if ($len == 10) {
  ## if it's in our local area code
  if (substr($num, 0, 3) eq $LOCAL_AREA_CODE) {
    $num =~ s/^...//;
  }
  ## append 1- for long distance
  else {
    $num = "1$num";
  }
}
## if it's not a valid number
elsif ($len != 7) {
  die "$0: invalid number: $num\n";
}

## call dtmfdial
system('dtmfdial', $num);
