#!/bin/bash
#This script is supposed to run under directory cva_e2e_sample_l which is generated by script pack_binary.sh
#Please use "sudo" to run this script for installing libva, media-driver and MediaSDK library binaries.

#echo "If libva/media-driver/MediaSDK have been installed before, their libraies will be overwrote!"

if [ "$EUID" -ne 0 ]
then
    echo "Please use sudo -E to run this script"
    exit
fi

cur_home=~
echo "Current home dir $cur_home"
if [[ cur_home == *"root"* ]];
then
    echo "Please use sudo to run this script"
    exit
fi

echo "Install dependent packages"
apt-get -y install libssl-dev dh-autoreconf libgl1-mesa-dev libpciaccess-dev build-essential ffmpeg libjpeg-dev libavcodec-dev libavutil-dev libavformat-dev libopencv-dev pkg-config libgflags-dev

#install libva binaries
root_path=$PWD
LIBVA_INSTALL_PREFIX="/opt/intel/svet/msdk"
LIBVA_INSTALL_PATH="${LIBVA_INSTALL_PREFIX}/lib"
LIBVA_DRIVERS_PATH="${LIBVA_INSTALL_PATH}/dri"
LIBVA_DRIVER_NAME="iHD"

cd $root_path/libva/
echo "Install libva header files and binaries"
set -x
mkdir -p ${LIBVA_INSTALL_PREFIX}/include/va
/usr/bin/install -c -m 644 va_drm.h ${LIBVA_INSTALL_PREFIX}/include/va
/usr/bin/install -c -m 644 va_dri2.h va_dricommon.h ${LIBVA_INSTALL_PREFIX}/include/va
/usr/bin/install -c -m 644 va_backend_glx.h va_glx.h ${LIBVA_INSTALL_PREFIX}/include/va
mkdir -p ${LIBVA_INSTALL_PATH}
/usr/bin/install -c libva.so.2.1100.0 ${LIBVA_INSTALL_PATH}/libva.so.2.1100.0
/usr/bin/install -c libva.lai ${LIBVA_INSTALL_PATH}/libva.la
/usr/bin/install -c libva-drm.so.2.1100.0T ${LIBVA_INSTALL_PATH}/libva-drm.so.2.1100.0
/usr/bin/install -c libva-drm.lai ${LIBVA_INSTALL_PATH}/libva-drm.la
/usr/bin/install -c libva-x11.lai ${LIBVA_INSTALL_PATH}/libva-x11.la
/usr/bin/install -c libva-glx.so.2.1100.0T ${LIBVA_INSTALL_PATH}/libva-glx.so.2.1100.0
/usr/bin/install -c libva-x11.so.2.1100.0T ${LIBVA_INSTALL_PATH}/libva-x11.so.2.1100.0
/usr/bin/install -c libva-glx.lai ${LIBVA_INSTALL_PATH}/libva-glx.la
cd ${LIBVA_INSTALL_PATH} && { ln -s -f libva.so.2.1100.0 libva.so.2 || { rm -f libva.so.2 && ln -s libva.so.2.1100.0 libva.so.2; }; }
cd ${LIBVA_INSTALL_PATH} && { ln -s -f libva.so.2.1100.0 libva.so || { rm -f libva.so && ln -s libva.so.2.1100.0 libva.so; }; }
cd ${LIBVA_INSTALL_PATH} && { ln -s -f libva-drm.so.2.1100.0 libva-drm.so.2 || { rm -f libva-drm.so.2 && ln -s libva-drm.so.2.1100.0 libva-drm.so.2; }; }
cd ${LIBVA_INSTALL_PATH} && { ln -s -f libva-drm.so.2.1100.0 libva-drm.so || { rm -f libva-drm.so && ln -s libva-drm.so.2.1100.0 libva-drm.so; }; }
cd ${LIBVA_INSTALL_PATH} && { ln -s -f libva-x11.so.2.1100.0 libva-x11.so.2 || { rm -f libva-x11.so.2 && ln -s libva-x11.so.2.1100.0 libva-x11.so.2; }; }
cd ${LIBVA_INSTALL_PATH} && { ln -s -f libva-x11.so.2.1100.0 libva-x11.so || { rm -f libva-x11.so && ln -s libva-x11.so.2.1100.0 libva-x11.so; }; }
cd ${LIBVA_INSTALL_PATH} && { ln -s -f libva-glx.so.2.1100.0 libva-glx.so.2 || { rm -f libva-glx.so.2 && ln -s libva-glx.so.2.1100.0 libva-glx.so.2; }; }
cd ${LIBVA_INSTALL_PATH} && { ln -s -f libva-glx.so.2.1100.0 libva-glx.so || { rm -f libva-glx.so && ln -s libva-glx.so.2.1100.0 libva-glx.so; }; }
mkdir -p ${LIBVA_INSTALL_PREFIX}/include/va
cd $root_path/libva/
/usr/bin/install -c -m 644  va.h va_backend.h va_backend_vpp.h va_compat.h va_dec_av1.h va_dec_hevc.h va_dec_jpeg.h va_dec_vp8.h va_dec_vp9.h va_drmcommon.h va_egl.h va_enc_hevc.h va_enc_h264.h va_enc_jpeg.h va_enc_vp8.h va_fei.h va_fei_h264.h va_enc_mpeg2.h va_fei_hevc.h va_enc_vp9.h va_str.h va_tpi.h va_version.h va_vpp.h va_x11.h ${LIBVA_INSTALL_PREFIX}/include/va
mkdir -p ${LIBVA_INSTALL_PATH}/pkgconfig
/usr/bin/install -c -m 644 libva.pc libva-drm.pc libva-x11.pc libva-glx.pc ${LIBVA_INSTALL_PATH}/pkgconfig
mkdir -p ${LIBVA_INSTALL_PREFIX}/bin
cp $root_path/libva-utils/vainfo ${LIBVA_INSTALL_PREFIX}/bin/
set +x

