/*
 * drivers/net/wireless/rtl8192cu/make_drv
 *
 * (C) Copyright 2007-2012
 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#!/bin/bash
#========================================
# Common functions
#========================================
intf_usb()
{
	sed -i '/CONFIG_USB_HCI = /c\CONFIG_USB_HCI = y' Makefile
	sed -i '/CONFIG_PCI_HCI = /c\CONFIG_PCI_HCI = n' Makefile
	sed -i '/CONFIG_SDIO_HCI = /c\CONFIG_SDIO_HCI = n' Makefile
}
intf_pcie()
{
	sed -i '/CONFIG_USB_HCI = /c\CONFIG_USB_HCI = n' Makefile
	sed -i '/CONFIG_PCI_HCI = /c\CONFIG_PCI_HCI = y' Makefile
	sed -i '/CONFIG_SDIO_HCI = /c\CONFIG_SDIO_HCI = n' Makefile
}
intf_sdio()
{
	sed -i '/CONFIG_USB_HCI = /c\CONFIG_USB_HCI = n' Makefile
	sed -i '/CONFIG_PCI_HCI = /c\CONFIG_PCI_HCI = n' Makefile
	sed -i '/CONFIG_SDIO_HCI = /c\CONFIG_SDIO_HCI = y' Makefile
}

nic_8192c()
{
	sed -i '/CONFIG_RTL8192C = /c\CONFIG_RTL8192C = y' Makefile
	sed -i '/CONFIG_RTL8192D = /c\CONFIG_RTL8192D = n' Makefile
}
nic_8192d()
{
	sed -i '/CONFIG_RTL8192C = /c\CONFIG_RTL8192C = n' Makefile
	sed -i '/CONFIG_RTL8192D = /c\CONFIG_RTL8192D = y' Makefile
}

ch_obj_cu()
{
        sed -i '/obj-$(CONFIG_RTL/ c obj-$(CONFIG_RTL8192CU) := $(MODULE_NAME).o' Makefile
        sed -i '/export CONFIG_RTL/ c export CONFIG_RTL8192CU = m' Makefile
}

ch_obj_ce()
{
        sed -i '/obj-$(CONFIG_RTL/ c obj-$(CONFIG_RTL8192CE) := $(MODULE_NAME).o' Makefile
        sed -i '/export CONFIG_RTL/ c export CONFIG_RTL8192CE = m' Makefile
}

ch_obj_du()
{
        sed -i '/obj-$(CONFIG_RTL/ c obj-$(CONFIG_RTL8192DU) := $(MODULE_NAME).o' Makefile
        sed -i '/export CONFIG_RTL/ c export CONFIG_RTL8192DU = m' Makefile
}

ch_obj_de()
{
        sed -i '/obj-$(CONFIG_RTL/ c obj-$(CONFIG_RTL8192DE) := $(MODULE_NAME).o' Makefile
        sed -i '/export CONFIG_RTL/ c export CONFIG_RTL8192DE = m' Makefile
}

get_svn_revision()
{
	if [ -z "$svn_rev" ] && [ -f .svn/entries ]; then
		svn_rev=`sed -n '11 s/^\([0-9][0-9]*\)$/\1/p' .svn/entries`
	fi
	if [ -z "$svn_rev" ]; then
		svn_rev="xxxx"
	fi
}

#========================================
# Sub functions
#========================================

add_version_file()
{
	defversion="#define DRIVERVERSION\t\"$postfix\""
	echo -e $defversion > include/rtw_version.h
}

conf_driver()
{
	sed -i '/CONFIG_AUTOCFG_CP = /c\CONFIG_AUTOCFG_CP = y' Makefile
	if [ -f include/rtw_version.h ]; then	
            echo "rtw_version.h has existed!"
	else
	add_version_file
	fi
}

#========================================
# Main Script Start From Here
#========================================


if [ ! -f include/rtw_version.h ]; then	
	# Make Version string
	if [ -z "$2" ]; then
		get_svn_revision
		version="v3.3.0_"$svn_rev
		datestr=$(date +%Y%m%d)
		postfix=$version.$datestr
	else
		postfix=$2
	fi
fi


if [ $# -eq 1 ]; then
	card=$1
	echo "You have selected $card"
else
	# Select NIC type
	echo "Please select card type(1/2):"
	select card in RTL8192cu RTL8192du;
	do
		echo "You have selected $card"
		break
	done
fi

# Make
case "$card" in
	[Rr][Tt][Ll][8][1][9][2][Cc][Uu])
	conf_driver;
	nic_8192c;
	intf_usb;
	ch_obj_cu;;
	[Rr][Tt][Ll][8][1][9][2][Cc][Ee])
	conf_driver;
	nic_8192c;
	intf_pcie;
	ch_obj_ce;;
	[Rr][Tt][Ll][8][1][9][2][Dd][Uu])
	conf_driver;
	nic_8192d;
	intf_usb;
	ch_obj_du;;
	[Rr][Tt][Ll][8][1][9][2][Dd][Ee])
	conf_driver;
	nic_8192d;
	intf_pcie;
	ch_obj_de;;
	*)
	echo "Unknown NIC type"
	;;
esac
