#!/bin/sh

# SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
set -e
set -x
workdir=$1
url=$2
digest=$3

mkdir -p "$workdir"
output=$(realpath "$workdir")
rm -r "$output" || true
name=$(basename "$url")
cachedir="/tmp/fetch-dsc-repo/$name"
mkdir -p "$cachedir" || true
cd "$cachedir"

if ! [ -f /usr/bin/dget ]
then
    echo "dget not found, please install devscripts first"
    exit 1;
fi

dget -u -d "$url"

actual_hash=$(sha256sum "$cachedir/$name" | awk '{print $1}')
if [ "X$actual_hash" != "X$digest" ]; then
    echo "digest mismatched: $actual_hash vs $digest"
    exit 1;
fi

dpkg-source -x --no-copy "$cachedir/$name" "$output"
