Skip to content

Instantly share code, notes, and snippets.

@noureddin
Last active November 26, 2020 06:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noureddin/fcec40f5b9410de3406572d96c6b375b to your computer and use it in GitHub Desktop.
Save noureddin/fcec40f5b9410de3406572d96c6b375b to your computer and use it in GitHub Desktop.
A small script to install/update Anki for Desktop Gnu/Linux systems
#!/usr/bin/env perl
use v5.14; use warnings; use autodie; use utf8;
# Run as root to install or upgrade Anki. To upgrade, you have to have Anki
# installed using this script, otherwise, it will be downloaded and installed
# again. But that would not make you loss any data.
# It needs Perl 5.14, axel, tar, and make. tar and make are required.
# Perl 5.12 may be used instead. And axel can be replaced by your preferred
# downloader below.
# This is a small script to install/update Anki for Desktop Gnu/Linux systems.
# Download it and optionally put it somewhere in your (the root's) $PATH,
# as it must be run with the root to install Anki. (Eg, in '/usr/bin'.)
# It checks if there is Anki installed before by this script, and compares the
# latest version to it, to upgrade only if there is a new version.
# On error, it deletes the package it downloaded and the extracted files if any.
# To preserve them, change '$tmpdir' below to a directory you have,
# instead of '$tmpdirobj->dirname';
# On successful installation, it creates, in the same directory as itself, with
# the same owner, group, and permissions, a new script called AnkiUninstall,
# which uninstalls Anki.
use open qw( :encoding(UTF-8) :std );
binmode STDIN, ':encoding(UTF-8)';
binmode STDOUT, ':encoding(UTF-8)';
use File::Temp;
use File::Basename 'dirname';
use File::Spec;
use File::stat;
$SIG{__DIE__} = sub { chdir "/" }; # so that $tmpdir (below) can be removed
# 0. get the pkg url, + initializations
my $page = scalar(`wget -qO- 'https://apps.ankiweb.net/'`);
unless ($page) {
die "Could not download the AnkiWeb page. Please check you Internet connection.\n";
}
my ($url) = $page =~ m|href="(https://github[.]com/ankitects/anki/releases/download/[^"]+-linux-amd64[.]tar[.]bz2)"|;
undef $page;
my $installscript = File::Spec->rel2abs(__FILE__);
my $uninstallscript = dirname($installscript).'/AnkiUninstall';
my ($pkgname) = $url =~ m|([^/]+)$|;
my ($pkgdir) = $pkgname =~ m|^(.+)[.]tar[.]bz2|;
my ($version) = $pkgname =~ m|anki-([^-]+)|;
my $tmpdirobj = File::Temp->newdir; # auto-removed on exit
my $tmpdir = $tmpdirobj->dirname;
chdir $tmpdir;
# 1. check if the pkg is newer than what we have
if (-e $uninstallscript) { # we have Anki installed by this script
open my $un, '<', $uninstallscript;
<$un>; # the shebang; ignored
<$un> =~ /([.0-9]+)/; # the version string
if ($1 eq $version) { # assume different version means it's a newer version
die "No newer version for Anki yet.\n"
}
close $un;
}
# 2. if so, get the pkg
print "Downloading Anki $version\n";
system('axel', $url, '-a')==0 or die "Error in downloading.\n";
print "Downloaded Anki $version\n";
# 3. extract
print "Extracting\n";
system('tar', 'xvjf', $pkgname)==0 or die "Error in extraction.\n";
chdir $pkgdir;
print "Extracted\n";
# 4. install
print "Installing\n";
system('make', 'install')==0 or die "Error in installating Anki; are you root?\n";
print "Installed successfully\n";
# 5. prepare the AnkiUninstall script
open my $fo, '>', $uninstallscript;
open my $fi, '<', 'Makefile';
print { $fo } "#!/bin/sh\n# Anki $version\nmake uninstall -f - <<'END_OF_ANKI_MAKEFILE'\n";
print { $fo } <$fi>;
print { $fo } "\nEND_OF_ANKI_MAKEFILE\nrm -f '$uninstallscript'\n";
close $fi;
close $fo;
# 6. copy the permissions, owner, and group of AnkiInstall (this script) to AnkiUninstall
my $s = stat($installscript);
chmod $s->mode & 07777, $uninstallscript;
chown $s->uid, $s->gid, $uninstallscript;
print "Uninstallation script created successfully; call 'sudo AnkiUninstall' to remove Anki.\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment