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
25function clear_dir(){
26        if [ ! -d "$1" ]; then
27                mkdir -p $1
28        else
29                rm -rf $1/*
30        fi
31}
32
33function create_dir(){
34        if [ ! -d "$1" ]; then
35                mkdir -p $1
36        fi
37}
38
39arg_sdk_path=${BASE_PATH}/sdk
40arg_full_sdk_path=${BASE_PATH}/sdk-full
41arg_conf_path=${BASE_PATH}/config/init_sdk.config
42arg_help="0"
43
44function print_help() {
45  cat <<EOF
46  use assembleHap [options] <mainclass> [args...]
47
48    --sdk_path=[public sdk absolute address] (default:$arg_sdk_path)
49    --full_sdk_path=[full sdk absolute address] (default:$arg_full_sdk_path)
50    --conf_path=[config absolute address] (default:$arg_conf_path)]
51
52    --help  - prints help screen
53
54EOF
55}
56
57function parse_arguments() {
58        local helperKey="";
59        local helperValue="";
60        local current="";
61
62        while [ "$1" != "" ]; do
63                current=$1;
64                helperKey=${current#*--};
65                helperKey=${helperKey%%=*};
66                helperKey=$(echo "$helperKey" | tr '-' '_');
67                helperValue=${current#*=};
68                if [ "$helperValue" == "$current" ]; then
69                        helperValue="1";
70                fi
71                # echo "eval arg_$helperKey=\"$helperValue\"";
72
73                eval "arg_$helperKey=\"$helperValue\"";
74                shift
75        done
76}
77
78parse_arguments ${@};
79
80if [ "$arg_help" != "0" ]; then
81        print_help;
82        exit 1;
83fi
84
85
86function init_sdk_dir(){
87        echo "init sdk dir..."
88        clear_dir ${arg_sdk_path}
89	clear_dir ${arg_full_sdk_path}
90}
91
92function load_sdk(){
93        source_path=""
94	sdk_type=${1}
95	sdk_version=${2}
96        sdk_url=${3}
97	echo "loading sdk from ${sdk_url}..."
98	target_path=${BASE_PATH}
99        if [[ "${sdk_type}" == "public" ]]; then
100        	target_path=${arg_sdk_path}
101	elif [[ "${sdk_type}" == "full" ]]; then
102        	target_path=${arg_full_sdk_path}
103	else
104		echo "the ${sdk_url} is invalid"
105        	exit 1
106        fi
107        clear_dir ${TEMP_SDK_PAK}
108        cd ${TEMP_SDK_PAK}
109        wget ${sdk_url}
110        
111        for z in `ls`
112        do
113                if [ "${z##*.}" == "gz" ]; then
114                        tar -zxf ${z}
115                fi
116        done
117        
118        for z in ${TEMP_SDK_PAK}/*
119        do
120                if [ -d ${z}/linux ]; then
121                        source_path=${z}
122                fi
123        done
124        
125        cd ${source_path}/linux/
126        for z in `ls`
127        do
128                if [ "${z##*.}" != "zip" ]; then
129                        continue
130                fi
131                echo "unzip ${z}ing..."
132                unzip -oq ${z}
133        done
134        
135        for z in ${source_path}/linux/*
136        do
137                if [ "${z##*.}" == "zip" ]; then
138                        continue
139                fi
140                if [ -f "${z}" ]; then
141                        continue
142                fi
143                create_dir ${target_path}/${sdk_version}
144                echo "moving... ${z} to ${target_path}/${sdk_version}/${z##*/}"
145                mv ${z} ${target_path}/${sdk_version}/${z##*/}
146                if [ ${z##*/} == "ets" ]; then
147                        echo "ets npm install"
148                        cd ${target_path}/${sdk_version}/${z##*/}/build-tools/ets-loader
149                        npm install > /dev/null 2>&1
150                fi
151                if [ ${z##*/} == "js" ]; then
152                        echo "js npm install"
153                        cd ${target_path}/${sdk_version}/${z##*/}/build-tools/ace-loader
154                        npm install > /dev/null 2>&1
155                fi
156        
157        done
158        rm -rf ${TEMP_SDK_PAK}
159}
160
161
162init_sdk_dir
163
164while read line
165do
166	if [ ${line:0:1} == "#" ]; then
167		continue
168	fi
169	sdk_desc=(${line//;/ })
170        load_sdk ${sdk_desc[0]} ${sdk_desc[1]} ${sdk_desc[2]}         
171done < ${arg_conf_path}
172
173echo "sdk init success.."
174exit 0
175