#!/usr/bin/env perl # # vim: set ts=4:noet # # sboupgrade # script to update an installed SlackBuild. # # author: Jacob Pipkin # date: Boomtime, the 39th day of Discord in the YOLD 3178 # license: WTFPL use 5.16.0; use SBO::Lib; use File::Basename; use Getopt::Std; use File::Copy; use strict; use warnings FATAL => 'all'; my %config = %SBO::Lib::config; my $self = basename ($0); sub show_usage () { print < $location, GET => 'REQUIRES'); return unless $$requires[0]; # do nothing if a req list contains %README% return if '%README%' ~~ @$requires; # do nothing if there's a circular requirement FIRST: for my $req (@$requires) { my $req_req = get_from_info (LOCATION => get_sbo_location $req, GET => 'REQUIRES'); return if $sbo ~~ @$req_req; } # else proceed for my $req (@$requires) { my $inst = get_installed_sbos; my $inst_names= get_inst_names $inst; unless ($req ~~ @$inst_names) { say $readme; say "$sbo has $req listed as a requirement."; print "Shall I attempt to install it first? [y] "; if ( =~ /^[Yy\n]/) { system ('/usr/sbin/sboupgrade', '-oN', $req) == 0 or die "$req failed to install.\n"; } } } return; } # look for any (user|group)add commands in the README sub grok_user_group ($) { exists $_[0] or script_error 'grok_user_group requires an argument'; my $readme = shift; my $readme_array = [split /\n/, $readme]; my $cmd_regex = qr/^\s*#\s+((user|group)add.*)/; my @cmds; push @cmds, ($_ =~ $cmd_regex)[0] for @$readme_array; return unless exists $cmds[0]; say "\n". $readme; print "\nIt looks like this slackbuild requires the following command(s)"; say " to be run first:"; say " # $_" for @cmds; print "Shall I run it/them now? [y] "; if ( =~ /^[Yy\n]/) { for my $cmd (@cmds) { system ($cmd == 0) or warn "\"$cmd\" exited non-zero\n"; } } return 1; } # see if the README mentions any options sub grok_options ($) { exists $_[0] or script_error 'grok_options requires an argument'; my $readme = shift; return unless $readme =~ /[A-Z]+=[^\s]/; say "\n". $readme; print "\nIt looks this slackbuilds has options; would you like to set any"; print " when the slackbuild is run? [n] "; if ( =~ /^[Yy]/) { my $ask = sub () { print "\nPlease supply any options here, or enter to skip: "; chomp (my $opts = ); return if $opts =~ /^$/; return $opts; }; my $kv_regex = qr/[A-Z]+=[^\s]+(|\s([A-Z]+=[^\s]+){0,})/; my $opts = &$ask; FIRST: while ($opts !~ $kv_regex) { warn "Invalid input received.\n"; $opts = &$ask; } return $opts; } return; } # prompt for the readme sub readme_prompt ($$) { exists $_[0] or script_error 'readme_prompt requires an argument.'; my ($sbo, $location) = @_; my $fh = open_read (get_readme_path $sbo); my $readme = do {local $/; <$fh>}; close $fh; # check for requirements, useradd/groupadd, options grok_requirements $sbo, $location, $readme; grok_user_group $readme; my $opts = grok_options $readme; print "\n". $readme unless $opts; # present the name as -compat32 if appropriate my $name = $compat32 ? "$sbo-compat32" : $sbo; print "\nProceed with $name? [y]: "; exit 0 unless =~ /^[Yy\n]/; return $opts; } # do the things with the provided sbos - whether upgrades or new installs. sub process_sbos ($) { exists $_[0] or script_error 'process_sbos requires an argument.'; my $todo = shift; my @failures; FIRST: for my $sbo (@$todo) { my $opts = 0; $opts = readme_prompt $sbo, $locations{$sbo} unless $no_readme; # switch compat32 on if upgrading a -compat32 $compat32 = 1 if $sbo =~ /-compat32$/; my ($version, $pkg, $src); eval { ($version, $pkg, $src) = do_slackbuild ( OPTS => $opts, JOBS => $jobs, LOCATION => $locations{$sbo}, COMPAT32 => $compat32, ); }; if ($@) { push @failures, $sbo; } else { unless ($distclean eq 'TRUE') { make_clean $sbo, $src, $version unless $noclean eq 'TRUE'; } else { make_distclean ( SBO => $sbo, SRC => $src, VERSION => $version, LOCATION => $locations{$sbo}, ); } do_upgradepkg $pkg unless $no_install; # move package to $config{PKG_DIR} if defined unless ($config{PKG_DIR} eq 'FALSE') { my $dir = $config{PKG_DIR}; unless (-d $dir) { mkdir ($dir) or warn "Unable to create $dir\n"; } if (-d $dir) { move ($pkg, $dir), say "$pkg stored in $dir"; } else { warn "$pkg left in /tmp\n"; } } elsif ($distclean eq 'TRUE') { unlink $pkg; } } } return @failures; } sub print_failures (;@) { if (exists $_[0]) { say "Failures:"; say " $_" for @_; exit 1; } } # deal with any updates prior to any new installs. # no reason to bother if only_new is specified, ie running from sboinstall. goto INSTALL_NEW if $only_new; # doesn't matter what's updatable and what's not if force is specified my @updates unless $force; unless ($force) { my $updates = get_available_updates; push @updates, $$_{name} for @$updates; } my $todo_upgrade; # but without force, we only want to update what there are updates for unless ($force) { for my $sbo (@ARGV) { push $$todo_upgrades, $sbo if $sbo ~~ @updates; } } else { my $inst = get_installed_sbos; my $inst_names= get_inst_names $inst; FIRST: for my $sbo (@ARGV) { push $todo_upgrade, $sbo if $sbo ~~ @$inst_names; } } my @failures = process_sbos $todo_upgrade if exists $todo_upgrade[0]; print_failures @failures; INSTALL_NEW: exit 0 unless $install_new; my $todo_install; FIRST: for my $sbo (@ARGV) { my $name = $compat32 ? "$sbo-compat32" : $sbo; my $inst = get_installed_sbos; my $inst_names = get_inst_names $inst;; warn "$name already installed\n", next FIRST if $name ~~ @$inst_names; # if compat32 is TRUE, we need to see if the non-compat version exists. if ($compat32) { my $inst = get_installed_sbos; my $inst_names = get_inst_names $inst; unless ($sbo ~~ @$inst_names) { print "\nYou are attempting to install $name, however, $sbo is not"; print " yet installed. Shall I install it first? [y] "; if ( =~ /^[Yy\n]/) { my @args = ('/usr/sbin/sboupgrade', '-oN', $sbo); system (@args) == 0 or exit 1; } else { warn "Please install $sbo\n" and exit 0; } } } push $todo_install, $sbo; } @failures = process_sbos $todo_install if exists $todo_install[0]; print_failures @failures; exit 0;