#!/usr/bin/perl

use strict;
use LWP;

my $product = 'emailswitch';

my @warehouses = qw(-gb -jp -cn -us1 -us);
my $warehouse =  undef;
if ($ARGV[0]) {
	if(grep(/$ARGV[0]/i, @warehouses)) {
		($warehouse = $ARGV[0]) =~ s/-//;
		$warehouse = uc $warehouse;
	}
	else {
		print "$ARGV[0] is not a known warehouse option.  Known warehouses are @warehouses\n";
		exit(1);
	}
}
else {
	print "qapass now requires a warehouse option.  Known warehouses are @warehouses\n";
	exit (1);
}
if ($warehouse eq 'US') {
	$warehouse = 'US1';
}
open (MODELFILE, '</etc/barracuda/product');
while (<MODELFILE>) {
        $product = $_;
}
close MODELFILE;

chomp $product;

my $serial = 0;
open (SERIALFILE, '</etc/barracuda/serial');
while (<SERIALFILE>) {
	$serial = $_;
}
close SERIALFILE;
chomp $serial;

if ( -e "/tmp/burnin") {
	print ("Burnin was not completed successfully.  This unit should be marked as failed.  Exiting.\n");
	exit (1);
}

if ( -e "/tmp/qafailed") {
	print ("WARNING:  This unit was previously marked as having failed QA.  Changing to passed.  If this was a mistake, you must remove the serial number from the BOS system.  Tell Ken.\n");
	unlink("/tmp/qafailed");
}

# Register the serial number with the server
# Create a user agent object
my $ua = LWP::UserAgent->new;
$ua->agent("$0/0.1 " . $ua->agent);
my $url = undef;
if ($warehouse ne 'US') {
	$url = "https://manufacturing:N3rfH3rders\@ops.barracudanetworks.com:443/~order/prod_accept.cgi?serial=$serial&wh=$warehouse"
}
else {
	$url =  "https://manufacturing:N3rfH3rders\@ops.barracudanetworks.com:443/~order/prod_accept.cgi?serial=$serial";
}
my $req = HTTP::Request->new(GET => $url);
$req->header('Accept' => 'text/html');
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
# Check the outcome of the response
if (!$res->is_success) {
	print "\n\n\n\n\nSerial number registration failed!\nThis unit will be unable to ship until this is corrected.\n  Make sure the unit is connected to the network and try again.\n";
	exit (1);
}
$ua = LWP::UserAgent->new;
$ua->agent("$0/0.1 " . $ua->agent);
$url = "https://manufacturing:N3rfH3rders\@ops.barracudanetworks.com/cgi-bin/get_serial_status.cgi?serial=$serial";
$req = HTTP::Request->new(GET => $url);
$res = $ua->request($req);
if (!$res->content) {
	print "Unable to get FG status\n";
	exit (1);
}
my $fg_status = undef;
for my $line (split /^/, $res->content) {
	if ($line =~ /bc_prdb_status=(\d+)/) {
		$fg_status = $1;
	}
}
if ($fg_status != 1) {
	print "FG status -$fg_status.  Exiting.\n";
	exit(1);
}
my $loc_string = "&loc=$warehouse";

$url = "https://manufacturing:N3rfH3rders\@ops.barracudanetworks.com/cgi-bin/shipping.cgi?option=qadocs&xlist=1&box_label=1&printer_loc=manufacturing&submit=Print&serial_id=$serial&override=$serial$loc_string";
$ua = LWP::UserAgent->new;
$ua->agent("$0/0.1 " . $ua->agent);
$req = HTTP::Request->new(GET => $url);
$req->header('Accept' => 'text/html');
$res = $ua->request($req);
if (!$res->is_success) {
	print "Unable to print box label for serial $serial\n";
	exit (1);
}
$url = "https://manufacturing:N3rfH3rders\@ops.barracudanetworks.com/cgi-bin/shipping.cgi?option=qadocs&serial_label=1&printer_loc=manufacturing&submit=Print&serial_id=$serial&override=$serial$loc_string";
$ua = LWP::UserAgent->new;
$ua->agent("$0/0.1 " . $ua->agent);
$req = HTTP::Request->new(GET => $url);
$req->header('Accept' => 'text/html');
$res = $ua->request($req);
if (!$res->is_success) {
	print "Unable to print serial label for serial $serial\n";
	exit (1);
}
system("/bin/touch", "/tmp/qapassed");
if (my $pid = fork) {
	print ("Printing labels for serial $serial\n");
	exit(0);
}
elsif (defined ($pid)) {
	exec "/boot/os_tools/spin_lights.pl &>/dev/null";
}

