#!/bin/bash

download_url=$1
os_file_name=$2
download_filename=${os_file_name}.new
tmp_file=/usr/local/zjmf/logs/${os_file_name}.txt

download_path=/home/kvm/images

download_url=(${download_url// / })

function check(){
    if [ -z $1 ];then
        read -p "input the path(e.g: /root):" path
    else
        path=$1
    fi
    if [ "${path:0:1}" = "/" ];then
        if [ ! -d ${path} ];then
            mkdir -p ${path}
        fi
    else
        pwd=`pwd`
        if [ ! -d "${pwd}/${path}" ];then
            mkdir -p ${pwd}/${path}
        fi
        path=${pwd}/${path}
    fi
    df | while read line
    do
        line_path=`echo ${line} | awk -F' ' '{print $6}'`
        line_avail=`echo ${line} | awk -F' ' '{print $4}'`
        if [ "${line_path:0:1}" != "/" ]; then
            continue
        fi
        if [ "${line_path}" = "/" ]; then
            root_avail=${line_avail}
            if [ -f /tmp/tmp_root_avail ];then
                rm /tmp/tmp_root_avail
            fi
            echo ${root_avail} > /tmp/tmp_root_avail
            continue
        fi
        path_length=${#line_path}
        if [ "${path:0:${path_length}}" = "${line_path}" ];then
            path_avail=${line_avail}
            if [ -f /tmp/tmp_path_avail ];then
                rm /tmp/tmp_path_avail
            fi
            echo ${path_avail} > /tmp/tmp_path_avail
            break
        fi
    done
    if [ -f /tmp/tmp_path_avail ];then
        path_avail=`cat /tmp/tmp_path_avail`
        rm /tmp/tmp_path_avail
    fi
    if [ -f /tmp/tmp_root_avail ];then
        root_avail=`cat /tmp/tmp_root_avail`
        rm /tmp/tmp_root_avail
    fi
    if [ -z ${path_avail} ];then
        path_avail=${root_avail}
    fi
    echo ${path_avail}
}

if [ ! -d "$download_path" ];then
    mkdir -p $download_path
fi

echo "os_file_name=$os_file_name" > $tmp_file
#send to php which file to transport now
for((i=0;i<${#download_url[@]};i++));do
    file_size=`curl -s -I -m 60 http://${download_url[i]}/cloud/images/$os_file_name |grep -E "Content-Length" |awk '{print $2}' |sed 's/.$//g'`
    if [[ -z "$file_size" && $file_size -lt 100000 ]];then
        if [ $i -eq `expr ${#download_url[@]} - 1` ];then
            echo "status=can not download" >> $tmp_file
            exit 1
        else
            continue
        fi
    else
		break
    fi
done
target_disk_avail=$(check /home/kvm/images)
if [ -n "$file_size" -a "$file_size" != "0" ];then
	# check harddisk avaliable
	[ `expr $file_size + 1048576` -gt `expr $target_disk_avail \* 1024` ] && echo "status=partition nearly full" >> $tmp_file && exit 2
    #curl -s -k -d "group=imageDownloadGetStatus&filesize=$file_size&os_name=$os_name&os_file_name=$os_file_name" "https://$dashboard:20443/report_api.php" &
	echo "filesize=$file_size" >> $tmp_file
	aria2_url=""
	for((i=0;i<${#download_url[@]};i++));do
		if [ $i -eq 0 ];then
			aria2_url=${download_url[i]}
		else
			aria2_url=${download_url[i]}","$aria2_url
		fi
	done
  aria2_url="http://{$aria2_url}/cloud/images/$os_file_name"
  [ `ls -al $download_path/${download_filename}.aria2 |wc -l` -ne 0 ] && rm -rf $download_path/${download_filename}*
  aria2c --file-allocation=none -c -P $aria2_url -d $download_path -o $download_filename > /dev/null 2>&1
  if [ $? -eq 0 ];then
		\mv $download_path/$download_filename $download_path/$os_file_name
		echo "status=download files success" >> $tmp_file
	else
		echo "status=download files failed" >> $tmp_file
  fi
else
	echo "status=can not download" >> $tmp_file
fi