1#!/usr/bin/env bash
2# SPDX-License-Identifier: LGPL-2.1-or-later
3
4# Download and extract coverity tool
5
6set -e
7set -o pipefail
8
9# Environment check
10if [ -z "$COVERITY_SCAN_TOKEN" ]; then
11    echo >&2 'ERROR: COVERITY_SCAN_TOKEN must be set'
12    exit 1
13fi
14
15# Use default values if not set
16PLATFORM="$(uname)"
17TOOL_BASE="${TOOL_BASE:-/tmp/coverity-scan-analysis}"
18TOOL_ARCHIVE="${TOOL_ARCHIVE:-/tmp/cov-analysis-${PLATFORM}.tgz}"
19TOOL_URL="https://scan.coverity.com/download/${PLATFORM}"
20
21# Make sure wget is installed
22sudo apt-get update && sudo apt-get -y install wget
23
24# Get coverity tool
25if [ ! -d "$TOOL_BASE" ]; then
26    # Download Coverity Scan Analysis Tool
27    if [ ! -e "$TOOL_ARCHIVE" ]; then
28        echo -e "\033[33;1mDownloading Coverity Scan Analysis Tool...\033[0m"
29        wget -nv -O "$TOOL_ARCHIVE" "$TOOL_URL" --post-data "project=$COVERITY_SCAN_PROJECT_NAME&token=$COVERITY_SCAN_TOKEN"
30    fi
31
32    # Extract Coverity Scan Analysis Tool
33    echo -e "\033[33;1mExtracting Coverity Scan Analysis Tool...\033[0m"
34    mkdir -p "$TOOL_BASE"
35    pushd "$TOOL_BASE"
36    tar xzf "$TOOL_ARCHIVE"
37    popd
38fi
39
40echo -e "\033[33;1mCoverity Scan Analysis Tool can be found at $TOOL_BASE ...\033[0m"
41