#!/usr/bin/perl

use strict;
package main;

use File::stat;
use File::Glob;
use File::Copy;
use File::Basename;
use File::Path;
use Cwd;

use constant DEBUG => 0;

my $isWindows = ( $^O eq "MSWin32" || $^O eq "cygwin" );

# Dummy variables (used in common.pm)
my $opt_verbose = 0;
my $shadow = 0;
my $outpath = $ENV{QPEDIR};

# Find the depot path
my $depotpath = undef;
open IN, "$outpath/src/config.pri" or die "Cannot open $outpath/src/config.pri\n";
while ( defined($_ = <IN>) ) {
    if ( /QTOPIA_DEPOT_PATH=(.*)/ ) {
	$depotpath = $1;
	last;
    }
}
close IN;
if ( !defined($depotpath) ) {
    die "Can't find the depot path in $outpath/src/config.pri\n";
}

# Load some subs
open IN, "$depotpath/bin/common.pm" or die "Cannot open $depotpath/bin/common.pm\n";
eval join("", <IN>) or die "Error while evaluating common.pm: $!\n";
close IN;

# Windows depot builds use the perl scripts directly rather than the compiled code
if ( $isWindows ) {
    check_script("$0", "$depotpath/bin", $ARGV[0]);
}


my $QPEDIR = shift or usage();

my $features = "$QPEDIR/include/qtopia/qpe_features.h";
my $origFile = undef;
if ( open IN, $features ) {
    $origFile = join("", <IN>);
    close IN;
}

my $newFile = "";
for $_ ( @ARGV ) {
    if ( /(.*)=(.*)/ ) {
        $newFile .= "#define $1 $2\n";
    } else {
        $newFile .= "#define $_\n";
    }
}

DEBUG and print "New file:\n$newFile";
DEBUG and print "Orig file:\n$origFile";

if ( !defined($origFile) || $newFile ne $origFile ) {
    open OUT, ">$features";
    print OUT $newFile;
    close OUT;
}

exit 0;


sub usage
{
    print "Usage:  ".script_name($0)." QPEDIR [feature] [feature=blah] ...\n";
    exit 2;
}