echo "Install media-driver binaries to  ${LIBVA_DRIVERS_PATH}"
cd $root_path
set -x
mkdir -p ${LIBVA_DRIVERS_PATH}
cp media-driver/iHD_drv_video.so ${LIBVA_DRIVERS_PATH}/iHD_drv_video.so
cp media-driver/libigfxcmrt.so ${LIBVA_INSTALL_PATH}
set +x

echo "Install MediaSDK binaries to /opt/intel/mediasdk/"
cd $root_path
set -x
cd $root_path/MediaSDK
mkdir -p ${LIBVA_INSTALL_PREFIX}/share/mfx
mkdir -p ${LIBVA_INSTALL_PREFIX}/include
mkdir -p ${LIBVA_INSTALL_PREFIX}/lib/mfx
cp -rf include/* ${LIBVA_INSTALL_PREFIX}/include/
cp -rf lib/* ${LIBVA_INSTALL_PREFIX}/lib/
cp -rf share/* ${LIBVA_INSTALL_PREFIX}/share
ln -n ${LIBVA_INSTALL_PATH}/libmfxhw64.so.1.35 ${LIBVA_INSTALL_PATH}/libmfxhw64.so || { rm -f ${LIBVA_INSTALL_PATH}/libmfxhw64.so && ln -n ${LIBVA_INSTALL_PATH}/libmfxhw64.so.1.35 ${LIBVA_INSTALL_PATH}/libmfxhw64.so; }
set +x

#echo "Add enviroment setting command line to ~/.bashrc"
#
#va_env_set=`grep "LIBVA_DRIVERS_PATH=/usr/lib/x86_64-linux-gnu/dri" ~/.bashrc -c`

#if (( va_env_set == 0 ))
#then
#set -x
#    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/x86_64-linux-gnu:/usr/lib
#    export LIBVA_DRIVERS_PATH=/usr/lib/x86_64-linux-gnu/dri
#    export LIBVA_DRIVER_NAME=iHD
#    export MFX_HOME=/opt/intel/mediasdk
#set +x
#    echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/x86_64-linux-gnu:/usr/lib' >> ~/.bashrc
#    echo "export LIBVA_DRIVERS_PATH=/usr/lib/x86_64-linux-gnu/dri" >> ~/.bashrc
#    echo "export LIBVA_DRIVER_NAME=iHD" >> ~/.bashrc
#    echo "export MFX_HOME=/opt/intel/mediasdk" >> ~/.bashrc
#
#fi

#if [[ $LD_LIBRARY_PATH != *"/opt/intel/mediasdk/lib/"* ]]
#then
#    export LD_LIBRARY_PATH="/opt/intel/mediasdk/lib/:$LD_LIBRARY_PATH"
#    echo 'export LD_LIBRARY_PATH="/opt/intel/mediasdk/lib/:$LD_LIBRARY_PATH"' >> ~/.bashrc
#fi

openvino_env_set=`grep "intel/openvino_2021/bin/setupvars.sh" ~/.bashrc -c`
if (( openvino_env_set == 0 ))
then
    if [ -f ~/intel/openvino/bin/setupvars.sh ]
    then
        echo "source ~/intel/openvino_2021/bin/setupvars.sh" >> ~/.bashrc
    elif [ -f /opt/intel/openvino/bin/setupvars.sh ]
    then
        echo "source /opt/intel/openvino_2021/bin/setupvars.sh" >> ~/.bashrc
    fi
fi

if [[ -d $cl_cache_dir ]];
then
    echo "cl_cache is enabled. \$cl_cache_dir : $cl_cache_dir"
else
    echo "Add enabling cl_cache commands to ~\.bashrc"
    set -x
    mkdir -p ~/cl_cache
    export cl_cache_dir=~/cl_cache
    echo "mkdir -p ~/cl_cache" >> ~/.bashrc
    echo "export cl_cache_dir=~/cl_cache" >> ~/.bashrc
fi

cd $root_path
source ./download_and_copy_models.sh
echo "Please run vainfo firstly to check if media driver has been installed successfully"
echo "After all models are downloaded successfully, please run source ./svet_env_setup.sh, then ./run_face_detection_test.sh for testing 16-channel 1080p face detection"
echo "Please switch to text mode(No X server) and use root user (su -p) to run the test"
