#!/usr/bin/env perl # # sboconfig # script to handle sbotools configuration # # author: Jacob Pipkin # date: Pungenday, the 40th day of Discord in the YOLD 3178 # license: WTFPL use strict; use warnings FATAL => 'all'; use SBO::Lib; use File::Basename; use Getopt::Std; use File::Copy; use File::Path qw(make_path); my %config = %SBO::Lib::config; my $self = basename ($0); sub show_usage { print < '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}) { die "You have provided an invalid parameter for -j\n" unless ($changes{JOBS} =~ /^\d+$/ || $changes{JOBS} eq 'FALSE'); } 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) { my ($fh, $filename) = tempfile (DIR => $SBO::Lib::tempdir); close $fh; copy ($conf_file, $filename); # 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') { my $fh = open_fh ($filename, '>>'); print {$fh} "$key=$val\n"; close $fh; } move ($filename, $conf_file); } else { # no config file, easiest case of all. my $fh = open_fh ($conf_file, '>'); print {$fh} "$key=$val\n"; close $fh; } } while (my ($key, $value) = each %changes) { print "Setting $key to $value...\n"; config_write ($key, $value); } exit 0;