19bf68546ba5515eba6e1a1d5d5e8d487202564a
[src/xds/xds-server.git] / scripts / sdks / agl / get-sdk-info
1 #!/bin/bash
2  ###########################################################################
3 # Copyright 2018 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 . $(dirname "$0")/_env-init.sh
21
22 usage() {
23     echo "Usage: $(basename $0) [-h|--help] [-f|--file <sdk-filename>] [-u|--url <https_url>] [--md5 <string>]"
24         exit 1
25 }
26
27 SDK_FILE=""
28 MD5VAL=""
29 URL=""
30
31 while [ $# -ne 0 ]; do
32     case $1 in
33         -f|--file)
34             shift
35             SDK_FILE=$1
36             ;;
37         -h|--help)
38             usage
39             ;;
40         --md5)
41             shift
42             MD5VAL=$1
43             ;;
44         -u|--url)
45             shift
46             URL=$1
47             ;;
48         *)
49             echo "Invalid argument: $1"
50             usage
51             ;;
52     esac
53     shift
54 done
55
56 if [ "${SDK_FILE}" != "" ]; then
57
58     if [ ! -f "${SDK_FILE}" ]; then
59         echo "sdk file doesn't exist"
60         exit 1
61     fi
62
63     # Criteria to consider that SDK is a valid AGL sdk:
64     #  - shell and binary file type
65     #  - "$MARKER:$" string found
66     #  - md5sum match MD5VAL if MD5VAL is set
67
68     if ! file "${SDK_FILE}" |grep shell |grep -q binary ; then
69         echo "Not expected file type"
70         exit 2
71     fi
72
73     if ! grep -aq -m1 "^MARKER:$" "${SDK_FILE}"; then
74         echo "marker not found"
75         exit 4
76     fi
77
78     if [ "${MD5VAL}" != "" ]; then
79         if ! echo "${MD5VAL} ${SDK_FILE}" | md5sum --status -c -; then
80             echo "md5sum dit not match"
81             exit 5
82         fi
83     fi
84
85     filename=$(basename "${SDK_FILE}")
86     sdkUrl=file://${SDK_FILE}
87     sdkDate=$(stat -c %y "${SDK_FILE}")
88     sdkSize=$(/bin/ls -sh "${SDK_FILE}" |cut -d' ' -f1)
89
90 elif [ "${URL}" != "" ]; then
91
92     filename=$(basename "${URL}")
93     sdkUrl=${URL}
94     sdkDate=""
95     sdkSize=""
96
97 else
98     echo "--file or --url option must be set"
99     exit 1
100 fi
101
102 # assume that sdk name follow this format :
103 #  _PROFILE_-_COMPILER_ARCH_-_TARGET_-crosssdk-_ARCH_-toolchain-_VERSION_.sh
104 # for example:
105 #  poky-agl-glibc-x86_64-agl-demo-platform-crosssdk-corei7-64-toolchain-4.0.1.sh
106
107
108 if [[ "${filename}" != *"crosssdk"* ]]; then
109     echo "malformed sdk file name"
110     exit 6
111 fi
112
113 profile=$(echo "${filename}" | sed -r 's/(.*)-glibc.*/\1/')
114 version=$(echo "${filename}" | sed -r 's/.*toolchain-(.*).sh/\1/')
115 arch=$(echo "${filename}" | sed -r 's/.*crosssdk-(.*)-toolchain.*/\1/')
116 installPath=${SDK_ROOT_DIR}/${profile}/${version}/${arch}
117
118 [ "${profile}" = "" ] && { echo "profile not set"; exit 7; }
119 [ "${version}" = "" ] && { echo "version not set"; exit 8; }
120 [ "${arch}" = "" ] && { echo " arch not set"; exit 9; }
121
122 status="Not Installed"
123 if [ -d ${installPath} ]; then
124     envFile=$(find "${installPath}" -maxdepth 1 -name "${SDK_ENV_SETUP_FILENAME}")
125     [ "${envFile}" != "" ] && status="Installed"
126 fi
127
128 read -r -d '' res <<- EndOfMessage
129 {
130     "name":         "${profile}_${arch}_${version}",
131     "description":  "AGL SDK ${arch} (version ${version})",
132     "profile":      "${profile}",
133     "version":      "${version}",
134     "arch":         "${arch}",
135     "path":         "${installPath}",
136     "url":          "${sdkUrl}",
137     "status":       "${status}",
138     "date":         "${sdkDate}",
139     "size":         "${sdkSize}",
140     "md5sum":       "${MD5VAL}",
141     "setupFile":    "${envFile}"
142 }
143 EndOfMessage
144
145 echo "$res"
146 exit 0