839d647ffcd4d69a6cdfd11fa66befb16ce7c33b
[src/xds/xds-server.git] / scripts / sdks / agl / add
1 #!/bin/bash
2  ###########################################################################
3 # Copyright 2017 IoT.bzh
4 #
5 # author: Sebastien Douheret <sebastien@iot.bzh>
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #     http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 ###########################################################################
19
20 SCRIPTS_DIR=$(cd $(dirname "$0") && pwd)
21 . ${SCRIPTS_DIR}/_env-init.sh
22
23 usage() {
24     echo "Usage: $(basename $0) [-h|--help] [-f|--file <sdk-filename>] [-u|--url <https_url>] [--force] [--no-clean]"
25         exit 1
26 }
27
28 TMPDIR=""
29 SDK_FILE=""
30 URL=""
31 DEBUG_OPT=""
32 UUID=""
33 do_cleanup=true
34 do_force=false
35 while [ $# -ne 0 ]; do
36     case $1 in
37         --debug)
38             set -x
39             DEBUG_OPT="-D"
40             ;;
41         -f|--file)
42             shift
43             SDK_FILE=$1
44             ;;
45         --force)
46             do_force=true
47             ;;
48         -u|--url)
49             shift
50             URL=$1
51             ;;
52         --uuid)
53             shift
54             UUID=$1
55             ;;
56         -no-clean)
57             do_cleanup=false
58             ;;
59         -h|--help)
60             usage
61             ;;
62         *)
63             echo "Invalid argument: $1"
64             usage
65             ;;
66     esac
67     shift
68 done
69
70 [ "$SDK_FILE" = "" ] && [ "$URL" = "" ] && { echo "--file or --url option must be set"; exit 1; }
71
72 # Create SDK root dir if needed
73 [ ! -d "${SDK_ROOT_DIR}" ] && mkdir -p "${SDK_ROOT_DIR}"
74 cd "${SDK_ROOT_DIR}" || exit 1
75
76 # Cleanup
77 trap "cleanExit" 0 1 2 15
78 cleanExit ()
79 {
80     if ($do_cleanup); then
81         [[ -d ${TMPDIR} ]] && rm -rf ${TMPDIR}
82     fi
83 }
84
85 # Download sdk
86 if [ "$URL" != "" ]; then
87     TMPDIR=$(mktemp -d)
88     SDK_FILE=${TMPDIR}/$(basename "${URL}")
89     echo "Downloading $(basename "${SDK_FILE}") ..."
90     wget --no-check-certificate "$URL" -O "${SDK_FILE}" || exit 1
91 fi
92
93 # Compute uuid when needed
94 if [ "$UUID" = "" ] && [ "$URL" != "" ]; then
95     UUID=$(echo "$URL" | md5sum |cut -d' ' -f1)
96 else
97     echo "UUID value must be specify using --uuid option."
98     exit 1
99 fi
100
101 # Retreive SDK info
102 sdkNfo=$(${SCRIPTS_DIR}/get-sdk-info --file "${SDK_FILE}" --uuid "${UUID}")
103 if [ "$?" != "0" ]; then
104     echo "$sdkNfo"
105     exit 1
106 fi
107
108 PROFILE=$(echo "$sdkNfo" |grep -Eo '"profile"[^,]*' |cut -d'"' -f4)
109 VERSION=$(echo "$sdkNfo" |grep -Eo '"version"[^,]*' |cut -d'"' -f4)
110 ARCH=$(echo "$sdkNfo" |grep -Eo '"arch"[^,]*' |cut -d'"' -f4)
111 DESTDIR=$(echo "$sdkNfo" |grep -Eo '"path"[^,]*' |cut -d'"' -f4)
112
113 [ "$PROFILE" = "" ] && { echo "PROFILE is not set"; exit 1; }
114 [ "$VERSION" = "" ] && { echo "VERSION is not set"; exit 1; }
115 [ "$ARCH" = "" ] && { echo "ARCH is not set"; exit 1; }
116 [ "$DESTDIR" = "" ] && { echo "DESTDIR (path) is not set"; exit 1; }
117
118 [ -d "${DESTDIR}" ] && [ "$do_force" != "true" ] && { echo "SDK already installed in $DESTDIR"; exit 1; }
119
120 # Cleanup previous install
121 rm -rf "${DESTDIR}" && mkdir -p "${DESTDIR}" || exit 1
122
123 # Install sdk
124 chmod +x "${SDK_FILE}"
125 ${SDK_FILE} ${DEBUG_OPT} -y -d "${DESTDIR}" 2>&1