#!/bin/bash

## Copyright (C) 2018 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

## Self-contained script. Don't add dependencies to other files. See man page
## for the purpose of this script.

## Dependency of derivative-maker.

echo "START: $0"
set -e
set -o pipefail
set -o nounset

## Code duplication. Copied from helper-scripts/usr/libexec/helper-scripts/strings.bsh.
check_variable_name() {
  ## Ensures a string is a valid Bash variable name.
  [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]
}

root_check() {
   if [ "$(id -u)" != "0" ]; then
      echo "ERROR: This must be run as root (sudo)!"
      exit 1
   else
      true "INFO: Script running as root."
   fi
}

set_default_variable() {
  local var_name var_content
  var_name="$1"
  var_content="$2"

  check_variable_name "${var_name}" || return 1
  if [ -z "${!var_name:-}" ]; then
    echo "INFO: Variable '$var_name' is already set? No. - Setting:
                $var_name='$var_content'"
    eval "$var_name='$var_content'"
    return 0
  fi
  echo "INFO: Variable '$var_name' is already set? Yes. - Already:
              $var_name='${!var_name}'"
}

root_check

set_default_variable apt_target_key_derivative "/usr/share/keyrings/derivative.asc"
set_default_variable apt_source_key_temp_folder_derivative "/var/lib/derivative-maker/repo-add-dist"
set_default_variable apt_source_key_derivative "/var/lib/derivative-maker/repo-add-dist/derivative.asc"

## Variables set by derivative-maker.
set_default_variable sources_list_target_folder_build_remote_derivative "/etc/apt/sources.list.d"
set_default_variable sources_list_target_build_remote_derivative "/etc/apt/sources.list.d/derivative.sources"

set_default_variable codename "trixie"
set_default_variable sources_list_build_remote_derivative "Types: deb
URIs: https://deb.kicksecure.com
Suites: $codename
Components: main contrib non-free
Enabled: yes
Signed-By: $apt_target_key_derivative"

mkdir --parents -- "$sources_list_target_folder_build_remote_derivative"
mkdir --parents -- "$apt_source_key_temp_folder_derivative"

cat <<EOF | tee -- "$apt_source_key_derivative" >/dev/null
-----BEGIN PGP PUBLIC KEY BLOCK-----

[encoded data, 8580 characters]
I3Fw6EJPjR87w2Rso3fGb9KQQgPpoMSIIqU1DP1uGA==
=zSJ+
-----END PGP PUBLIC KEY BLOCK-----
EOF

true "INFO: apt_source_key_derivative: $apt_source_key_derivative"
true "INFO: apt_target_key_derivative: $apt_target_key_derivative"

true "INFO: cp --verbose -- apt_source_key_derivative apt_target_key_derivative"
cp --verbose -- "$apt_source_key_derivative" "$apt_target_key_derivative"

## Debugging.
true "INFO: cat -- $apt_target_key_derivative + counting lines using wc -l"
cat -- "$apt_target_key_derivative" | wc -l || true

true "sources_list_target_build_remote_derivative: $sources_list_target_build_remote_derivative"

cat <<EOF | tee -- "$sources_list_target_build_remote_derivative" >/dev/null
## auto-generated by: $0
## variable 'sources_list_build_remote_derivative':
$sources_list_build_remote_derivative
EOF

## Debugging.
cat -- "$sources_list_target_build_remote_derivative" || true

echo "OK."
true "END: $0"
