#!/bin/bash

cuda_installer=$1

nvidia_devices=$(lspci -d 10de:)
if [ "$nvidia_devices" == "" ]; then
	echo "make sure there is a nvidia pci device"
	exit -1
fi

if [ $(id -u) != 0 ]; then
	echo "must be root"
	exit -1
fi

wait_time=0
get_cuda=0
while [ $wait_time -lt 100 ]; do
	wget "http://mirrors.tencentyun.com/install/GPU/$cuda_installer" -O /tmp/$cuda_installer
	if [ $? == 0 ]; then
		get_cuda=1
		break;
	fi
	wait_time=$(($wait_time+1))
	sleep 1
done
if [ x$get_cuda != x1 ]; then
        echo "download cuda file failed, please check the input,url oand network"
        exit -1
fi

chmod u+x /tmp/$cuda_installer
/tmp/$cuda_installer --toolkit --samples --silent
echo "install nvidia cuda finished"
rm -f /tmp/$cuda_installer

if [ -d /usr/local/cuda-*/samples ]; then
    cd /usr/local/cuda-*/samples/1_Utilities/deviceQuery && make> /dev/null 2>&1 && ./deviceQuery > /dev/null 2>&1 || { echo "install cuda failed and exit."; exit 1;}
else
    cd /root/NVIDIA_CUDA-*_Samples/1_Utilities/deviceQuery && make> /dev/null 2>&1 && ./deviceQuery > /dev/null 2>&1 || { echo "install cuda failed and exit."; exit 1;}
fi

# restart barad
if [ -d /usr/local/qcloud/monitor/barad/ ]; then
    if [ $(ps -ef | grep barad_agent | wc -l) -gt 1 ]; then
        /usr/local/qcloud/monitor/barad/admin/stop.sh
        /usr/local/qcloud/monitor/barad/admin/trystart.sh
        echo "barad agent restarted"
    fi
fi
