#!/usr/bin/env python
# -*- coding:utf-8 -*-

import sys
import os

import mtdpart as mp

#top_config_file = root_dir + "/.config"
top_config_file =  "./.config"
top_config = open(top_config_file)

for line in top_config:
    if "CONFIG_PARTITION_INTREE" in line:
        if line[0] != "#":
            print "in uboot"
            os.system("rm -f ./board/realtek/rlxboard/partition.ini")
            for line in top_config:
                if "PARTITION_NOR_16MB" in line:
                    if line[0] != "#":
                        print "in nor"
                        os.system("ln -s -r ./board/realtek/rlxboard/partition_nor_16MB.ini ./board/realtek/rlxboard/partition.ini")
                elif "PARTITION_NAND_128MB" in line:
                    if line[0] != "#":
                        print "in nand"
                        os.system("ln -s -r ./board/realtek/rlxboard/partition_nand_128MB.ini ./board/realtek/rlxboard/partition.ini")
                elif "PARTITION_RAMFS" in line:
                    if line[0] != "#":
                        print "in ramfs"
                        os.system("ln -s -r ./board/realtek/rlxboard/partition_ramfs.ini ./board/realtek/rlxboard/partition.ini")
                elif "PARTITION_EMMC" in line:
                    if line[0] != "#":
                        print "in emmc"
                        os.system("ln -s -r ./board/realtek/rlxboard/partition_emmc.ini ./board/realtek/rlxboard/partition.ini")
                elif "PARTITION_NOR_8MB" in line:
                    if line[0] != "#":
                        print "in nor"
                        os.system("ln -s -r ./board/realtek/rlxboard/partition_nor_8MB.ini ./board/realtek/rlxboard/partition.ini")
    elif "CONFIG_PARTITION_CUSTOM" in line:
        if line[0] != "#":
            print "custom partition"
            os.system("rm -f ./board/realtek/rlxboard/partition.ini")
            for line in top_config:
                if "CONFIG_PARTITION_CUSTOM_PATH" in line:
                    if line[0] != "#":
                        partition_path = line.split("=")[1]
                        partition_path = partition_path.strip()
                        print "custom partition parth: %s" % partition_path
                        cmd = "ln -s -r %s ./board/realtek/rlxboard/partition.ini"  % partition_path
                        os.system(cmd)

    partition = "./board/realtek/rlxboard/partition.ini"

top_config.close()

if __name__ == "__main__":
#    partition = root_dir + "/target/bsp/partition.ini"
    mode = "null"
    baudrate = "null"
    flash = "null"
    secure = False
    user_config_file = "include/generated/autoconf.h"
    user_config = open(user_config_file)
    for line in user_config:
        if "CONFIG_RTS_NOR_BOOT" in line:
            flash = "nor"
        elif "CONFIG_CMD_NAND" in line:
            flash = "nand"
        elif "CONFIG_RTS_EMMC_BOOT" in line:
            flash = "emmc"
    user_config.seek(0)
    for line in user_config:
        if "CONFIG_FIT_SIGNATURE" in line:
            secure = True
    user_config.seek(0)
    for line in user_config:
        if "CONFIG_QUAD_CHANNEL" in line:
            mode = "quad"
        elif "CONFIG_DUAL_CHANNEL" in line:
            mode = "dual"
        elif "CONFIG_QPI_MODE" in line:
            mode = "qpi"
    user_config.seek(0)
    for line in user_config:
        if "CONFIG_NAND_QUAD_CHANNEL" in line:
            mode = "quad"
        elif "CONFIG_NAND_DUAL_CHANNEL" in line:
            mode = "dual"
    user_config.seek(0)
    for line in user_config:
        if "CONFIG_BAUDRATE_57600" in line:
            baudrate = "57600"
        elif "CONFIG_BAUDRATE_115200" in line:
            baudrate = "115200"
    user_config.close()
    if mode == "null":
        #set quad channel as default mode
        mode = "quad"
        print "can't find channel info, use %s instead" % (mode)

    mapping_config = "include/generated/mtd_mapping_config.h"
    ps = mp.Parts(partition, mode, baudrate, flash, secure)
    ps.write_mapping_config(mapping_config)
