Improved and fixed local SDK tarball installation.
[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 do_cleanup=true
33 do_force=false
34 while [ $# -ne 0 ]; do
35     case $1 in
36         --debug)
37             set -x
38             DEBUG_OPT="-D"
39             ;;
40         -f|--file)
41             shift
42             SDK_FILE=$1
43             ;;
44         --force)
45             do_force=true
46             ;;
47         -u|--url)
48             shift
49             URL=$1
50             ;;
51         -no-clean)
52             do_cleanup=false
53             ;;
54         -h|--help)
55             usage
56             ;;
57         *)
58             echo "Invalid argument: $1"
59             usage
60             ;;
61     esac
62     shift
63 done
64
65 [ "$SDK_FILE" = "" ] && [ "$URL" = "" ] && { echo "--file or --url option must be set"; exit 1; }
66
67 # Create SDK root dir if needed
68 [ ! -d ${SDK_ROOT_DIR} ] && mkdir -p ${SDK_ROOT_DIR}
69 cd ${SDK_ROOT_DIR} || exit 1
70
71 # Cleanup
72 trap "cleanExit" 0 1 2 15
73 cleanExit ()
74 {
75     if ($do_cleanup); then
76         [[ -d ${TMPDIR} ]] && rm -rf ${TMPDIR}
77     fi
78 }
79
80 # Download sdk
81 if [ "$URL" != "" ]; then
82     TMPDIR=$(mktemp -d)
83     SDK_FILE=${TMPDIR}/$(basename ${URL})
84     echo "Downloading $(basename ${SDK_FILE}) ..."
85     wget --no-check-certificate "$URL" -O "${SDK_FILE}" || exit 1
86 fi
87
88 # Retreive SDK info
89 sdkNfo=$(${SCRIPTS_DIR}/get-sdk-info --file "${SDK_FILE}")
90 if [ "$?" != "0" ]; then
91     echo $sdkNfo
92     exit 1
93 fi
94
95 PROFILE=$(echo "$sdkNfo" |egrep -o '"profile"[^,]*' |cut -d'"' -f4)
96 VERSION=$(echo "$sdkNfo" |egrep -o '"version"[^,]*' |cut -d'"' -f4)
97 ARCH=$(echo "$sdkNfo" |egrep -o '"arch"[^,]*' |cut -d'"' -f4)
98
99 [ "$PROFILE" = "" ] && { echo "PROFILE is not set"; exit 1; }
100 [ "$VERSION" = "" ] && { echo "VERSION is not set"; exit 1; }
101 [ "$ARCH" = "" ] && { echo "ARCH is not set"; exit 1; }
102
103 DESTDIR=${SDK_ROOT_DIR}/${PROFILE}/${VERSION}/${ARCH}
104
105 [ -d ${DESTDIR} ] && [ "$do_force" != "true" ] && { echo "SDK already installed in $DESTDIR"; exit 1; }
106
107 # Cleanup previous install
108 rm -rf ${DESTDIR} && mkdir -p ${DESTDIR} || exit 1
109
110 # Install sdk
111 chmod +x ${SDK_FILE}
112 ${SDK_FILE} ${DEBUG_OPT} -y -d ${DESTDIR} 2>&1