#!/bin/bash

## Copyright (C) 2021 - 2023 ENCRYPTED SUPPORT LP <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

## This script is used by package anon-shared-build-apt-sources-tpo.

set -e
set -o pipefail

source_file="$1"
target_file="$2"

if [ "$source_file" = "" ] || [ "$target_file" = "" ]; then
   echo "$0: ERROR: syntax: $0 source-file target-file" >&2
   exit 2
fi

if ! test -r "$source_file" ; then
   echo "$0: ERROR: source file '$source_file' does not exist or not readable!" >&2
   exit 3
fi

target_folder="${target_file%/*}"

if ! test -w "$target_folder" ; then
   echo "$0: ERROR: target_folder '$target_folder' not writeable!

By Debian default, root rights are required to write to folder '/etc/apt/trusted.gpg.d'.

Perhaps prepending 'sudo' would help? Consider:

sudo $0 $source_file $target_file" >&2
   exit 4
fi

gpg-dearmor "$source_file" "$target_file"

if ! test -r "$target_file" ; then
   echo "$0: ERROR: target_file '$target_file' not readable after writing!" >&2
   exit 6
fi

echo "$0: INFO: OK."
