#!/bin/sh

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

set -e

action=$1
artifact=$2
argument=$3

case "$action" in
download)
    if ! command -v dget >/dev/null 2>&1; then
        echo "dget not found, please install devscripts first" >&2
        exit 1
    fi

    mkdir "$argument"
    cd "$argument"
    dget -u -d "$artifact"
    ;;
verify)
    if ! command -v dscverify >/dev/null 2>&1; then
        echo "dscverify not found, please install devscripts first" >&2
        exit 1
    fi

    dscverify --nosigcheck "$artifact/$argument"
    ;;
materialize)
    output=$4

    if ! command -v dpkg-source >/dev/null 2>&1; then
        echo "dpkg-source not found, please install dpkg-dev first" >&2
        exit 1
    fi

    cd "$artifact"
    dpkg-source -x --no-copy "$argument" "$output"
    ;;
*)
    echo "unsupported dsc source action: $action" >&2
    exit 1
    ;;
esac
