Skip to content

Instantly share code, notes, and snippets.

@nathanharper
Last active August 29, 2015 14:02
Show Gist options
  • Save nathanharper/bc83dc30cce964594351 to your computer and use it in GitHub Desktop.
Save nathanharper/bc83dc30cce964594351 to your computer and use it in GitHub Desktop.
Script to look for a new version of Davmail on Sourceforge, download it, and install it.
#!/usr/bin/perl
use strict;
use warnings;
use File::Path qw(make_path);
my $download_url = "http://sourceforge.net/projects/davmail/files/latest/download?source=files";
my $user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/34.0.1847.116 Chrome/34.0.1847.116 Safari/537.36';
my $version_file = "davmail_version";
my $davmail_deb = "davmail_latest.deb";
my $version_path = "$ENV{'HOME'}/.config/davmail_updater";
make_path($version_path) unless(-d $version_path);
my $version = `curl -s -w '%{redirect_url}' -o /dev/null '$download_url'`;
$version =~ s/^.*\/[^\/-]+-((\d+\.){2}\d+)-[^\/\?]+\?.*$/$1/g;
print "version: $version\n";
my $v_file_exists = 1;
open(my $fh, '<', "$version_path/$version_file") or $v_file_exists = 0;
if ($v_file_exists == 1) {
my $line = <$fh>;
close $fh;
print "last version: $line\n";
if ($line eq $version) {
print "No new version found\n";
exit 0;
}
}
else {
print "No version file found\n";
}
open($fh, '+>', "$version_path/$version_file") or die "Could not write new version.";
print $fh "$version";
close $fh;
print "Downloading version $version\n";
# Without the fake user agent, Sourceforge will send us a zip file.
# Trick it into sending the download page so we can grab the .deb URL.
$download_url = `wget --quiet -U '$user_agent' '$download_url' -O -`;
$download_url =~ s/^.*(<a\s[^>]*class="direct-download"[^>]*>).*$/$1/gs;
$download_url =~ s/^.* href="([^"]+)".*$/$1/g;
$download_url =~ s/&amp;/&/g;
$download_url =~ s/'//g;
if ($download_url !~ /^https?:\/\/[^"']+$/) {
print "Download url invalid: $download_url\n";
exit 1;
}
print "Downloading: $download_url\n";
print `wget '$download_url' -O '$version_path/$davmail_deb'`, "\n";
print "Installing davmail\n";
print `yes | gdebi '$version_path/$davmail_deb' 2>&1`, "\n";
print "Finished.\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment