#!/bin/sh
## The form of the command is
## GPL_Clean_Scipt <PLATFORM_NAME> <WIRELESS_NAME> <MODEL_NAME>
## Where
##     PLATFORM_NAME:   AR7161
##     WIRELESS_NAME:   AP94
##     MODEL_NAME: DIR-825EU_B1
###################################################################
#export GENERATING_GPL environment variable for apps/cgi/man,cgi/misc and cgi/lib Makefile use
#please see below description -->Return to /home/AthSDK to check make process
#cd $ATHSDK_PATH
#make clean || exit 1
#make clean will go to apps/cgi/main,apps/cgi/misc and apps/cgi/lib doing make clean
#if GENERATING_GPL=y and CONFIG_GPL_CGI=y(apps/.config) will do nothing, do not delete *.o,
#reserve *.o for third party GPL_public.o to link into an executable file(ssi)
export GENERATING_GPL=y

if [ "${1}" = "" -o "${3}" = "" ]; then
    echo "GPL_Clean_Scipt <PLATFORM> <WIRELESS> <MODEL_NAME>"
    echo "ex, <PLATFORM_NAME> = QCA9558"
    echo "ex, <WIRELESS_NAME> = driver"
    echo "ex, <MODEL_NAME> = DIR-855L"
    exit
fi

########Environment Setting#####################
ATHSDK_PATH=`(pwd)`/../
PLATFORM_PATH=${ATHSDK_PATH}/platform/$1
WIRELESS_PATH=${ATHSDK_PATH}/wireless/$2
WWW_PATH=${ATHSDK_PATH}/www/$3
APPS_PATH=${ATHSDK_PATH}/apps

