#!/usr/bin/perl
#
# tellmeafortune.pl
#
# This Perl / AppleScript script will tell you a fortune using 
# Mac OS X's speech synthesizer
#
# Pierre Bauduin <pierre@baudu.in>
# Version 0.0.3 (May 4th 2007)
#
# Note: you must have the fink package "fortune" installed
# If you don't have it (yet);
# 1) Download fink from http://www.finkproject.org/
# 2) Install fink
# 3) Fire up Terminal and type: sudo apt-get install fortune-mod
#
# 
use strict;
use MacPerl 'DoAppleScript';

# Reality check ! Is this machine running Mac OS X (Darwin) ?
my $uname = `uname`;
chomp $uname;
unless ($uname =~ /Darwin/)
	{
	die ("This script will only run on Mac OS X, and you seem to be running $uname.\n");
	}

if ( -f "/sw/bin/fortune" )
{
my $line = `/sw/bin/fortune -s computers literature people wisdom`;
# Replacing charachters
# carriage return
$line =~ s/\n/ /g;
# tab
$line =~ s/\t/ /g;
# Punctuation
$line =~ s/\,/ /g;
$line =~ s/\:/ /g;
$line =~ s/\./ /g;
$line =~ s/\;/ /g;
# Multiple spaces
$line =~ s/\ \ */ /g;
# What is behind -- is the name of the author
# I don't want to say the name of the author
# Let's split
my @data = split(/--/, $line);
# What I need is in $data[0] 
my $sentence_to_say = $data[0];
printf "I am going to say: $sentence_to_say\n";
MacPerl::DoAppleScript (<<END_SCRIPT);
	tell application "Finder"
        	say "$sentence_to_say" 
	end tell
END_SCRIPT
}
else
{
printf "Oops ! I could not find /sw/bin/fortune !\n";
printf "Here is how to install it:\n";
printf "   1) Download fink from http://www.finkproject.org/\n";
printf "   2) Install fink\n";
printf "   3) Fire up Terminal and type: sudo apt-get install fortune-mod\n";
die "ERROR: fortune-mod is not installed.\n";
}

