aboutsummaryrefslogtreecommitdiff
path: root/sboclean
blob: 0c0e30a6f019176e044e8581be8930fca7f68c1b (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
#!/usr/bin/env perl
#
# vim: set ts=4:noet
#
# sboclean
# script to clean stuff left around from sbotools.
#
# author: Jacob Pipkin <j@dawnrazor.net>
# date: Boomtime, the 6th day of Confusion in the YOLD 3178
# license: WTFPL <http://sam.zoy.org/wtfpl/COPYING>

use SBO::Lib;
use File::Basename;
use Getopt::Std;
use File::Path qw(remove_tree);
use strict;
use warnings FATAL => 'all';

my %config = %SBO::Lib::config;
my $self = basename ($0);

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

Options:
  -h: this screen.
  -v: version information.
  -d: clean distfiles.
  -w: clean working directories.
  -i: be interactive.

EOF
}

my %options;
getopts ('hvdwi', \%options);

show_usage () && exit (0) if exists $options{h};
show_version () && exit (0) if exists $options{v};
my $clean_dist = exists $options{d} ? 'TRUE' : 'FALSE';
my $clean_work = exists $options{w} ? 'TRUE' : 'FALSE';
my $interactive = exists $options{i} ? 'TRUE' : 'FALSE';

if ($clean_dist eq 'FALSE' && $clean_work eq 'FALSE') {
	show_usage ();
	die "You must specify at least one of -d or -w.\n";
}

sub remove_stuff {
	exists $_[0] or script_error ('remove_stuff requires an argument');
	print "Nothing to do.\n" and return 1 unless -d $_[0];
	my $dir = shift;
	opendir (my $dh, $dir);
	FIRST: while (my $ls = readdir $dh) {
		next FIRST if $ls =~ /^\.[\.]{0,1}$/;
		my $full = "$dir/$ls";
		if ($interactive eq 'TRUE') {
			print "Remove $full? [n] ";
			next FIRST unless <STDIN> =~ /^[Yy]/;
		}
		unlink $full if -f $full;
		remove_tree ($full) if -d $full;
	}
}

remove_stuff ($config{SBO_HOME} . '/distfiles') if $clean_dist eq 'TRUE';
remove_stuff ('/tmp/SBo') if $clean_work eq 'TRUE';

exit 0;