#!/usr/bin/perl
use strict;
use warnings;
use FindBin qw($Bin);
my $perlib_dir = "$Bin/../../PerlLib";
use lib "$Bin/../../PerlLib";
use BRCM::GenConfig;
use File::Basename;
use File::Spec::Functions;
use File::Copy;

# $p will allow us to GET values from the PROFILE
# $c will allow us to SET (and GET and DRIVER_SETUP) on the config
# file as we transform it from a template to the final config

# arguments 
# * profile file
# * config  file
my $profiledirstr = shift;
my $p    = new BRCM::GenConfig($profiledirstr);
my $chip = $p->get('BRCM_CHIP');
my $c    = new BRCM::GenConfig( shift, Chip => $chip, Profile => $p );
my $profiledir=dirname($profiledirstr);
my $builddir=dirname($profiledir);
$builddir=dirname($builddir);
my $wlrouterdir;


if ( $p->get("WLCSM_DEBUG_TRACE") ) {
    $c->set( "CONFIG_BCM_WLCSM_DEBUG", "y" );
}

$c->driver_setup( "WIRELESS", "WLAN" );
if ( $p->get("BRCM_DRIVER_WIRELESS") ) {
    my $wlan_impl;
    $c->set( "CONFIG_HOTPLUG", "y" );
    $c->set( "CONFIG_SYSFS",   "y" );
    if ( $p->get("BRCM_WAPI") ) {
        $c->set( "CONFIG_BCM_WAPI", "y" );
    }

    if ( $p->get("BRCM_WLALTBLD") =~ /\w/ ) {
        $c->set( "CONFIG_BCM_WLALTBLD", $p->get("BRCM_WLALTBLD") );
    }
    else {
        # profile didn't spcify, so uncomment the temokate value
        $c->set( "CONFIG_BCM_WLALTBLD", $c->get("# CONFIG_BCM_WLALTBLD") );
    }
    if ( my $i = $p->get("BRCM_WLBRAND") ) {
        $c->set( "CONFIG_BCM_$i", "y" );
    }
    if ( $wlan_impl = $p->get("BCM_WLIMPL") ) {
           $c->set( "CONFIG_BCM_WLAN_IMPL", $wlan_impl );
    } else {
        if ( my $wlan_env_impl = $ENV{"BCM_WLIMPL"} ) { 
            $c->set( "CONFIG_BCM_WLAN_IMPL", $wlan_env_impl );
            $wlan_impl=$wlan_env_impl;
        } else {
            if( $wlan_impl = $c->get('# CONFIG_BCM9'.$chip.'_WLAN_IMPL') ) {
                $c->set( "CONFIG_BCM_WLAN_IMPL", $wlan_impl );
            } else {
                print("WHY WLIMPL DEFAULT IS NOT SET??? SETTO 24\n");
            }
        }
    }

    if ( $p->get("BUILD_BRCM_HNDROUTER") || $p->get("BUILD_BRCM_CPEROUTER") ) {
        if ( $p->get("BUILD_BRCM_HOSTAPD") ) {
            $c->set( "CONFIG_WIRELESS", "y");
            $c->set( "CONFIG_CFG80211", "m");
            $c->set( "CONFIG_NL80211_TESTMODE", "y");
            $c->set( "CONFIG_CFG80211_DEFAULT_PS", "y");
        }
    }

    if ( $p->get("BUILD_BCM_WLAN_DPDCTL") && $p->get("BUILD_BRCM_CPEROUTER") ) {
        $c->set( "CONFIG_BCM_WLAN_DPDCTL", "y");
    }

    print("wlan impl:.$wlan_impl\n\n");
    if( my $hndbuild = $c->get("CONFIG_BCM_HNDROUTER")) {
        print("HND build, no need to change .config\n");
    } else {
        my $dot_configfile;
        my $configfile;
        my $cperouter_conf;
        my $wl_builddir=catfile($builddir,'bcmdrivers',catfile('broadcom','net',"wl"));
        my $impldir=catfile($wl_builddir,'impl'.$wlan_impl,);
        my $to_init_conf=0;
        $wlrouterdir =catfile($impldir,catfile('main','src','router'));
        unless ( -e $wlrouterdir and -d $wlrouterdir ) {
            # for Hudu release, router dir is under components 
            $wlrouterdir =catfile($impldir,catfile('main','components','router'));
        }
        my $wlconf_adjust=catfile($wl_builddir,'wlconf_adjust.pl');
        my $cpeconfig_init=catfile($wl_builddir,'cperouter_init_config.pl');
        $dot_configfile = catfile($wlrouterdir,'.config');
        #If configuration file does not exist, it means first time run
        #Here,before Kernel and User APPS,build,set up configurations.
        if ( $p->get("BUILD_BRCM_CPEROUTER") ) {
            $c->set( "CONFIG_BCM_CPEROUTER", 'y');
            $c->set( "CONFIG_BCM_HNDROUTER", 'y');
            $configfile = catfile($wlrouterdir,catfile('config','defconfig-4.1-vista-dhdap-router'));
            $cperouter_conf =  catfile($wlrouterdir,catfile('config','defconfig-4.1-vista-dhdap-bca-cperouter'));
            unless ( -f  $cperouter_conf)  {
                copy($configfile,$cperouter_conf);
                $to_init_conf=1;
            }
        } else {
            $cperouter_conf = catfile($wlrouterdir,catfile('config','defconfig-2.6-vista-dhdap-router-atlas'));
            unless ( -f $dot_configfile ) {
                $to_init_conf=1;
            }
        }
        if( $to_init_conf == 1) {
            if ( my $i = $p->get("BUILD_WLVISUALIZATION") ) {
                $c->set( "CONFIG_BCM_WLAN_WLVISUALIZATION", $i );
            } else {
                $c->set( "CONFIG_BCM_WLAN_WLVISUALIZATION", "n");
            }
            if ( my $i = $p->get("BUILD_WBD") ) {
                $c->set( "CONFIG_BCM_WLAN_WBD", $i );
            } else {
                $c->set( "CONFIG_BCM_WLAN_WBD", "n");
            }
            system($cpeconfig_init,$builddir,$wl_builddir,$wlrouterdir,$cperouter_conf,$perlib_dir,$profiledirstr);
        }
    }

    if ( $p->get("BUILD_CUSTOMER") && ($p->get("BUILD_CUSTOMER_NAME") eq "\"ctc\"")) {
        $c->set( "CONFIG_BCM_WLCXC", "m");
        $c->set( "CONFIG_BCM_WLCXC_IMPL", "1");
    }
}

$c->write( );

