#!/bin/bash

if  [ $# -ne 2 ] || [ "$1" != "--version" ] ;then
    echo -e "Error:This installation requires to enter the parameter value of the openstation version, for example: \n"
    echo "=========================================================\n"
    echo "# bash openstation-install-online.sh --version latest"
	echo "# bash openstation-install-online.sh --version 0.6.8"
    echo "=========================================================\n"
    exit -1
else
    osta_version=$2
fi

base_dir=$(cd `dirname $0`;pwd)

# 打印联网安装提示信息
echo -e "============================================================\n"
while true;do
        read -p "The OpenStation installation requires an internet connection to download the required components. Please ensure that the internet is properly connected and press the Enter key to continue." input

        if [ -z "$input" ]; then
                break
        else
                echo -e "ERROR: please press the Enter key to continue."
        fi
done

#校验网络连通性
echo -e "============================================================\n"
echo -e "Checking Internet Connectivity:"
ping_res=`ping -c 2  nvidia.github.io 2>&1`
if [ $? == 0 ];then
    echo -e "`ping -c 2  nvidia.github.io 2>&1|sed 's/^/   /'`"
    echo -e "   Internet connection is active.\n"
else
    echo -e "`ping -c 2  nvidia.github.io 2>&1|sed 's/^/   /'`"
    echo -e "   ERROR: Internet connection is unactive.\n"
        #exit 1
fi
echo -e "============================================================\n"

#开始下载文件
if [ "${osta_version}" == "latest" ];then
	file_url="https://fastaistack.oss-cn-beijing.aliyuncs.com/openstation/openstation-pkg-online-latest.tar.gz"
	file_sha256sum_url="https://fastaistack.oss-cn-beijing.aliyuncs.com/openstation/SHA256SUM-ONLINE-LATEST"
else
	file_url="https://fastaistack.oss-cn-beijing.aliyuncs.com/openstation/openstation-pkg-online-v${osta_version}.tar.gz"
	file_sha256sum_url="https://fastaistack.oss-cn-beijing.aliyuncs.com/openstation/SHA256SUM-ONLINE-${osta_version}"
fi

file_name=$(basename "$file_url")
echo -e "Start downloading the OpenStation installation package."
echo -e "Downloading the file: $file_name..."
begin_time=$(date +%s)
curl -O "$file_sha256sum_url"


if [ "${osta_version}" == "latest" ];then
	if [ -f "$file_name" ] && [ "`cat SHA256SUM-ONLINE-LATEST|awk '{print $1}'`" == "`sha256sum openstation-pkg-online-latest.tar.gz | awk '{print $1}'`" ];then
		echo -e "The $file_name file already exists, skipping the download."
	else
		curl -O "$file_url"
		#校验下载过程中网络是否中断
		if [ $? -ne 0 ]; then
			echo -e "Error: The $file_name file download failed and please check your internet!"
			exit 1
		else
			echo -e "The $file_name file has been downloaded successfully."
		fi

	fi
else
	if [ -f "$file_name" ] && [ "`cat SHA256SUM-ONLINE-${osta_version}|awk '{print $1}'`" == "`sha256sum openstation-pkg-online-v${osta_version}.tar.gz | awk '{print $1}'`" ];then
		echo -e "The $file_name file already exists, skipping the download."
	else
		curl -O "$file_url"
		#校验下载过程中网络是否中断
		if [ $? -ne 0 ]; then
			echo -e "Error: The $file_name file download failed and please check your internet!"
			exit 1
		else
			echo -e "The $file_name file has been downloaded successfully."
		fi

	fi
fi



echo -e "============================================================\n"
end_time=$(date +%s)
let elapsed_time=end_time-begin_time
all_time=$(echo "$elapsed_time/60"| bc)
echo -e "The $file_name file download time is ${all_time}m."
echo -e "============================================================\n"

#校验下载的文件是否损坏
echo -e "Verifying the integrity of the installation package..."
if [ "${osta_version}" == "latest" ];then
	if [ "`cat SHA256SUM-ONLINE-LATEST|awk '{print $1}'`" != "`sha256sum openstation-pkg-online-latest.tar.gz | awk '{print $1}'`" ]; then
		echo -e "Error: The file is corrupted or incomplete!"
		rm -f "$file_name"
		exit 1
	else
		echo -e "The downloaded installation package file is normal. Now, start to extract the installation package..."
		tar -xvzf $file_name
		echo -e "The extraction of the installation package is completed."
        cd ${base_dir}/openstation-pkg-online-latest/deploy
		bash install.sh true
	fi
else
	if [ "`cat SHA256SUM-ONLINE-${osta_version}|awk '{print $1}'`" != "`sha256sum openstation-pkg-online-v${osta_version}.tar.gz | awk '{print $1}'`" ]; then
		echo -e "Error: The file is corrupted or incomplete!"
		rm -f "$file_name"
		exit 1
	else
		echo -e "The downloaded installation package file is normal. Now, start to extract the installation package..."
		tar -xvzf $file_name
		echo -e "The extraction of the installation package is completed."
		cd ${base_dir}/openstation-pkg-online-v${osta_version}/deploy
		bash install.sh true
	fi
fi




