A variety of simple automation scripts and enhancements to Sway and i3.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
4.7KB

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use IO::Prompter;
  5. use File::Which qw( which );
  6. # Configurable variables
  7. my $lang = 'en-CA';
  8. my $install = "$ENV{HOME}/.local/bin";
  9. my $working = "/tmp/firefox.new";
  10. # Download link as provided by https://www.mozilla.org/$lang/firefox/developer/
  11. my $url = 'https://download.mozilla.org/?product=firefox-devedition-latest-ssl&os=linux64&lang='.$lang;
  12. # Ensure that we can unzip
  13. unless (which("bunzip2")) {
  14. die "Requires bunzip2. Try:\nsudo apt install zutils or your distro's equivalent\n";
  15. }
  16. # Only understood argument is to not bother verifying the package
  17. my ($download_key, $verify) = (0,1);
  18. if (defined $ARGV[0]) {
  19. if ($ARGV[0] eq '--no-verify') {
  20. $verify = 0;
  21. } else {
  22. die "Didn't understand argument $ARGV[0]\n";
  23. }
  24. }
  25. # If verification is to be done, make sure we have the key
  26. unless ($verify) {
  27. print "Checking for Mozilla GPG key in keyring...\n";
  28. system("gpg --list-keys release\@mozillla.com 2>&1 > /dev/null");
  29. if ($?) {
  30. my $YN = prompt (-in => *STDIN, "You don't currently have Mozilla's GPG key in your keyring.\nWould you like to install it? If not, installation will not be verified. [y/N]: ", -single);
  31. if ($YN eq 'y' || $YN eq 'Y') {
  32. $download_key = 1;
  33. } else {
  34. $verify = 0;
  35. }
  36. }
  37. }
  38. # If a previous download still exists remove it
  39. if ( -e $working ) {
  40. system("rm -rf $working");
  41. if ($?) {
  42. die "Working directory $working already exists and failed to remove: $!\n";
  43. }
  44. }
  45. # Get version currently installed
  46. my $current = `$install/firefox/firefox-bin --version`;
  47. $current =~ s/.*\s([^\s])/$1/;
  48. chomp $current;
  49. use WWW::Mechanize;
  50. my $mech = WWW::Mechanize->new();
  51. # $url is actually going to redirect to the proper current dowload, so just get the redirect.
  52. my $head = $mech->head($url);
  53. my ($location, $version);
  54. # This redirect will have the version id, so we can use that to determine if a new version actually exists
  55. if ($head->{'_msg'}) {
  56. $version = $location = $head->{'_previous'}->{'_headers'}->{'location'};
  57. $version =~ s/^.*\-([^\-]*)\.tar\.bz2$/$1/;
  58. if ($version eq $current) {
  59. die "Current ($current) is the same as New ($version)\n";
  60. }
  61. } else {
  62. print "unable to find new download\n";
  63. }
  64. mkdir($working) || die "Couldn't make $working: $!\n";
  65. # Download package
  66. print "Fetching package $location...\n";
  67. $mech->get($location);
  68. $mech->save_content( $working."/firefox-".$version.".tar.bz2", binmode => ':raw', decoded_by_headers => 1 );
  69. # If verification is required, get signature as well
  70. if ($verify) {
  71. $location .= '.asc';
  72. print "Fetching GPG signature $location...\n";
  73. $mech->get($location);
  74. $mech->save_content( $working."/firefox-".$version.".tar.bz2.asc", binmode => ':raw', decoded_by_headers => 1 );
  75. }
  76. # If key still needs to be fetched, get it
  77. if ($download_key) {
  78. $location =~ s/linux-x86_64.*$/KEY/;
  79. print "Fetching GPG key $location...\n";
  80. $mech->get($location);
  81. $mech->save_content( $working."/mozilla.pgp", binmode => ':raw', decoded_by_headers => 1 );
  82. # And install it
  83. print "Installing Mozilla GPG key in keyring...\n";
  84. system("gpg --import $working/mozilla.pgp 2>&1 > /dev/null");
  85. if ($?) {
  86. die "Warning: failed to import key. Cannot verify integrity.\nDownloaded to $working. You can check and install it to $install manually: $!\n";
  87. }
  88. unlink("$working/mozilla.pgp");
  89. }
  90. # Verify the package
  91. if ($verify) {
  92. print "Verifying download with Mozilla GPG key...\n";
  93. system("gpg --verify $working/firefox-$version.tar.bz2.asc");
  94. if ($?) {
  95. die "Warning: failed to verify download. Signing failed.\nDownloaded to $working. You can check and install it to $install manually: $!\n";
  96. }
  97. }
  98. # Uncompress
  99. print "Uncompressing download with bunzip2...\n";
  100. system("bunzip2 $working/firefox-$version.tar.bz2");
  101. if ($?) {
  102. die "Failed to uncompress: $!\n";
  103. }
  104. # Extract
  105. print "Extracting from TAR archive...\n";
  106. system("tar -xf $working/firefox-$version.tar -C $working");
  107. if ($?) {
  108. die "Failed to extract: $!\n";
  109. }
  110. # Bin the old backup
  111. if (-e "$install/.firefox.old") {
  112. print "Removing previous backup folder...\n";
  113. system("rm -rf $install/.firefox.old");
  114. if ($?) {
  115. die "Failed to remove: $!\n";
  116. }
  117. }
  118. # Move current to old
  119. print "Backing up currently installed version ($current) to $install/.firefox.old...\n";
  120. system("mv $install/firefox $install/.firefox.old");
  121. if ($?) {
  122. die "Failed to move: $!\n";
  123. }
  124. # Move new to current
  125. print "Moving new version to $install for final installation...\n";
  126. system("mv $working/firefox $install/");
  127. if ($?) {
  128. die "Failed to move: $!\n";
  129. }
  130. # Hurray!
  131. print "Installation of version $version complete. You should restart firefox whenever it is convenient.\n";
  132. exit();