4d9eadf55f0eb618f6b520e8a01651405d42e00e
[src/xds/xds-server.git] / scripts / xds-utils / install-agl-sdks.sh
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 . /etc/xdtrc
21
22 [ -z "$SDK_BASEURL" ] && SDK_BASEURL="http://iot.bzh/download/public/2017/XDS/sdk/"
23 [ -z "$XDT_SDK" ] && XDT_SDK=/xdt/sdk
24
25 # Support only poky_agl profile for now
26 PROFILE="poky-agl"
27
28 # Dynamically retreive SDKs (reduce timeout in case of iot website is not available)
29 SDKS=$(curl -s ${SDK_BASEURL} --connect-timeout 10 | grep -oP  'href="[^"]*.sh"' | cut -d '"' -f 2) || echo "WARNING: IoT.bzh download site not available."
30
31 usage() {
32     echo "Usage: $(basename $0) [-h|--help] [-clean] [-f|--file <agl-sdk-filename>] [-a|--arch <arch name>] [-l|--list]"
33         echo "For example, arch name is: aarch64, armv7vehf or corei7-64"
34         exit 1
35 }
36
37 getFile() {
38     arch=$1
39     for sdk in ${SDKS}; do
40         echo $sdk | grep "${PROFILE}.*${arch}.*.sh" > /dev/null 2>&1
41         if [ "$?" = "0" ]; then
42             echo $sdk
43             return 0
44         fi
45     done
46     echo "No SDK tarball found for arch $arch"
47     return 1
48 }
49
50 do_cleanup=false
51 FILE=""
52 ARCH=""
53 while [ $# -ne 0 ]; do
54     case $1 in
55         -h|--help|"")
56             usage
57             ;;
58         -f|--file)
59             shift
60             FILE=$1
61             ;;
62         -a|--arch)
63             shift
64             ARCH=$1
65             ;;
66         -l|--list)
67             echo "Available SDKs tarballs:"
68             for sdk in $SDKS; do echo " $sdk"; done
69             exit 0
70             ;;
71         -clean)
72             do_cleanup=true
73             ;;
74         *)
75             echo "Invalid argument: $1"
76             usage
77             ;;
78     esac
79     shift
80 done
81
82 if [ "$ARCH" = "x86-64" ]; then
83     echo "Warning x86-64 architure name is deprecated, please use corei7-64 instead !"
84     exit 1
85 fi
86
87 [ ! -d ${XDT_SDK} ] && mkdir -p ${XDT_SDK}
88
89 if [ "$FILE" = "" ]; then
90     FILE=$(getFile $ARCH)
91     if [ "$?" != "0" ]; then
92         echo "$FILE"
93         exit 1
94     fi
95     SDK_FILE=${XDT_SDK}/${FILE}
96 elif [ ! -f "$FILE" ]; then
97     echo "SDK file not found: $FILE"
98     exit 1
99 else
100     DIR=$(cd $(dirname $FILE); pwd)
101     FILE=$(basename $FILE)
102     SDK_FILE=${DIR}/${FILE}
103 fi
104
105 if [ "$ARCH" = "" ]; then
106     echo "Option -a|--arch must be set"
107     usage
108 fi
109
110 # Check that ARCH name matching SDK tarball filename
111 echo "$FILE" | grep "$ARCH" > /dev/null 2>&1
112 if [ "$?" = "1" ]; then
113     echo "ARCH and provided filename doesn't match !"
114     exit 1
115 fi
116
117 cd ${XDT_SDK} || exit 1
118
119 # Cleanup
120 trap "cleanExit" 0 1 2 15
121 cleanExit ()
122 {
123     if ($do_cleanup); then
124         [[ -f ${SDK_FILE} ]] && rm -f ${SDK_FILE}
125     fi
126 }
127
128 # Get SDK installer
129 if [ ! -f "${SDK_FILE}" ]; then
130     do_cleanup=true
131     wget "$SDK_BASEURL/$FILE" -O "${SDK_FILE}" || exit 1
132 fi
133
134 # Retreive default install dir to extract version
135 offset=$(grep -na -m1 "^MARKER:$" "${SDK_FILE}" | cut -d':' -f1)
136 eval $(head -n $offset "${SDK_FILE}" | grep ^DEFAULT_INSTALL_DIR= )
137 VERSION=$(basename $DEFAULT_INSTALL_DIR)
138
139 [ "$PROFILE" = "" ] && { echo "PROFILE is not set"; exit 1; }
140 [ "$VERSION" = "" ] && { echo "VERSION is not set"; exit 1; }
141
142 DESTDIR=${XDT_SDK}/${PROFILE}/${VERSION}/${ARCH}
143
144 # Cleanup previous install
145 rm -rf ${DESTDIR} && mkdir -p ${DESTDIR} || exit 1
146
147 # Install sdk
148 chmod +x ${SDK_FILE}
149 ${SDK_FILE}  -y -d ${DESTDIR}