aboutsummaryrefslogtreecommitdiff
path: root/sboremove
blob: 5ec429aefa90ff9e68e4af2f3482929c319e9e5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env perl
#
# vim: set ts=4:noet
#
# sboremove
# script to remove an installed SlackBuild
#
# author: Luke Williams <xocel@iquidus.org>
# license: WTFPL <http://sam.zoy.org/wtfpl/COPYING>

use 5.16.0;
use strict;
use warnings FATAL => 'all';
use SBO::Lib;
use Getopt::Long qw(:config bundling);
use File::Basename;

my $self = basename ($0);

sub show_usage () {
	print <<EOF
Usage: $self [options] sbo

Options (defaults shown first where applicable):
  -h|--help:
    this screen.
  -v|--version:
    version information.
  -R|--norequirements:
    do not parse requirements.
  -a|--alwaysask:
    always ask to remove, even if required by other packages on system.

Note: optional dependencies need to be removed separately. 

EOF
}

my ($help, $vers, $non_int, $no_reqs, $alwaysask, @excluded);

GetOptions (
	'help|h'			=> \$help,
	'version|v'			=> \$vers,
	'norequirements|R'	=> \$no_reqs, 
	'nointeractive'		=> \$non_int,
	'alwaysask|a'		=> \$alwaysask,
);

show_usage and exit 0 if $help;
show_version and exit 0 if $vers;
show_usage and exit 0 unless exists $ARGV[0];

my $inst_names = get_inst_names (get_installed_sbos);

my @remove;
for my $sbo (@ARGV) {
	if (get_sbo_location ($sbo)) {
		if ($sbo ~~ @$inst_names) {
			push @remove, $sbo; 
		} else {
			say "$sbo is not installed";
		} 
	} else {
		say "Unable to locate $sbo in the SlackBuilds.org tree."
	}
}

my ($remove_queue, %required_by, %warnings, @confirmed);

sub get_requires ($) {
	my $location = get_sbo_location (shift);
	my $requires = get_from_info (LOCATION => $location, GET => 'REQUIRES')
		if $location;
	return $requires;
}

# Determine required by for all installed sbo's
sub get_reverse_reqs () {
	FIRST: for my $inst (@$inst_names) {
		my $requires = get_requires $inst;
		next FIRST unless $$requires[0];
		for my $req (@$requires) {
			unless ( $req eq "%README%" ) {
				push @$required_by{$req}, $inst if $req ~~ $inst_names;
			}		
		}	
	}
}

sub get_required_by ($) {
	my $sbo = shift;
	my @dep_of;
	get_reverse_reqs unless keys %required_by > 0;
	if ( $required_by{$sbo} ) {
		for my $req_by (@{$required_by{$sbo}}) {
			unless ($req_by ~~ @confirmed) {
				push @dep_of, $req_by;
			}
		}
	}
	return \@dep_of;
}

sub confirm_remove ($) {
	my $sbo = shift;
	unless ($sbo ~~ @confirmed) {
		push @confirmed, $sbo;
	}
}

# Determine dependencies & warnings
if ($no_reqs) {
	$remove_queue = \@remove;
} else {
	$remove_queue = get_build_queue(\@remove, \%warnings);
	@$remove_queue = reverse(@$remove_queue);
}

# Check if packages in queue are actually installed on system 
my @temp;
if ($inst_names) {
	for my $sbo (@$remove_queue) {
		if ($sbo ~~ $inst_names) {
			push @temp, $sbo;
		}
	}
	$remove_queue = \@temp;
}

# Gather instructions from user
unless ($non_int) {
	my ($req_by, $req_count, $cnf_count);
	for my $pkg (@$remove_queue) {
		$req_by = get_required_by $pkg;
		$cnf_count = 0;
		for my $val (@$req_by) {
			if ( $val ~~ @confirmed ) { $cnf_count++; }  
		}
		my $rootpkg = ($pkg ~~ @remove);
		if ($cnf_count == @$req_by or $rootpkg or $alwaysask) {  
			say $pkg;
			if ( @$req_by ) {
				say "Required by: " . join(' ', @$req_by);
			} else {
				unless ($rootpkg) {
					#Skip this prompt if the pkg in question was passed by user.
					say "It appears $pkg will no longer be required on your system";
				}
			}
			if ( exists $warnings{$pkg} ) {
				say "$pkg suggests you view its README before proceeding,";
				print "view now? [y] : ";
				chomp(my $viewrm = <STDIN>);
				if ($viewrm =~ /^[Yy]/) {
					print "\n";
					my $locrm = get_sbo_location ($pkg);
					$locrm .= '/README';
					open(README, '<', $locrm) or die "could not open $locrm";
					for my $line (<README>) {
						print $line;
					}
				}
			}
			print "Remove $pkg? [n] : ";
			chomp(my $userin = <STDIN>);
			if ($userin =~ /^[Yy]/) {
				confirm_remove $pkg;
				say "* added $pkg to remove queue\n";
			} else {
				say "* ignoring $pkg\n"
			}
		}
	}
} else {
	for my $sbo (@$remove_queue) {
		confirm_remove $sbo;
	}
}

# Show remove queue
my $remove_count = @confirmed;
if ($remove_count) {
	say "Removing $remove_count package(s)";
	for my $pkg (@confirmed) {
		print "$pkg ";
	}
	say "\n";	
} else {
	say 'Nothing to remove.';
	exit 0;
}

unless ($non_int) {
	print 'Are you sure you want to continue? [n] : ';
	unless (<STDIN> =~ /^[Yy]/) {
		say 'Exiting.';
		exit 0;
	}
}

system ("/sbin/removepkg $_") for @confirmed;

say "All operations have completed successfully.";

exit 0;