#!/usr/bin/perl

#APOD.pl. Grabs the latest Astro Pic O' the Day, resizes it to a
#200-pixel wide JPG, and FTP's it to our NT box.

# It can be run locally on NT if you compile the Image::Grab module and
# the LWP bundle.

# see perldoc Image::Grab for full details
use Image::Grab;
use Net::FTP;
my $pic=new Image::Grab;

# Grab the image with the following regex
$pic->url('.*\.(jpg|jpeg|gif)');
$pic->refer("http://antwrp.gsfc.nasa.gov/apod/");
$pic->grab;

# MIME type should be either "image/jpeg" or "image/gif"
my $ext = $pic->type;

# Quit if we didn't get it
die "Could not find an image" unless($ext=~/image/);

# Use the MIME type to pick our filename extension
$ext =~ s/image\///;
my $file = "original.".$ext;

# Write it out
open(OUT, ">$file") or die "Could not write todays file.";
print OUT $pic->image;
close OUT;

# Run montage
my $cmd="/opt/local/bin/montage +frame +shadow +label  -comment \"\" ";
   $cmd.="-quality 72 -geometry 200 $file today.jpg";
system $cmd;

my $ftp=Net::FTP->new("ntbox.domain.ex");
$ftp->login("anonymous","anon@server.us");
$ftp->cwd("/web/pub/main");
my $status=$ftp->put("today.jpg");
$ftp->quit;

# Check to see if it made it
die "File transfer failed" if($status ne "today.jpg");

# end