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