########Save(Re-tar) RootFS#####################
cd $ATHSDK_PATH/rootfs
if [ "$1" = "QCA9558" ] && [ -d target ]; then
	PLATFORM_PATH=${ATHSDK_PATH}/platform/linux
	rm -rf $PLATFORM_PATH/rootfs_db12x
	rm -rf target/etc/appflow/*;
	find -name .svn -exec rm -rf {} ';'
	find -name .git -exec rm -rf {} ';'
	find -name .gitignore -exec rm -rf {} ';'
	tar -jcf $PLATFORM_PATH/rootfs_db120.tar.bz2 target || exit 1
else
	echo "!!! PLATFORM Error !!!"
        echo "!!! Only permit to run GPL_Clean_Script once !!!"
        exit 1;
fi

#=======================optional remove==================================
#Remove non-use Apps (compare apps\Makefile and apps\.config)
#rm -rf $APPS_PATH/freeradius-client-1.1.6/ openldap-2.4.11/ gmp-4.2.2/
#rm -rf $APPS_PATH/openswan-2.6.16/ openssl-0.9.8h/ openvpn-2.1_rc9/
#rm -rf $APPS_PATH/libpcap-0.9.8/ pcre-7.7/ snort-2.8.2.2/ ppp-2.4.4/ mldc/
#rm -rf $APPS_PATH/bridge-utils/ busybox-1.00-pre10/ bpalogin/
#rm -rf $APPS_PATH/accel-pptp-0.8.2/ pptpd-1.3.4/ dhcp-forwarder-0.7/ dhcpv6/ dnsmasq/
#rm -rf $APPS_PATH/dropbear/ hostapd/ inadyn/ iptables/ libupnp-1.6.3/
#rm -rf $APPS_PATH/mrd/ openagent-2.0/ pptp/ snmp/ uuid/ zebra/ zlib/

########Remove full-private Apps#################
cd  ${APPS_PATH}
rm -rf DCC/ doc/ fw_query/ timer/ ip_lookup/ tftpd/ tftpd_new/ wcn/ mbandwidth/
rm -rf Mail-On-Schedule/ tracegw/ miniupnpd-1.0-RC7/ oray/ owera/
rm -rf heartbeat/ miniupnpd-1.3/ wpa_supplicant/ header/ nat_cfg/ upnpd_ath/
rm -rf wps_ath/ gpio/ rc/
rm -rf Agent5.0/ Agent5.5/ openagent-2.0/ intellon/
rm -rf QCAcfg2nvram/ QAPTools/ sigmatool_source/

CONFIG_GPL_CGI_ENABLE=$(cat ${APPS_PATH}/.config | grep CONFIG_GPL_CGI | awk -F"="  '{print $2}')
if [ $CONFIG_GPL_CGI_ENABLE ]; then
	rm -rf cmds/ speedtest/ mcastd/
	cd  ${APPS_PATH}/libxmo
	find ./ -name "*.c" | xargs rm -rf
	find ./ -name "*.h" | xargs rm -rf

	cd  ${APPS_PATH}/cgi
	mkdir ../GPL_sourcecode
	chmod 777  -R ../GPL_sourcecode
	cp include/public.h ../GPL_sourcecode
	cp include/ssc.h ../GPL_sourcecode
	cp include/multi_user.h ../GPL_sourcecode
	find ./ -name "*.c" | xargs rm -rf
	find ./ -name "*.h" | xargs rm -rf
	cp ../GPL_sourcecode/public.h include
	cp ../GPL_sourcecode/ssc.h include
	cp ../GPL_sourcecode/multi_user.h include
	rm -rf ../GPL_sourcecode
else
	rm -rf  cmds/ libxmo/ speedtest/ mcastd/
	cd  ${APPS_PATH}/cgi
	mkdir ../GPL_sourcecode
	chmod 777  -R ../GPL_sourcecode
	cp include/multi_user.h ../GPL_sourcecode
	rm -rf *
	mkdir include
	cp ../GPL_sourcecode/multi_user.h include
	rm -rf ../GPL_sourcecode
fi

########Remove half-private Apps#################
cd  ${APPS_PATH}
rm -rf libplatform/*.c
rm -rf libplatform/bsp.h
rm -rf nvram/*.c
rm -rf sutil/*.c
rm -rf artblock/*.c
rm -rf linux_nvram/*.c

########Remove wireless driver################
rm -rf $WIRELESS_PATH

########Remove Platform driver################
########We have to release boot loader source code#########

cd $PLATFORM_PATH
rm -rf doc/  drivers/

########Remove useless file################
cd $ATHSDK_PATH
rm -rf www/model_list www/Test-Page/ doc/

########Remove WWW file################
cd $WWW_PATH
rm -rf *.txt nvram.default 

#Do not delete www/ by Eric 20090715
#Do not delete bsp.h by Ken 20090715
rm -rf config/ GPL_Doc/ Test-Page/ AutoRun_CD/ Sorenson-Admin-password_utility/

########Copy GPL Makefile to Normal Makefile################
cp $ATHSDK_PATH/GPL_Makefile $ATHSDK_PATH/Makefile || exit 1
cp $ATHSDK_PATH/platform/PlatformGPL_Makefile $ATHSDK_PATH/platform/Makefile || exit 1
cp $APPS_PATH/AppsGPL_Makefile $APPS_PATH/Makefile || exit 1
cp $APPS_PATH/AppsGPL_Makefile.vpn $APPS_PATH/Makefile.vpn || exit 1
########Return to /home/AthSDK to check make process########
cd $ATHSDK_PATH
make clean || exit 1
############## rm Makefiles of opensource which are automatically generated by .configure #####################
cd $ATHSDK_PATH/apps
rm -f libdaemon-0.14/Makefile
rm -f expat-2.0.1/Makefile
rm -f avahi-0.6.30/Makefile
rm -f iptables-1.4.5/Makefile
cd $ATHSDK_PATH
################################################################################################################
echo "Start to make GPL Source Code...Please Wait..."
if ! make > makelog.log 2>&1
then
	echo "!!!!!! make GPL  Fail !!!!!!"
	exit 1
fi

echo "Start to make boot loader...Please Wait..."
####  
#if ! make loader > makeloader.log 2>&1
#then
#	echo "!!!!!! make boot loader Fail !!!!!!"
#	exit 1
#fi

echo "Start to make clean...Please Wait"
if ! make clean > makeclean.log 2>&1
then
	 echo "!!!!!! make clean GPL Code Fail !!!!!!"
	 exit 1
fi

############## rm Makefiles of opensource which are automatically generated by .configure #####################
cd $ATHSDK_PATH/apps
rm -f libdaemon-0.14/Makefile
rm -f expat-2.0.1/Makefile
rm -f avahi-0.6.30/Makefile
rm -f iptables-1.4.5/Makefile
################################################################################################################

########Remove .svn folder ################
cd $ATHSDK_PATH
find -name .svn -exec rm -rf {} ';'
find -name .git -exec rm -rf {} ';'
find -name .gitignore -exec rm -rf {} ';'

########Chang apps/.config  "#CONFIG_GPL_SDK is not set"  to  "CONFIG_GPL_SDK=y" ######
cd $ATHSDK_PATH/apps
sed s/.*CONFIG_GPL_SDK.*/CONFIG_GPL_SDK=y/g .config > tmp.config
mv tmp.config .config
######################################################################################

echo "======================================================="
echo " Generate GPL Source Code and makelog.log Finished !!!"
echo " Please run Release_CD phase 2 !!! "
echo "======================================================="
echo "Rlease_CD <RELEASE_CD_PATH> <MODEL_NAME> <PLATFORM_NAME> 2 <GPL_VERSION_NAME>"
echo ""
echo "Attention !!!!!!!!!!!!!!!"
echo "###########################################################################################################"
echo "Naming Rule of GPL_VERSION_NAME"
echo "Model Name + HW version + \"_GPL\" + FW version + FW region"
echo ""
echo "*Model Name:"
echo "Model name without dash. DIR-615 will be DIR615"
echo ""
echo "*HW version:"
echo "D1 means H/W version D1, Dx means the GPL code can be used in any D type H/W version."
echo ""
echo "*FW version"
echo "If HW version is A1, FW version will start from 1xx (xx=00~99)"
echo "If HW version is B1, FW version will start from 2xx (xx=00~99)"
echo "......"
echo "If HW version is D1, FW version will start from 4xx"
echo ""
echo "*FW region"
echo "NA, EU..."
echo ""
echo "Here are 2 examples"
echo "* DIR-825, HW B1, FW DIR825B1_FW205NA"
echo "DIR825B1_GPL205NA"
echo "* DIR-600, HW B1/B2/B3, FW DIR600Bx_FW205, Bx means the FW can be used on multi HW."
echo "DIR600Bx_GPL205"
echo ""
echo ""
echo "###########################################################################################################"
