1#  Copyright (c) 2023 Huawei Device Co., Ltd.
2#  Licensed under the Apache License, Version 2.0 (the "License");
3#  you may not use this file except in compliance with the License.
4#  You may obtain a copy of the License at
5
6#      http://www.apache.org/licenses/LICENSE-2.0
7
8#  Unless required by applicable law or agreed to in writing, software
9#  distributed under the License is distributed on an "AS IS" BASIS,
10#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11#  See the License for the specific language governing permissions and
12#  limitations under the License.
13
14#!/bin/bash
15
16
17#npm
18npm config set @ohos:registry=https://repo.harmonyos.com/npm/
19
20date_time=`date +"%Y%m%d%H%M%S"`
21CUR_PATH=$(cd $(dirname ${BASH_SOURCE[0]}) && pwd)
22BASE_PATH=$(dirname ${CUR_PATH})
23TEMP_SDK_PAK=${BASE_PATH}/temp_sdk_${date_time}
24
25arg_url=""
26arg_sdk_path=""
27arg_api_version=""
28arg_help="0"
29
30function clear_dir(){
31        if [ ! -d "$1" ]; then
32                mkdir -p $1
33        else
34                rm -rf $1/*
35        fi
36}
37
38function get_sdk_type(){
39	sdk_url=${1}
40	is_public=$(echo ${sdk_url} | grep "\-public")
41	if [[ ${is_public} != "" ]]; then
42                arg_sdk_type="public"
43		return
44        fi
45        is_full=$(echo ${sdk_url} | grep "\-full")
46        if [[ ${is_full} != "" ]]; then
47                arg_sdk_type="full"
48        fi
49	arg_sdk_type="unknown"
50}
51
52
53function print_help() {
54  cat <<EOF
55  use update_sdk [options] <mainclass> [args...]
56
57    --url=[sdk url]
58    --sdk_path=[public sdk absolute address]
59
60    --help  - prints help screen
61
62EOF
63}
64
65function parse_arguments() {
66        local helperKey="";
67        local helperValue="";
68        local current="";
69
70        while [ "$1" != "" ]; do
71                current=$1;
72                helperKey=${current#*--};
73                helperKey=${helperKey%%=*};
74                helperKey=$(echo "$helperKey" | tr '-' '_');
75                helperValue=${current#*=};
76                if [ "$helperValue" == "$current" ]; then
77                        helperValue="1";
78                fi
79                # echo "eval arg_$helperKey=\"$helperValue\"";
80
81                eval "arg_$helperKey=\"$helperValue\"";
82                shift
83        done
84}
85
86parse_arguments ${@};
87
88if [ "$arg_help" != "0" ]; then
89        print_help;
90        exit 1;
91fi
92
93if [ "${arg_url}" == "" ]; then
94        echo "--url is not null"
95        exit 1;
96fi
97
98if [ "${arg_sdk_path}" == "" ]; then
99        echo "--sdk_path is not null"
100        exit 1;
101fi
102
103
104
105# 添加交互式判断 sdk目录是否删除 判空等操作
106clear_dir ${arg_sdk_path}
107clear_dir ${TEMP_SDK_PAK}
108cd ${TEMP_SDK_PAK}
109wget ${arg_url}
110
111for z in `ls`
112do
113	if [ "${z##*.}" == "gz" ]; then
114                tar -zxf ${z}
115        fi
116done
117
118source_path=""
119for z in ${TEMP_SDK_PAK}/*
120do
121        if [ -d ${z}/linux ]; then
122                source_path=${z}
123        fi
124done
125cd ${source_path}/linux/
126for z in `ls`
127do
128	if [ "${z##*.}" != "zip" ]; then
129		continue
130        fi
131	echo "unzip ${z}ing..."
132	unzip -oq ${z}
133done
134
135if [ "${arg_api_version}" == "" ]; then
136	for z in ${source_path}/linux/*
137	do
138        	if [ "${z##*.}" == "zip" ]; then
139                	continue
140        	fi
141        	if [ -f "${z}" ]; then
142                	continue
143        	fi
144		if [ -f "${z}/oh-uni-package.json" ]; then
145			arg_api_version=$(cat ${z}/oh-uni-package.json | grep "apiVersion")
146			break
147		fi
148	done
149fi
150echo "apiVersion is ${arg_api_version##*:}"
151target_path=${arg_sdk_path}
152for z in ${source_path}/linux/*
153do
154	if [ "${z##*.}" == "zip" ]; then
155		continue
156	fi
157	if [ -f "${z}" ]; then
158                continue
159        fi
160	echo "moving... ${z} to ${target_path}"
161	mv ${z} ${target_path}/
162	if [ ${z##*/} == "ets" ]; then
163		echo "ets npm install"
164		cd ${target_path}/${z##*/}/build-tools/ets-loader
165		npm install > /dev/null 2>&1
166	fi
167	if [ ${z##*/} == "js" ]; then
168		echo "js npm install"
169                cd ${target_path}/${z##*/}/build-tools/ace-loader
170                npm install > /dev/null 2>&1
171        fi
172
173done
174rm -rf ${TEMP_SDK_PAK}
175
176echo "sdk undate success.."
177exit 0
178