#!/usr/bin/perl

###############################################################################
###############################################################################
##
##  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
##  Copyright (C) 2004 Red Hat, Inc.  All rights reserved.
##  
##  This copyrighted material is made available to anyone wishing to use,
##  modify, copy, or redistribute it subject to the terms and conditions
##  of the GNU General Public License v.2.
##
###############################################################################
###############################################################################

##
## This script will pull out #define's and convert them into variables that
## can be used for shell or perl scripts.
##
## It takes 2 or 3 parameters.  The first parameter is the filename to search
## in.  The second paramter is the desired output format.  Supported types
## ar sh and perl.  An optional third parameter may be given.  If specified,
## only that partiucular #define will be extracted, otherwise, all #defines
## are extracted.   This script is not capable of handling condiitional rules.
## 

$usage = "usage: define2var <filename> <perl|sh> [define]";

if (@ARGV != 2 && @ARGV != 3)
{
	die "$usage\n";	
}

my ($filename, $type, $define ) = @ARGV;
my $pfx,$sfx;

if ($type eq "sh") 
{ 
	($pfx,$sfx) = ("",""); i
} 
elsif ($type eq "perl" ) 
{ 
	($pfx,$sfx) = ("\$",";"); 
}
else 
{ 
	die "Unknown type: $type\n"; 
}

open FILE, "< $filename" or die "error opening $filename: $!\n";

while (<FILE>)
{
	chomp;
	if ($_ =~ /^#define\s+(\S+)\s+(.*)/)
	{
		if(! $define ) 
		{
			print "$pfx$1=$2$sfx\n";
		}
		elsif ($define eq $1 )
		{
			print "$pfx$1=$2$sfx\n";
			exit 0;
		}
	}
}

die "\"$define\" not found\n" if ($define);

exit 0;
