#/bin/sh

echo $PWD
path_to_avs=$PWD
path_to_avs=$(dirname $path_to_avs)
echo $path_to_avs

qtpath="/opt/qt-commercial-clean/5.13.1/Automotive/intel-corei7-64"
device_connection="root@192.168.3.138"

use_wakeup_word=true
build_libcurl=true
deploy_curl_to_device=true
build_avs_sdk=false
copy_sdk_to_toolchain=false
copy_sdk_to_device=false

build_alexa_package=false
deploy_alexa_package=false

source $qtpath/toolchain/environment-setup-corei7-64-poky-linux

#fix curl
if $build_libcurl; then
	echo "===========>>>>>>>> libcURL"
	cd $path_to_avs/boot2qt/
	wget https://github.com/nghttp2/nghttp2/releases/download/v1.39.2/nghttp2-1.39.2.tar.bz2
	tar -xvf nghttp2-1.39.2.tar.bz2
	cd nghttp2-1.39.2
	./configure --host=x86_64-none-linux --target=x86_64-none-linux --enable-shared --prefix=/usr 
#LDFLAGS="-L/opt/qt-commercial-clean/5.13.1/Automotive/intel-corei7-64/toolchain/sysroots/corei7-64-poky-linux/usr/lib/"
	make -j8
	make DESTDIR=$path_to_avs/boot2qt/build-nghttp2 install
	cp $path_to_avs/boot2qt/build-nghttp2/usr/* $qtpath/toolchain/sysroots/corei7-64-poky-linux/usr/ -r
	
	cd $path_to_avs/boot2qt/
	wget http://curl.haxx.se/download/curl-7.54.0.tar.bz2
	tar -xvjf curl-7.54.0.tar.bz2
	cd curl-7.54.0
	./configure --with-nghttp2=/usr --with-ssl --host=x86_64-none-linux --target=x86_64-none-linux --enable-shared --prefix=/usr --with-ca-bundle=/etc/ssl/certs/ca-certificates.crt --with-ca-path=/etc/ssl/certs/
	make -j8
	make DESTDIR=$path_to_avs/boot2qt/build-curl install
	cp $path_to_avs/boot2qt/build-curl/usr/* $qtpath/toolchain/sysroots/corei7-64-poky-linux/usr/ -r
 
	
fi

if $deploy_curl_to_device; then
	echo "===========>>>>>>>> Copy libcURL to target device"
	scp -r $path_to_avs/boot2qt/build-nghttp2/usr/* $device_connection:/usr
	scp -r $path_to_avs/boot2qt/build-curl/usr/* $device_connection:/usr
fi



##SDK sources
if $build_avs_sdk; then
	cd $path_to_avs/boot2qt/
	rm -rf $path_to_avs/boot2qt/avs-device-sdk-1.12
	wget https://github.com/alexa/avs-device-sdk/archive/v1.12.zip
	unzip v1.12.zip 

	cd $path_to_avs/boot2qt/
	wget -c http://www.portaudio.com/archives/pa_stable_v190600_20161030.tgz
	tar zxf pa_stable_v190600_20161030.tgz
	cd portaudio
	./configure --without-jack --host=x86_64-none-linux --target=x86_64-none-linux
	make -j8 
	
	cd $path_to_avs/boot2qt/
	rm -rf snowboy
	git clone https://github.com/Kitt-AI/snowboy/

	if $use_wakeup_word; then
		#cd $path_to_avs/boot2qt/
		#rm -rf BLAS-3.8.0
		#wget http://www.netlib.org/blas/blas-3.8.0.tgz
		#tar zxf blas-3.8.0.tgz
		#cd BLAS-3.8.0
		#gfortran -shared -O2 *.f -o libblas.so -fPIC
		cp /usr/lib/libblas/libblas.so.3.6.0 $qtpath/toolchain/sysroots/corei7-64-poky-linux/usr/lib/libblas.so.3
		scp /usr/lib/libblas/libblas.so.3.6.0 $device_connection:/usr/lib/libblas.so.3
	fi
	
	rm -rf avs-device-sdk-build
	mkdir avs-device-sdk-build
	cd avs-device-sdk-build

	#build SDK
	if $use_wakeup_word; then
		echo "============ Building SDK with wake up word ============="
		echo $path_to_avs
		#echo "add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)" >> $path_to_avs/boot2qt/avs-device-sdk-1.12/CMakeLists.txt 
		cd $path_to_avs/boot2qt/avs-device-sdk-1.12
		git apply ../avs_kwd.diff
		echo $path_to_avs/boot2qt/avs-device-sdk-build
		cd $path_to_avs/boot2qt/avs-device-sdk-build
		echo $PWD
		cmake $path_to_avs/boot2qt/avs-device-sdk-1.12 -DKITTAI_KEY_WORD_DETECTOR=ON -DGSTREAMER_MEDIA_PLAYER=ON -DPORTAUDIO=ON -DPORTAUDIO_LIB_PATH=$path_to_avs/boot2qt/portaudio/lib/.libs/libportaudio.a -DPORTAUDIO_INCLUDE_DIR=$path_to_avs/boot2qt/portaudio/include -DKITTAI_KEY_WORD_DETECTOR_LIB_PATH=$path_to_avs/boot2qt/snowboy/lib/ubuntu64/libsnowboy-detect.a -DKITTAI_KEY_WORD_DETECTOR_INCLUDE_DIR=$path_to_avs/boot2qt/snowboy/include -DKWD=ON -DCMAKE_BUILD_TYPE=DEBUG
	else
		echo "============ Building SDK without wake up word ============="
		cd $path_to_avs/boot2qt/avs-device-sdk-build
		cmake $path_to_avs/boot2qt/avs-device-sdk-1.12 -DSENSORY_KEY_WORD_DETECTOR=OFF -DGSTREAMER_MEDIA_PLAYER=ON -DPORTAUDIO=ON -DPORTAUDIO_LIB_PATH=$path_to_avs/boot2qt/portaudio/lib/.libs/libportaudio.a -DPORTAUDIO_INCLUDE_DIR=$path_to_avs/boot2qt/portaudio/include
	fi

	make -j8
	make DESTDIR=$path_to_avs/boot2qt/build install
fi

#copy to toolchain
if $copy_sdk_to_toolchain; then
	echo "========== Copy SDK to toolchain ================="
	cd $path_to_avs/boot2qt
	cp build/usr/local/include/* $qtpath/toolchain/sysroots/corei7-64-poky-linux/usr/include -r
	cp build/usr/local/lib/* $qtpath/toolchain/sysroots/corei7-64-poky-linux/usr/lib -r
fi

if $copy_sdk_to_device; then
	#copy to device
	cd $path_to_avs/boot2qt
	scp -r build/usr/local/lib/*.so $device_connection:/usr/lib
fi

#build Alexa app
if $build_alexa_package; then
	cd $path_to_avs/boot2qt
	rm qtvoiceassistant -rf
	git clone "https://codereview.qt-project.org/qt/qtvoiceassistant"
	cd qtvoiceassistant
	#git checkout 23aec57aafb6b24e62e52e1ac76520ce755df272
	git fetch "https://codereview.qt-project.org/qt/qtvoiceassistant" refs/changes/98/273098/1 && git checkout FETCH_HEAD
	if $use_wakeup_word; then
		git apply ../kwd.diff	
	fi
	git apply ../appid.diff
	rm -rf .build
	mkdir .build
	cd .build
	qmake ../com.luxoft.alexa.pro
	make package -j8
fi


#copy and install Alexa
if $deploy_alexa_package; then
	echo 'Copy and install Alexa'
	cd $path_to_avs/boot2qt/qtvoiceassistant/.build
	echo $PWD
	scp com.luxoft.alexaboot2qt.pkg $device_connection:/tmp
	ssh $device_connection -t 'appman-controller install-package /tmp/com.luxoft.alexaboot2qt.pkg -a'
	ssh $device_connection -t 'appman-controller start-application com.luxoft.alexaboot2qt'
fi
