#!/usr/bin/perl

use strict;
use LWP::Simple;

my $title;
my $lyrics;

if ($ARGV[0] eq "") {
  print "Need search parameters e.g. lyrics.pl aerosmith crazy\n";
  exit;
}

my $sSearch = join("+",@ARGV);
my $sURL = "http://www.lyricsdir.com/search/?q=$sSearch&b=Search+Lyrics";
my $sContent = get($sURL);

if ($sContent =~ /<li><a href=.(http:\/\/www.lyricsdir.com\/[a-z-]+.html)/) {
  $sURL = $1;
}

$sContent = get($sURL);

if ($sContent =~ /<h1>(.+)<\/h1>.+<div id="lyrics.*">(.+)<\/div>.+<div id="header">/s) {
  $title = $1;
  $lyrics = $2;
}

$lyrics =~ s/<br \/>//g;

open (WFILE1, ">/tmp/temp.lyrics");
print WFILE1 "$title\n$lyrics";
close(WFILE1);
