aboutsummaryrefslogtreecommitdiff
path: root/sboconfig
blob: 83fdceafe476b5d8619fbb16f7824538f2d93f09 (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
#!/usr/bin/env perl
#
# sboconfig
# script to handle sbotools configuration
#
# author: Jacob Pipkin <j@dawnrazor.net>
# date: Pungenday, the 40th day of Discord in the YOLD 3178
# license: WTFPL <http://sam.zoy.org/wtfpl/COPYING>

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

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

sub show_usage {
	print <<EOF
Usage: $self [options] [arguments]

Options:
  -h: this screen.
  -v: version information.
  -l: show current options.

Config options (defaults shown):
  -c FALSE:
      NOCLEAN: if TRUE, do NOT clean up after building by default.
  -d FALSE:
      DISTCLEAN: if TRUE, DO clean distfiles by default after building.
  -j FALSE:
      JOBS: numeric -j setting to feed to make for multicore systems.
  -p FALSE:
      PKG_DIR: set a directory to store packages in. 
  -s /usr/sbo:
      SBO_HOME: set the SBo directory.

EOF
}

my %options;
getopts ('hvlc:d:p:s:j:', \%options);

show_usage () and exit (0) if exists $options{h};
show_version () and exit (0) if exists $options{v};

if (exists $options{l}) {
	my @keys = sort {$a cmp $b} keys %config;
	for my $key (@keys) {
		print "$key=$config{$key}\n";
	}
	exit 0;
}

show_usage () unless %options;

my %valid_confs = (
	c => 'NOCLEAN',
	d => 'DISTCLEAN',
	j => 'JOBS',
	p => 'PKG_DIR',
	s => 'SBO_HOME',
);
	
# setup what's being changed.
my %changes;
while (my ($key, $value) = each %valid_confs) {
	$changes{$value} = $options{$key} if exists $options{$key};
}
if (exists $changes{JOBS}) {
	unless ($changes{JOBS} =~ /^\d+$/ || $changes{JOBS} eq 'FALSE') {
		die "You have provided an invalid parameter for -j\n";
	}
}

my $conf_dir = $SBO::Lib::conf_dir;;
my $conf_file = $SBO::Lib::conf_file;

# safely modify our conf file; copy to a temp location, edit the temp file,
# move the edited file into place
sub config_write {
	script_error ('config_write requires two arguments.') unless exists $_[1];
	my ($key, $val) = @_;
	if (! -d $conf_dir) {
		mkdir ($conf_dir)
			or die "Unable to create $conf_dir. Exiting.\n";
	}
	if (-f $conf_file) {
		# get a temp file and fill it with the contents of our config file
		my ($fh, $filename) = make_temp_file ();
		open my $reader, '<', $conf_file;
		print {$fh} <$reader>;
		close $fh;
		# tie the file so that if $key is already there, we just change that
		# line and untie it
		tie my @temp, 'Tie::File', $filename;
		my $has = 'FALSE';
		my $regex = qr/\A\Q$key\E=/;
		FIRST: for my $tmpline (@temp) {
			if ($tmpline =~ $regex) {
				$has = 'TRUE';
				$tmpline = "$key=$val";
				last FIRST;
			}
		}
		untie @temp;
		# otherwise, append our new $key=$value pair
		if ($has eq 'FALSE') {
			open (my $writer, '>>', $filename);
			print {$writer} "$key=$val\n";
			close $writer;
		}
		move ($filename, $conf_file);
	} else {
		# no config file, easiest case of all.
		open my $writer, '>', $conf_file;
		print {$writer} "$key=$val\n";
		close $writer;
	}
}

while (my ($key, $value) = each %changes) {
	print "Setting $key to $value...\n";
	config_write ($key, $value);
}

exit 0;