#!/usr/bin/perl -w
#
#    Copyright 2007, Rusetsky Dmitry (dimania@mail.ru) All rights reserved.
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of either:
#
#    a) the GNU General Public License as published by the Free Software
#    Foundation; either version 1, or (at your option) any later
#       version, or
#
#    b) the "Artistic License" which comes with Perl.
#
#    On Debian GNU/Linux systems, the complete text of the GNU General
#    Public License can be found in `/usr/share/common-licenses/GPL' and
#    the Artistic Licence in `/usr/share/common-licenses/Artistic'.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
#
#########################################################################
#
# Programm load firmware to Host based Printers over printserver Dlink DP-301U
#
# Usage: fwload4dlink [user[:password]@]host <firmware>
# 
# Prereq: Net::FTP, LWP::UserAgent
# Version: 0.01
# 
#########################################################################

use strict;
use warnings;

use Net::FTP;
use LWP::UserAgent;




our $VERSION;

$VERSION = '0.01';

#initial stuff
my ($username, $password, $host);

#get command line arguments
my $pr_status = 0;
my @opts = grep /^-/, @ARGV;
my @other = grep /^[^-]/, @ARGV;
my $server = shift @other;
my $firmware = shift @other;

my @arg_opts = map { split( /=/, $_ ) } grep( /=/, @opts);
my %arg_opts = (
		"--timeout" => "300",
		"--port" => "21",
		@arg_opts,
    );
map { $arg_opts{$_}++ } grep /^[^=]+$/, @opts;

if ($arg_opts{"-h"} || $arg_opts{"--help"}) {
    print join "", <DATA>;
    exit(0);
}

unless (defined $firmware && defined $server) {
    print("$0 [user[:password]@]host <firmwire file>\n");
    exit 1;
}

print "fwload4dlink $VERSION - 2007 (c) by Dimania <dimania\@mail.ru>\n";

my $timeout = $arg_opts{"--timeout"};
my $port = $arg_opts{"--port"};

#split them

my (@parts) = split /@/, $server;
my ($left, $right);
if (@parts <= 2) {
    ($left, $right) = @parts;
} else {
    $right = pop @parts;
    $left = join '@', @parts;
}

unless (defined $right) {
    $right = $left;
    undef $left;
}

($username, $password) = split /:/, $left if defined $left;
$host = $right if defined $right;

unless ( defined $username ) { $username = ""; }
unless ( defined $password ) { $password = ""; }

# Get status printer

  my $ua = LWP::UserAgent->new;
  $ua->agent("Mozilla/5.0");

  # Create a request
  #print "http://$username:$password\@$host";
  my $req = HTTP::Request->new(GET => "http://$username:$password\@$host");

  # 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 $res->content;
      my @string;
      @string = split /\r\n/, $res->content;
      my $i=0;

    while( defined( $string[$i] ) )
     {
      if ( $string[$i] =~ /Printer Status.*:.*On.*line.*/ ) 
       {     
        print "Printer On line\n"; $pr_status=1; last; 
       }
      if ( $string[$i] =~ /Printer Status.*:.*Off.*line.*/ ) 
       { 
         print "\nYour printer is Off line! Please Turn ON printer and try again.\n";
         #print "Printer Off line\n"; 
         $pr_status=0; 
         exit;
       }     
        $i++;
     }
  }
  else {
      print $res->status_line, "\n";
      exit;
  }

#make the connection by ftp

my $ftp;
$ftp = new Net::FTP($host, Debug => 0, Passive => 0, Port => $port);
$ftp or die "Couldn't make FTP connection to $host on port $port: $@!\n";

$ftp->login($username, $password) or
    die "Login failure!\n";

$ftp->binary;

print "Successfully logged into $host\n"; 

my @files = $ftp->dir();
#map { $_ =~ s|^/|| } @files;
print "\nFiles: @files\n";


# Load firmware

my $status = $ftp->put($firmware,"USB1");

unless (defined  $status)
   {
    print "\nError load firmware to printer on port @files\n";
   }
  else
   {
    print "\nLoad firmware to printer on port @files sucessful!\n";
   }


my @status = $ftp->quit();



__DATA__

This programm load firmware to Host based Printers over printserver Dlink DP-301U

Usage: fwload4dlink [user[:password]@]host <firmware>


Examples:
 
   fwload4dlink printserver1.org lj1010.dl
   
   
