#!/usr/bin/perl

use strict;

my $url = "http://www.dhs.gov/dhspublic/getAdvisoryCondition";

my $verbose = 0;
my $i;
my $color = 0;

# parse command line
my $arg;
foreach $arg (@ARGV)
{
   if (substr($arg,0,1) eq "-")
   {
      for ($i=1;$i<length($arg);$i++)
      {
         my $letter = substr($arg,$i,1);
         if ($letter eq "v")
         {
            $verbose = 1;
         }
         elsif ($letter eq "V")
         {
            $verbose = 2;
         }
         elsif ($letter eq "c")
         {
            $color = 1;
         }
         else
         {
            print "usage: hlxml.pl [-[v|V][c]] [url]\n";
            print "	-v	verbose\n";
            print "	-V	even more verbose\n";
            print "	-c	use color instead of word for threat level\n";
         }
      }
   }
   else
   {
      $url = $arg;
   }
}

my $xml = `wget $url -qO -`;

my @tags = split(/</,$xml);

my $tag;
TAG: foreach $tag (@tags)
{
   my @props = split(/ /,$tag);
   my @words = split(/=/,$props[1]);

   if (substr($tag,0,1) eq "?" || $tag eq "" || !$#props || $words[1] eq "")
   {
      next TAG;
   }
 
   if ($verbose == 2)
   {
      print "The United States Department of Homeland Security has declared ";
   }

   if ($verbose >= 1)
   {
      $props[0] =~ s/_/ /;
      print $props[0]." ".$words[0];
      if ($verbose > 1)
      {
         print " to be ";
      }
      else
      {
         print " is ";
      }
   }
   $words[1] =~ s/"//;
   $words[1] =~ s/".*//;
   $words[1] =~ s/\n//;
   if ($color)
   {
      if ($words[1] eq "LOW")
      {
         $words[1] = "GREEN";
      }
      elsif ($words[1] eq "GUARDED")
      {
         $words[1] = "BLUE";
      }
      elsif ($words[1] eq "ELEVATED")
      {
         $words[1] = "YELLOW";
      }
      elsif ($words[1] eq "HIGH")
      {
         $words[1] = "ORANGE";
      }
      elsif ($words[1] eq "SEVERE")
      {
         $words[1] = "RED";
      }
   }
   print $words[1];
   if ($verbose >= 1)
   {
      print ".";
   }
   print "\n";
}

