#!perl
# this file is excecutable from the command line and compiled with perl2exe
# it shows a dialog box that allows the user to stop the download , or executes a setup file
# TO DO:
# learn perl TK and use a progress script that outputs the size of the file
# or simply checks the connection periodically to make sure the download is not interuppted
# and if it is restarts the download
# maybe use Net::FTP to restart the download
# put the downloads into a user ftp location on the site beadboard.info
use strict;
$0 = $^X unless ( $^X =~ m%(^|[/\\])(perl)|(perl.exe)$%i );
my ($program_dir) = $0 =~ m%^(.*)[/\\]%;
$program_dir ||= ".";
# for use while testing
$program_dir = "C:/Program Files/Apache Group/Apache2/htdocs/composer/addons";
chdir($program_dir);
#necessary for perl2exe
#perl2exe_include File::Path
#perl2exe_include LWP::Simple
#perl2exe_include File::Copy
#perl2exe_info CompanyName=Microwebber.com
#perl2exe_info FileDescription=LWP update BeadBoard
#perl2exe_info FileVersion='end'.0.0
#perl2exe_info InternalName=LWP updateApplication.exe
#perl2exe_info LegalCopyright=Microwebber.com 2006
#perl2exe_info OriginalFilename=updateApplication.exe
#perl2exe_info ProductName=LWP updateApplication
#perl2exe_info ProductVersion='end'.0.0
my $base = $program_dir;
$base =~ s/(.*)addons.*/$1/;
unless ( -e "$base/addons" ) {
print STDOUT qq~file location is incorrect $base/addons~;
}
# for debugging purposes
#my@dirs = split(m~/~,$base) ;
#my$dir = '';
#for(my$i=0;$i<@dirs;$i++){
#$dir .= $dirs[$i]."/";
#if(-e $dir){
#print STDOUT "exists $dir\n";
#} else{
#exit(0);
#}
#}
#exit(0);
#}
use LWP::Simple;
use File::Path;
use File::Copy;
use Tk;
#use Tk::ProgressBar;
my $mw = new MainWindow;
my$percent_done;
my$progress = $mw->ProgressBar(
-width => 200,
-length => 20,
-anchor => 's',
-from => 0,
-to => 100,
-blocks => 10,
-colors => [0, 'green', 50, 'yellow' , 80, 'red'],
-variable => \$percent_done
);
my$position = 1;
$progress->value($position);
my $frm_name = $mw->Frame();
my $install_button = $mw->Button( -text => "Install Now", -command => \&install_now );
my $cancel = $mw->Button( -text => "Cancel", -command => \&cancel_button );
my $textarea = $mw->Frame(); #Creating Another Frame
my $txt = $textarea->Text( -width => 40, -height => 10 );
my $srl_y = $textarea->Scrollbar( -orient => 'v', -command => [ yview => $txt ] );
my $srl_x = $textarea->Scrollbar( -orient => 'h', -command => [ xview => $txt ] );
$txt->configure(
-yscrollcommand => [ 'set', $srl_y ],
-xscrollcommand => [ 'set', $srl_x ]
);
$install_button->grid( -row => 1, -column => 1 );
$cancel->grid( -row => 1, -column => 2 );
$frm_name->grid( -row => 1, -column => 1, -columnspan => 2 );
$txt->grid( -row => 1, -column => 1 );
$srl_y->grid( -row => 1, -column => 2, -sticky => "ns" );
$srl_x->grid( -row => 2, -column => 1, -sticky => "ew" );
$textarea->grid( -row => 4, -column => 1, -columnspan => 2 );
$progress->grid( -row => 5, -column => 1, -columnspan => 2 );
MainLoop;
################## functions
sub install_now {
print "\ncalling server_fetch";
&server_fetch();
}
sub cancel_button {
my $response = $mw->messageBox(
-message => "Are you sure you don't want to install the update?",
-type => 'yesno',
-icon => 'question'
);
print "\nresponse" . $response;
if ( $response eq "Yes" ) {
# don't write the files
print "\nresponseexit(0)";
exit(0);
}
}
sub printLocks {
if ( open( LOCKF, ">$base/lockUpdate.txt" ) ) {
print LOCKF qq~1~;
close(LOCKF);
}
}
sub server_fetch {
print qq~\nIn server_fetch~;
$txt->insert( 'end', "\nFetching the files, size is approximately 5mb." );
if ( open( LOG, ">$base/updateApplication.log" ) ) {
&setTime();
}
else {
$txt->insert( 'end', "\nError: cannot transfer update, $!" );
}
my $setup = '';
my $url = qq~[URL unfurl="true"]http://anywhere.com/anyfile.com~;[/URL]
my $return = get($url);
unless ($return) {
$txt->insert( 'end', "\nCannot load the files, connection test failed." );
print LOG "\nCannot load the files, connection test failed.";
}
else {
$txt->insert( 'end', "\nUpdating files, please wait..." );
print LOG "\ncontinuing with the upgrade " . $return;
print STDOUT "Updating";
}
my ( $version, $filesize ) = split ( /\|/, $return );
if ($version) {
my $url = $version;
print LOG qq~\nDownloading $url~;
my $content = get($url);
if ($content) {
print LOG qq~\nDownload complete~;
# move all the old setup files to the addon directory
if ( opendir( PRG, $base ) ) {
while ( defined( $_ = readdir(PRG) ) ) {
if (/PBsetup/) {
if ( -e "$program_dir/$_" ) {
unlink("$program_dir/$_");
}
copy( "$base/$_", "$program_dir/$_" );
if ( -e "$base/$_" ) {
unlink("$base/$_");
}
}
}
}
if ( open( UPDATE, ">$base/PBsetup.exe" ) ) {
binmode(UPDATE);
print LOG qq~\nWriting $base/PBsetup.exe~;
$txt->insert( 'end', qq~\nWriting $base/PBsetup.exe~ );
close(LOG);
print UPDATE $content;
exec("PBsetup.exe");
}
else {
$txt->insert( 'end', "\nCannot write the setup file $base/PBsetup$version.exe. $!"
);
print LOG "\nCannot write the setup file $base/PBsetup$version.exe. $!";
}
}
}
else {
print LOG "\nThere has been an error downloading the update. Are you connected to the web?";
print LOG "\nThere has been an error downloading the update. Are you connected to the web?";
}
}
sub setTime {
my ( $sec, $min, $hour, $day, $mon, $year, $junk ) = gmtime( time() );
$txt->insert( 'end', "\n$sec $min, $hour, $day, $mon, $year" );
print LOG " $sec $min, $hour, $day, $mon, $year\n";
}
__END__