aboutsummaryrefslogtreecommitdiff
path: root/sboclean
blob: fd6f5c82d0cf7c0c2b609cfd34eb516d625b0e35 (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 5.16.0;
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} ? 1 : 0;
my $clean_work = exists $options{w} ? 1 : 0;
my $interactive = exists $options{i} ? 1 : 0;

unless ($clean_dist || $clean_work) {
	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';
	-d $[0] or say "Nothing to do." and return 1;
	my $dir = shift;
	opendir (my $dh, $dir);
	FIRST: while (my $ls = readdir $dh) {
		next FIRST if $ls =~ /^(\.){1,2}$/;
		my $full = "$dir/$ls";
		if ($interactive) {
			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;
remove_stuff '/tmp/SBo' if $clean_work;

exit 0;