#!/usr/local/bin/perl -w

# getbash.pl
# gets a quote from bash.org
#
# Randi Harper <sektie@freebsdgirl.com>

use IO::File;
use LWP::Simple;
use Getopt::Std;

use strict;

# getBashURL
# creates a URL based on the given arguments.
# takes args:
#   type - [random || latest || n || save] 
#   args - (args to type, as needed for n.) (NULL is default)
# returns: URL to be queried, 0 on error.

sub getBashURL {
	my ($type, $args) = @_;
	my $bashurl = "http://bash.org/xml/?";

	if ($type eq "random") {
		$bashurl = $bashurl . "random";
	}

	elsif ($type eq "latest") {
		$bashurl = "$bashurl" . "latest";
	}

	elsif ($type eq "n") {
		if ($args ne 0) {
			$bashurl = $bashurl . $args;
		}
		else {
			return;
		}
	}

    elsif ($type eq "save") {
        open(FP, ">>bash.org.quotes.txt");
        open(SAVE, ".tmp");
        while (<SAVE>) {
            print FP $_;
        }
        print FP "#" x 10; print FP "\n";
        print "Last Quote Saved!\n";
        close(SAVE); close(FP);
        exit(); 
    }

	else {
		return;
	}

	$bashurl = $bashurl . "&num=1";

	return $bashurl;
}

# getBashFile
# Retrieves the XML file from the bash.org website.
# takes args:
#   type - [random || latest || n] 
#   args - (args to type, as needed for n.) (NULL is default)
#	limit - [1 - 5] (1 is default)
# returns: URL to be queried, 0 on error, 1 on bash.org being down.

sub getBashFile {
	my ($type, $args, $limit) = @_;
	my ($content, $bashurl);
	
	unless (defined ($bashurl = getBashURL($type, $args, $limit))) {
		return;
	}

	unless (defined ($content = get($bashurl))) {
		return;
	}

	return $content;
}

# parseBashFile
# Takes the content of the bash file and parses into a readable format.
# takes args:
#   type - [random || latest || n] 
#   args - (args to type, as needed for n.) (NULL is default)
#	limit - [1 - 5] (1 is default)
# returns: URL(s) and quote(s) 

sub parseBashFile {
	my ($type, $args, $limit) = @_;
	my ($url, @quotelines);

	my $content = getBashFile($type, $args, $limit);
	
	my @tmplines = split(/\n/, $content);

	foreach (@tmplines) {
		s/^\s+//;
		s/\s+$//;
		if ($_ =~ /\<item rdf\:about\=\"(.*)\"\>/) {
			$url = $1;
		}
		elsif ($_ =~ /^\<description\>\<\!\[CDATA\[(.*)\]\]\><\/description\>$/) {
			@quotelines = split(/\<br \/\>/, $1);
			foreach (@quotelines) {
				s/\&gt\;/\>/g;
				s/\&lt\;/\</g;
				s/\&quot\;/\"/g;
			}
		}
	}
	return ($url, @quotelines);
}

sub usage {
	print "parsebash.pl v. 1.0\n";
	print " -- Randi Harper <sektie\@freebsdgirl.com>\n";
    print " -- Ventz Petkov <ventz\@ccs.neu.edu> (implemented save)\n";
	print "Usage: parsebash.pl -t <type> -a [args]\n";
	print "\t<type> can be random, n, latest, or save.\n";
	print "\t[args] is used to request a specific number with n.\n\n";
	print "Examples: parsebash.pl -t random\n";
    print "          parsebash.pl -t save\n";
	print "          parsebash.pl -t n -a 42253\n";
}

our ($opt_t, $opt_a);

getopt("ta");

if (!(defined $opt_t)) {
	usage();
	exit;
}

if ($opt_t eq "n" && !(defined $opt_a)) {
	usage();
	exit;
}


my ($url, @quote) =  parseBashFile($opt_t, $opt_a);
open(FP, ">.tmp");
print "URL: $url\n\n"; print FP "URL: $url\n\n";
foreach (@quote) {
	print "$_\n"; print FP "$_\n";
}
close(FP);
print "\n";

