# Perl module for UCW Configure Scripts # # (c) 2005--2010 Martin Mares # # This software may be freely distributed and used according to the terms # of the GNU Lesser General Public License. package UCW::Configure; use strict; use warnings; use IO::File; BEGIN { # The somewhat hairy Perl export mechanism use Exporter(); our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); $VERSION = 1.0; @ISA = qw(Exporter); @EXPORT = qw(&Init &Log &Notice &Warn &Fail &IsSet &IsGiven &Set &UnSet &Append &Override &Get &Test &TestBool &Include &Finish &FindFile &TryFindFile &DebugDump &PostConfig &AtWrite); @EXPORT_OK = qw(); %EXPORT_TAGS = (); } our %vars; our %overriden; our @postconfigs; our @atwrites; sub DebugDump() { print "VARS:\n"; print "$_: $vars{$_}\n" foreach( keys %vars ); } sub Log($) { print @_; STDOUT->flush; } sub Notice($) { print @_ if $vars{"VERBOSE"}; STDOUT->flush; } sub Warn($) { print "WARNING: ", @_; STDOUT->flush; } sub Fail($) { Log("ERROR: " . (shift @_) . "\n"); exit 1; } sub IsSet($) { my ($x) = @_; return exists $vars{$x}; } sub IsGiven($) { my ($x) = @_; return exists $overriden{$x}; } sub Get($) { my ($x) = @_; return $vars{$x}; } sub Set($;$) { my ($x,$y) = @_; $y=1 unless defined $y; $vars{$x}=$y unless $overriden{$x}; } sub UnSet($) { my ($x) = @_; delete $vars{$x} unless $overriden{$x}; } sub Append($$) { my ($x,$y) = @_; Set($x, (IsSet($x) ? (Get($x) . " $y") : $y)); } sub Override($;$) { my ($x,$y) = @_; $y=1 unless defined $y; $vars{$x}=$y; $overriden{$x} = 1; } sub Test($$$) { my ($var,$msg,$sub) = @_; Log "$msg ... "; if (IsSet($var)) { Log Get($var) . " (preset)\n"; } else { my $val = &$sub(); Set($var, $val); Log "$val\n"; } } sub TestBool($$$) { my ($var,$msg,$sub) = @_; Log "$msg ... "; if (IsSet($var) || IsGiven($var)) { Log ((Get($var) ? "yes" : "no") . " (set)\n"); } else { my ($val, $comment) = &$sub(); if ($val) { Set($var); } else { UnSet($var); } Log (($val ? "yes" : "no") . "\n"); } } sub TryFindFile($) { my ($f) = @_; if ($f =~ m{^/}) { return (-f $f) ? $f : undef; } elsif (-f $f) { return "./$f"; } elsif (-f (Get("SRCDIR")."/$f")) { return Get("SRCDIR")."/$f"; } else { return undef; } } sub FindFile($) { my ($f) = @_; my $F; defined ($F = TryFindFile($f)) or Fail "Cannot find file $f"; return $F; } sub Init($$) { my ($srcdir,$defconfig) = @_; sub usage($) { my ($dc) = @_; print STDERR "Usage: [/]configure " . (defined $dc ? "[" : "") . "" . (defined $dc ? "]" : "") . " [