#!/usr/bin/perl
use strict;
use warnings;
use FindBin qw($Bin);
use lib "$Bin/../../PerlLib";
use BRCM::GenConfig;

# $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 $p    = new BRCM::GenConfig(shift);
my $chip = $p->get('BRCM_CHIP');
my $c    = new BRCM::GenConfig( shift, Chip => $chip, Profile => $p );

$c->set( "CONFIG_MTD_BLOCK_RO",                     'y' );
$c->set( "CONFIG_MTD_ROM",                          'y' );
$c->set( "CONFIG_MTD_COMPLEX_MAPPINGS",             'y' );
$c->set( "CONFIG_MTD_BCM963XX",                     'y' );
$c->set( "CONFIG_SQUASHFS",                         'y' );
$c->set( "CONFIG_SQUASHFS_EMBEDDED",                'y' );
$c->set( "CONFIG_LZMA_FS_INFLATE",                  'y' );
$c->set( "CONFIG_SQUASHFS_FRAGMENT_CACHE_SIZE",     $p->get("BRCM_SQUASHFS_FRAGMENT_CACHE_SIZE") );

$c->set( "CONFIG_ROOT_FLASHFS", '"ro noinitrd"' );

if ( $p->get("BUILD_MODSW_EE") ) {
    $c->set( "CONFIG_OVERLAY_FS", 'y' );
}

if ( $p->get("CONFIG_OVERLAY_FS") ) {
    $c->set( "CONFIG_BCM_OVERLAYFS_BACKPORTS", 'y');
    $c->set( "CONFIG_BCM_JFFS2_OVERLAY", 'y');
    $c->set( "CONFIG_BCM_UBIFS_OVERLAY_BACKPORTS", 'y');
}

if ( $p->get("BRCM_KERNEL_ROOTFS") eq 'all' ) {

    $c->set( "CONFIG_MTD_CHAR",                         'y' );
    $c->set( "CONFIG_MTD_BLOCK",                        'y' );
    $c->set( "CONFIG_MTD_PARTITIONS",                   'y' );
    $c->set( "CONFIG_MTD_UBI",                          'y' );
    $c->set( "CONFIG_MTD_UBI_GLUEBI",                   'y' );
    $c->set( "CONFIG_MTD_UBI_BLOCK",                    'y' );

    $c->set( "CONFIG_JFFS2_FS",                         'y' );
    $c->set( "CONFIG_JFFS2_FS_XATTR",                   'y' );
    $c->set( "CONFIG_JFFS2_FS_WRITEBUFFER",             'y' );
    $c->set( "CONFIG_JFFS2_FS_DEBUG",                   "0" );
    $c->set( "CONFIG_JFFS2_SUMMARY",                    'y' );

    $c->set( "CONFIG_UBIFS_FS",                         'y' );
    $c->set( "CONFIG_UBIFS_FS_ZLIB",                    'y' );

    if ( $p->get("BUILD_STANDARD_NAND_DRIVER") ) {
        $c->set("CONFIG_MTD_NAND_BRCMNAND",'y');
    }

    if ( $p->get("BUILD_LEGACY_NAND_DRIVER") ) {
        $c->set("CONFIG_MTD_BRCMNAND",'y');

        if ( $chip =~ /^(63268|6838)$/ ) {
            $c->set( "CONFIG_BRCMNAND_MTD_EXTENSION",     "y" );
            $c->set( "CONFIG_BRCMNAND_MAJOR_VERS", "4" );
            $c->set( "CONFIG_BRCMNAND_MINOR_VERS", "0" );
        }
        elsif ( $chip =~ /^(63138|63381)$/ ) {
            $c->set( "CONFIG_BRCMNAND_MTD_EXTENSION",     "y" );
            $c->set( "CONFIG_BRCMNAND_MAJOR_VERS", "7" );
            $c->set( "CONFIG_BRCMNAND_MINOR_VERS", "0" );
        }
        elsif ( $chip =~ /^(63148|6848|6858|4908|6836|63158|6846|47622|63178|6856|6878|6855)$/ ) {
            $c->set( "CONFIG_BRCMNAND_MTD_EXTENSION",     "y" );
            $c->set( "CONFIG_BRCMNAND_MAJOR_VERS", "7" );
            $c->set( "CONFIG_BRCMNAND_MINOR_VERS", "1" );
        }
    }

    $c->set( "CONFIG_MTD_NAND",'y' );
    
    if ( $chip =~ /^(6838|6848|63381|63138|6858|4908|6836|63158|6846|6856|47622|63178|6878|6855)$/ ) {
        $c->set( "CONFIG_MTD_BCM_SPI_NAND",'y' );
    }
}

if ( $p->get("BRCM_KERNEL_AUXFS_JFFS2") ) {
    $c->set( "CONFIG_MTD_CHAR",                         'y' );
    $c->set( "CONFIG_MTD_BLOCK",                        'y' );
    $c->set( "CONFIG_JFFS2_FS",                         'y' );
    $c->set( "CONFIG_JFFS2_FS_WRITEBUFFER",             'y' );
    $c->set( "CONFIG_JFFS2_FS_DEBUG",                   "0" );
    $c->set( "CONFIG_AUXFS_JFFS2",                      'y' );
}

if ( $p->get("BRCM_RAMDISK_BOOT_EN") ) {

    $c->set( "CONFIG_BLK_DEV_INITRD",                   'y' );
    $c->set( "CONFIG_RD_GZIP",                          'y' );
    $c->set( "CONFIG_DECOMPRESS_GZIP",                  'y' );
    $c->set( "CONFIG_BLK_DEV_LOOP",                     'y' );
    $c->set( "CONFIG_BLK_DEV_RAM",                      'y' );
    $c->set( "CONFIG_AUTOFS_FS",                        'y' );
    $c->set( "CONFIG_AUTOFS4_FS",                       'y' );

    $c->unset( "CONFIG_RD_BZIP2" );
    $c->unset( "CONFIG_RD_LZMA" );
    $c->unset( "CONFIG_RD_XZ" );
    $c->unset( "CONFIG_RD_LZO" );
    $c->unset( "CONFIG_PROBE_INITRD_HEADER" );
    $c->unset( "CONFIG_BLK_DEV_CRYPTOLOOP" );
    $c->unset( "CONFIG_BLK_DEV_XIP" );

    $c->set( "CONFIG_INITRAMFS_SOURCE", '""' );
    $c->set( "CONFIG_BLK_DEV_LOOP_MIN_COUNT", "4");
    $c->set( "CONFIG_BLK_DEV_RAM_COUNT", "1");
    $c->set( "CONFIG_BLK_DEV_RAM_SIZE", $p->get("BRCM_RAMDISK_SIZE") );

}

if ( $p->get("BRCM_NFS_MOUNT_EN") ) {
    $c->set( "CONFIG_NETWORK_FILESYSTEMS",              'y' );
    $c->set( "CONFIG_NFS_FS",                           'y' );
    $c->set( "CONFIG_NFS_V3",                           'y' );
    $c->set( "CONFIG_NFS_COMMON",                       'y' );
}

if ( $p->get('DISABLE_NOR_RAW_PARTITION') ) {
    $c->set( 'CONFIG_BCM_DISABLE_NOR_RAW_PARTITION', 'y' );
}

$c->write();

