#!/usr/local/bin/perl
#Code is used to allow user to type in cities and dates, and returns the cheapest ticket

system "clear";						#clear screen

########	Set-up for user interface		#############

print "\nUse the following codes when entering dates of travel \n";
print "\n", "*" x 60, "\n\n";

print"January=Jan\tFebruary=Feb\tMarch=Mar\tApril=Apr\n";
print"May=May\t\tJune=Jun\tJuly=Jul\tAugust=Aug\n";
print"September=Sep\tOctober=Oct\tNovember=Nov\tDecember=Dec\n\n";

print "*" x 60, "\n\n";


#####################################################################

######## 	Obtain Departure,Destination, and Dates	#############

print "\nWhat Cities are you looking at?","\n";			#Prompt user for cities
print "\n";

print "Departure Airport:";					#read in departure					
	my $dep=<STDIN>;
	chomp ($dep);

print "Destination Airport:";					#read in destination
	my $des=<STDIN>;
	chomp ($des);

print "\nWhat Dates are you looking at?\n";

print "\nDeparture Date:";					#read in departure date
	my $dep_date=<STDIN>;
	@dep_list=(split / /, $dep_date);
	my $dep_month=@dep_list[0];
	my $dep_dat=@dep_list[1];
	chomp ($dep_month);
	chomp ($dep_dat);

print "Return Date:";						#read in return date
	my $ret_date=<STDIN>;
	@ret_list=(split / /, $ret_date);
	my $ret_month=@ret_list[0];
	my $ret_dat=@ret_list[1];
	chomp ($ret_month);
	chomp ($ret_dat);

my $file="$dep\_$des\__$dep_month\_$dep_dat\_$ret_month\_$ret_dat";	#Create file with appropriate
open OUT, "> $file" or die "Can't open $file: $!\n";			#items
close (OUT);

print "\nPlease Wait A moment....\n\n";


for (;;){

open (LYNX, "lynx -source 'http://www.itn.net/cgi/air?stamp=NEWCOOKY*itn%2Ford%3DNEWREC,itn/air/united&airline=United&persons=1&air_avail=10&air_class=coach+%28lowest+avail.%29&depart=$dep&dest.0=$des&mon_abbr.0=$dep_month&date.0=$dep_dat&hour_ampm.0=5+am&mon_abbr.1=$ret_month&date.1=$ret_dat&hour_ampm.1=5+am&ecert_num=&rt_ow=Round+Trip&best_itins=2&return_to=best_itins
'
 |") or die "Can't open lynx: $!";				#URL contains 4 parameters

my @lines= <LYNX>;						#take in lines from source code as
my $total=0;							#an array. Set line counter to 0

foreach(@lines){						#USD flags available price
	if (/USD/){						#total will be number of flights
		$total++;					#available
	}
}
print "Here are your prices:\n";

my $search="@lines";						#search through source code, match
								#USD, and apply offset to find prices

	for ($i=0; $i < $total; $i++){
        $search =~ m/(USD)/g;
	my $pos1=pos($search);
	
	my $price=substr($search, ($pos1-14),6);

	$prices[$i]=$price;
	
	print "$price\n";

}

open OUT, ">> $file" or die "Can't open $file: $!\n";

print OUT $prices[0];	
	 
print "\n";
print "The total amount of available fares is: $total\n";
print "\n";

sleep 60;

}
; 
