5 kx #!/bin/env perl
5 kx
5 kx use FindBin;
5 kx use lib $FindBin::Bin;
5 kx
5 kx use strict;
5 kx use warnings FATAL => 'all';
5 kx
5 kx use File::Basename;
5 kx use File::Temp;
5 kx use File::Path qw(make_path remove_tree);
5 kx use Storable qw(lock_store lock_nstore lock_retrieve);
5 kx use _kxLab;
5 kx
5 kx
5 kx my $program = basename( $0 );
5 kx
5 kx my ($hardware, $flavour, $poolname, $operation, $name, $value);
5 kx my $value_only = 'no';
5 kx
5 kx sub usage
5 kx {
5 kx print <<EOF;
5 kx
5 kx Usage: $program [options]
5 kx Options:
5 kx {--get | --set} - type of operation;
5 kx --hardware=HARDWARE - where HARDWARE ia a current HARDWARE name;
5 kx --flavour=FLAVOUR - where FLAVOUR ia a current FLAVOUR name;
5 kx --pool-name=NAME - where NAME is the variables pool name;
5 kx --name=NAME - where NAME is a variable name;
5 kx --value=string - where 'string' is a value of variable;
5 kx --value-only - if this option is used then $program
5 kx prints out only value of required vatiable,
5 kx if this option is not used then $program
5 kx prints out the pair 'NAME=VALUE' without
5 kx spaces around of equal ('=') symbol.
5 kx
5 kx EOF
5 kx exit;
5 kx }
5 kx
5 kx # args:
5 kx # - environment pool name ( perl,for example );
5 kx # - hardware name ( ci20, for example );
5 kx # - flavour name ( m512, for example );
5 kx # - variable;
5 kx # - value;
5 kx sub save_env
5 kx {
5 kx my $fname = shift;
5 kx my $name = shift;
5 kx my $value = shift;
5 kx
5 kx my $tabref;
5 kx
5 kx if( -s $fname and defined( Storable::file_magic( $fname ) ) )
5 kx {
5 kx $tabref = lock_retrieve( $fname );
5 kx if( ! defined $tabref )
5 kx {
5 kx $tabref = 0;
5 kx }
5 kx }
5 kx $tabref->{ $name } = $value;
5 kx lock_store( \%$tabref, $fname );
5 kx }
5 kx
5 kx sub get_env
5 kx {
5 kx my $fname = shift;
5 kx my $name = shift;
5 kx
5 kx my $value = "";
5 kx my $tabref;
5 kx
5 kx if( -s $fname and defined( Storable::file_magic( $fname ) ) )
5 kx {
5 kx $tabref = lock_retrieve( $fname );
5 kx }
5 kx if( defined $tabref )
5 kx {
5 kx my $val = $tabref->{ $name };
5 kx if( defined $val ) { $value = $val; }
5 kx }
5 kx return $value;
5 kx }
5 kx
5 kx
5 kx foreach ( @ARGV )
5 kx {
5 kx if( /--hardware=(\S*)/ )
5 kx {
5 kx $hardware = $1;
5 kx }
5 kx elsif( /--flavour=(\S*)/ )
5 kx {
5 kx $flavour = $1;
5 kx }
5 kx elsif( /--pool-name=(\S*)/ )
5 kx {
5 kx $poolname = $1;
5 kx }
5 kx elsif( /--name=(\S*)/ )
5 kx {
5 kx $name = $1;
5 kx }
5 kx elsif( /--value=(\S*)/ )
5 kx {
5 kx $value = $1;
5 kx }
5 kx elsif( /--set/ )
5 kx {
5 kx $operation = 'set';
5 kx }
5 kx elsif( /--get/ )
5 kx {
5 kx $operation = 'get';
5 kx }
5 kx elsif( /--value-only/ )
5 kx {
5 kx $value_only = 'yes';
5 kx }
5 kx elsif( /--help/ )
5 kx {
5 kx usage;
5 kx }
5 kx else
5 kx {
5 kx usage;
5 kx }
5 kx }
5 kx
5 kx if( ! defined $hardware or $hardware eq "" ) { usage; }
5 kx if( ! defined $poolname or $poolname eq "" ) { usage; }
5 kx if( ! defined $operation or $operation eq "" ) { usage; }
5 kx if( ! defined $name or $name eq "" ) { usage; }
5 kx if( $operation eq 'set' )
5 kx {
5 kx if( ! defined $value or $value eq "" ) { usage; }
5 kx }
5 kx
5 kx my $hash_fname;
5 kx if( defined $flavour and $flavour ne "" )
5 kx {
5 kx $hash_fname = _kxLab::build_system_tmpdir() . "/" . "." .
5 kx $hardware . "." . $flavour . "." . $poolname . ".transmitting-hash";
5 kx }
5 kx else
5 kx {
5 kx $hash_fname = _kxLab::build_system_tmpdir() . "/" . "." .
5 kx $hardware . "." . $poolname . ".transmitting-hash";
5 kx }
5 kx
5 kx if( $operation eq 'set' )
5 kx {
5 kx save_env( $hash_fname, $name, $value );
5 kx }
5 kx else
5 kx {
5 kx my $val = get_env( $hash_fname, $name );
5 kx if( defined $val and $val ne "" )
5 kx {
5 kx $value = $val;
5 kx }
5 kx }
5 kx
5 kx #
5 kx # ECHO according to '--value-only' option:
5 kx #
5 kx if( $value_only eq 'yes' ) { print $value ; }
5 kx else { print $name . "=" . $value ; }