#!/bin/bash
# Description: download requirements package by requirements.json content.

function download_requirement() {
    local len=$(cat requirements.json | jq ".|length")
    local end=$(($len - 1))
    for i in $(seq 0 $end); do
        local name=$(cat requirements.json | jq .[$i].name | sed 's/"//g')
        local package=$(cat requirements.json | jq .[$i].package | sed 's/"//g')
        local path=$(cat requirements.json | jq .[$i].path | sed 's/"//g')
        local url=$(cat requirements.json | jq .[$i].url | sed 's/"//g')
        local uncompress=$(cat requirements.json | jq .[$i].uncompress | sed 's/"//g')
        echo "download package: $name."
        [ -d "$path" ] || mkdir -p $path
        if [ $? -eq 0 ]; then
            wget -q ${url}/${package} -P $path
            if [ $? -eq 0 ]; then
                echo "download package to $path success."
            else
                echo "download package to $path failed."
                exit
            fi
            if [ "$uncompress"x == "true"x ]; then
                echo "uncompress $package to $path"
                tar -xf ${path}/${package} -C $path
                if [ $? -eq 0 ]; then
                    echo "uncompress package to $path success."
                    rm -rf ${path}/${package}
                else
                    echo "uncompress package to $path failed."
                    exit
                fi
            fi
        fi
    done
}

#source scripts/utils.sh
download_requirement
