sbotools2

Maintenance fork of the original sbotools version 2
git clone git://git.server.ky/slackcoder/sbotools2
Log | Files | Refs | README

sboconfig (5545B)


      1 #!/usr/bin/perl
      2 #
      3 # vim: ts=4:noet
      4 #
      5 # sboconfig
      6 # script to handle sbotools configuration
      7 #
      8 # authors: Jacob Pipkin <j@dawnrazor.net>
      9 #          Luke Williams <xocel@iquidus.org>
     10 #          Andreas Guldstrand <andreas.guldstrand@gmail.com>
     11 # maintainer: Slack Coder <slackcoder@server.ky>
     12 
     13 use 5.16.0;
     14 use strict;
     15 use warnings FATAL => 'all';
     16 use SBO::Lib qw/ _ERR_USAGE slurp usage_error script_error open_fh %config $conf_dir $conf_file show_version /;
     17 use File::Basename;
     18 use Getopt::Long qw(:config no_ignore_case_always);
     19 use File::Copy;
     20 use File::Path qw(make_path);
     21 use File::Temp qw(tempfile);;
     22 
     23 my $self = basename($0);
     24 
     25 sub show_usage {
     26 	print <<"EOF";
     27 Usage: $self [options] [arguments]
     28 
     29 Options:
     30   -h|--help:
     31     this screen.
     32   -v|--version:
     33     version information.
     34   -l|--list:
     35     show current options.
     36 
     37 Config options (defaults shown):
     38   -c|--clean FALSE:
     39       NOCLEAN: if TRUE, do NOT clean up after building by default.
     40   -d|--distclean FALSE:
     41       DISTCLEAN: if TRUE, DO clean distfiles by default after building.
     42   -f|--fallback-archive ftp://slackware.uk/sbosrcarch:
     43       FALLBACK_ARCHIVE: Fallback URL to download package source's.  FALSE to disable.
     44   -j|--jobs FALSE:
     45       JOBS: numeric -j setting to feed to make for multicore systems.
     46   -g|--gpg-key D3076BC3E783EE747F09B8B70368EF579C7BA3B6:
     47       GPG_KEY GPG key ID for verification.
     48   -p|--pkg-dir FALSE:
     49       PKG_DIR: set a directory to store packages in.
     50   -s|--sbo-home /usr/sbo:
     51       SBO_HOME: set the SBo directory.
     52   -o|--local-overrides FALSE:
     53       LOCAL_OVERRIDES: a directory containing local overrides.
     54   -V|--slackware-version FALSE:
     55       SLACKWARE_VERSION: use the SBo repository for this version.
     56   -r|--repo FALSE:
     57       REPO: use a repository other than SBo.
     58 
     59 EOF
     60 	return 1;
     61 }
     62 
     63 my %options;
     64 
     65 if (! GetOptions(\%options,
     66   'help|h',
     67   'version|v',
     68   'list|l',
     69   'noclean|c=s',
     70   'fallback-archive|f=s',
     71   'distclean|d=s',
     72   'jobs|j=s',
     73   'pkg-dir|p=s',
     74   'sbo-home|s=s',
     75   'local-overrides|o=s',
     76   'slackware-version|V=s',
     77   'repo|r=s',
     78   'gpg-key|g=s',
     79 )) {
     80   show_usage();
     81   exit 1;
     82 }
     83 
     84 if ($options{help}) { show_usage(); exit 0 }
     85 if ($options{version}) { show_version(); exit 0 }
     86 
     87 my %valid_confs = (
     88 	noclean             => 'NOCLEAN',
     89 	distclean           => 'DISTCLEAN',
     90 	'fallback-archive'  => 'FALLBACK_ARCHIVE',
     91 	jobs                => 'JOBS',
     92 	'gpg-key'           => 'GPG_KEY',
     93 	'pkg-dir'           => 'PKG_DIR',
     94 	'sbo-home'          => 'SBO_HOME',
     95 	'local-overrides'   => 'LOCAL_OVERRIDES',
     96 	'slackware-version' => 'SLACKWARE_VERSION',
     97 	'repo'              => 'REPO',
     98 );
     99 
    100 my %params = (
    101 	NOCLEAN           => 'c|--noclean',
    102 	DISTCLEAN         => 'd|--distclean',
    103     FALLBACK_ARCHIVE  => 'f|--fallback-archive',
    104 	JOBS              => 'j|--jobs',
    105 	GPG_KEY           => 'g|--gpg-key',
    106 	PKG_DIR           => 'p|--pkg-dir',
    107 	SBO_HOME          => 's|--sbo-home',
    108 	LOCAL_OVERRIDES   => 'o|--local-overrides',
    109 	SLACKWARE_VERSION => 'V|--slackware-version',
    110 	REPO              => 'r|--repo',
    111 );
    112 
    113 if (exists $options{list}) {
    114 	my @keys = sort {$a cmp $b} keys %config;
    115 	say "sboconfig -$params{$_}:\n    $_=$config{$_}" for @keys;
    116 	exit 0;
    117 }
    118 
    119 if (not %options) {
    120 	show_usage();
    121 	exit 0;
    122 }
    123 
    124 # setup what's being changed, sanity check.
    125 my %changes;
    126 for my $key (keys %valid_confs) {
    127 	my $value = $valid_confs{$key};
    128 	$changes{$value} = $options{$key} if exists $options{$key};
    129 }
    130 
    131 my $warn = 'You have provided an invalid parameter for';
    132 
    133 if (exists $changes{NOCLEAN}) {
    134 	usage_error("$warn -c") unless $changes{NOCLEAN} =~ /^(TRUE|FALSE)$/;
    135 }
    136 if (exists $changes{DISTCLEAN}) {
    137 	usage_error("$warn -d") unless $changes{DISTCLEAN} =~ /^(TRUE|FALSE)$/;
    138 }
    139 if (exists $changes{JOBS}) {
    140 	usage_error("$warn -j") unless $changes{JOBS} =~ /^(\d+|FALSE)$/;
    141 }
    142 if (exists $changes{GPG_KEY}) {
    143 	usage_error("$warn -g") unless $changes{GPG_KEY} =~ /^([0-9A-F]+|FALSE)$/;
    144 }
    145 if (exists $changes{PKG_DIR}) {
    146 	usage_error("$warn -p") unless $changes{PKG_DIR} =~ qr#^(/|FALSE$)#;
    147 }
    148 if (exists $changes{SBO_HOME}) {
    149 	usage_error("$warn -s") unless $changes{SBO_HOME} =~ qr#^(/|FALSE$)#;
    150 }
    151 if (exists $changes{LOCAL_OVERRIDES}) {
    152 	usage_error("$warn -o") unless $changes{LOCAL_OVERRIDES} =~ qr#^(/|FALSE$)#;
    153 }
    154 if (exists $changes{SLACKWARE_VERSION}) {
    155 	usage_error("$warn -V") unless $changes{SLACKWARE_VERSION} =~ m/^(\d+\.\d+|FALSE)$/;
    156 }
    157 
    158 unless ($< == 0) {
    159 	warn "This script requires root privileges.\n";
    160 	exit _ERR_USAGE;
    161 }
    162 
    163 sub config_write {
    164 	script_error('config_write requires at least two arguments.') unless @_ >= 2;
    165 
    166 	if (! -d $conf_dir) {
    167 		mkdir $conf_dir or usage_error("Unable to create $conf_dir. Exiting.");
    168 	}
    169 
    170 	my $conf = slurp($conf_file) || '';
    171 	_fixup_conf($conf);
    172 
    173 	while (@_ >= 2) {
    174 		my $key = shift;
    175 		my $val = shift;
    176 		say "Setting $key to $val...";
    177 
    178 		if ($conf =~ /^\Q$key\E=/m) {
    179 			$conf =~ s/^\Q$key\E=.*$/$key=$val/m;
    180 		} else {
    181 			$conf .= "$key=$val\n";
    182 		}
    183 	}
    184 
    185 	_fixup_conf($conf);
    186 
    187 	my ($conffh, $exit) = open_fh($conf_file, '>');
    188 	if ($exit) {
    189 		warn $conffh;
    190 		exit $exit;
    191 	}
    192 	print {$conffh} $conf;
    193 }
    194 
    195 # make sure there are no duplicate keys in the config
    196 sub _fixup_conf {
    197 	my @lines = split /\n/, $_[0];
    198 	my @fixed;
    199 	my %keys;
    200 	foreach my $line (@lines) {
    201 		# if it's a comment or blank line, just pass it through
    202 		if ($line =~ /^(#|\s*$)/) { push @fixed, $line; next; }
    203 
    204 		my ($key, $val) = split /=/, $line;
    205 		next if exists $keys{$key};
    206 		$keys{$key}++;
    207 		push @fixed, $line;
    208 	}
    209 
    210 	$_[0] = join "\n", @fixed, ''; # make sure we end with a newline if there are any lines
    211 }
    212 
    213 config_write(%changes) if %changes;
    214 
    215 exit 0;