#!/bin/sh

# SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
set -e

outputdir=$1
url=$2
digest=$3
cachedir=$4

# Check command tools 
if ! command -v wget
then
    echo "wget not found, please install wget first"
    exit 1;
fi

# Clean up old directorie and create parent directory
mkdir -p "$outputdir"
rm -r "$outputdir"
# Check cache
if [ -d "$cachedir/archive_$digest" ]; then
    cp -r "$cachedir/archive_$digest" "$outputdir"
    exit;
fi
# Create a temporary directory
tmpdir=$(mktemp -d)
# Change directory
cd "$tmpdir"
# Download dsc and tar
name=$(basename "$url")
wget "$url" -O "$name"
# Compare digest
actual_hash=$(sha256sum "$name" | awk '{print $1}')
if [ "X$actual_hash" != "X$digest" ]; then
    echo "File SHA256 digest is $actual_hash, expected $digest"
    exit 1;
fi
# Extract the archive
mkdir -p "$cachedir/tmp_$digest"
tar --no-same-owner -xvf "$name" -C "$cachedir/tmp_$digest"
mv "$cachedir/tmp_$digest" "$cachedir/archive_$digest"
cp -r "$cachedir/archive_$digest" "$outputdir"
# Clean temporary directory
rm -r "$tmpdir"
