#!/bin/sh

if [ $# -lt 3 ] || [ $# -gt 5 ]; then
	echo arg num is $#, not 3 or 4 or 5.
	exit
fi  

if [ -e ../../build_info ]; then
	cd ../../
	# ./build_info
	cd -
else
	echo "no build_info!!!"
fi

if [ -e Makefile ]; then
	cp Makefile ../../Makefile
else
	echo "no Makefile!!!"
fi

cd ../..
make clean

if [ -e ../../app_public/fuxi_public ]; then
	cd ../../app_public/fuxi_public
	export FUXI_PUB_PWD=`pwd`
	cd - > /dev/null
else
	echo "no ../../app_public/fuxi_public directory!!!"
	exit
fi

if [ -e ../../app_public/fuxi_public/fuxi_bsp ]; then
	cd ../../app_public/fuxi_public/fuxi_bsp
	export FUXI_BSP_PWD=`pwd`
	cd - > /dev/null
else
	echo "no ../../app_public/fuxi_public/fuxi_bsp directory!!!"
	exit
fi

if [ -e ../../app_public/fuxi_public/bsp_share ]; then
	cd ../../app_public/fuxi_public/bsp_share
	export BSP_SHARE_PWD=`pwd`
	cd - > /dev/null
else
	echo "no ../../app_public/fuxi_public/bsp_share directory!!!"
	exit
fi

if [ -e ../../app_public/fuxi_public/rt_lite ]; then
	cd ../../app_public/fuxi_public/rt_lite
	export FUXI_RT_LITE_PWD=`pwd`
	cd - > /dev/null
else
	echo "no ../../app_public/fuxi_public/rt_lite directory!!!"
	exit
fi

if [ -e ../../app_public ]; then
	cd ../../app_public
	export APP_PUB_PWD=`pwd`
	cd - > /dev/null
else
	echo "no ../app_public directory!!!"
	exit
fi

export PRJ_PWD=`pwd`
echo $PRJ_PWD
echo $FUXI_PUB_PWD
echo $FUXI_BSP_PWD
echo $BSP_SHARE_PWD
echo $APP_PUB_PWD
echo $FUXI_RT_LITE_PWD

app_name=dtu_t536
export app_name
app_dir=$1

mkdir -p tmp
cp -arf ./app/ ./tmp/
cp -arf $FUXI_BSP_PWD ./tmp/
cp -arf $BSP_SHARE_PWD ./tmp/
cp -arf $APP_PUB_PWD ./tmp/
cp -arf $FUXI_RT_LITE_PWD ./tmp/

if [ -e ./ko/$app_dir/ ]; then
   rm -rf ko/"$app_dir"/*.*
else
   mkdir ./ko/$app_dir
fi

if [ $# -eq 2 -o $# -eq 3 ]; then
	if [ -e $PRJ_PWD/app/lib61850.c ]; then
		cp -arf $PRJ_PWD/app/lib61850.c ./tmp/
	fi
elif [ $# -eq 4 ]; then
	if [ -e $FUXI_PUB_PWD/libs/lib61850.a ]; then
		cp -f $FUXI_PUB_PWD/libs/lib61850.a ./tmp/lib61850.o
	fi
	export FUCTIONS_EXT=-$4
#elif [ $# -eq 5 ]; then
#	if [ -e $FBS_PUB_PWD/libs/lib61850.a ]; then
#		cp -f $FBS_PUB_PWD/libs/lib61850.a ./tmp/lib61850.o
#	fi
#	export FUCTIONS_EXT="-$4 -$5"
fi

if [ -e $FUXI_PUB_PWD/libs/soft-fp.a ]; then
	cp -f $FUXI_PUB_PWD/libs/soft-fp.a ./tmp/soft-fp.o
fi

if [ -e $FUXI_PUB_PWD/libs/encrypt_eccsm.a ]; then
	cp -f $FUXI_PUB_PWD/libs/encrypt_eccsm.a ./tmp/encrypt_eccsm.o
fi

if [ -e $FUXI_PUB_PWD/libs/rt_lite.a ]; then
	cp -f $FUXI_PUB_PWD/libs/rt_lite.a ./tmp/rt_lite.o
fi

export GD_AREA=-$2
export CPU_TYPE=-$3
export MACHINE_TYPE=-$4

export DEFINES=$@
echo $DEFINES
echo begin build...
UPTIME1=`cat /proc/uptime`
#CPU="fuxi"

# /tmp/bear/bin/bear -- make -j6 v=1
make -j6

COMPILE_STATUS=$?

echo "$UPTIME1"
cat /proc/uptime

rm Makefile

if [ $COMPILE_STATUS != 0 ]; then
	#echo "!!!!!Compile App Failure!!!!!" >./ko/$app_dir/error.ERR
	#read -p "Press any key to continue." var
	exit 1
fi

echo "make $app_name successful......"
# CURRENT_DIR=$(cd $(dirname $0); pwd)
# echo $CURRENT_DIR
./crc_host -c "$app_name"

if [ $? -ne 0 ]; then
  	rm -rf ./ko/$app_dir/*.*
	./crc_host -a "$app_name"
	
	#校验CRC
	./crc_host -c "$app_name" > /run/shm/crc
	filename=`sed 's/\s\+$//g' /run/shm/crc`
  	echo > ./ko/$app_dir/$filename.crc
  
  #程序生成时间
  date  +"%Y-%m-%d %H.%M.%S"  > /run/shm/crc
  filename2=`sed 's/ / /g' /run/shm/crc`
  echo > ./ko/$app_dir/$filename2.date
  
  #
  cp "$app_name" ko/"$app_dir"/
  cp "$app_name" out/
  cp "$app_name" /mnt/hgfs/embedsky_share
fi


#ip="192.168.1.208"

# putftp.sh $app_name $ip
# echo putftp.sh $app_name $ip

