style(unitproc/bkt-bkunzip):Treat bkunzip function as template
[BK-2020-03.git] / user / bk-export-min-pubkeys.sh
CommitLineData
1da0db33
SBS
1#!/usr/bin/env bash
2# Desc: Export each pubkey to a separate file
3# Usage: bk-export-min-pubkeys.sh
b605b5b5 4# Version: 0.0.2
1da0db33
SBS
5
6time_now="$(date +%Y%m%dT%H%M%S%z)";
7dir_out="./$time_now..pubkeys";
8
9yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
10die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
11try() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
12gpg_get_pubkey() {
13 # Desc: Output ascii-armored gpg pubkey via stdout
14 # Input: arg1: pgp long id
15 # Output: stdout: ascii-armored minimal-signature pgp key
16 # Example: gpg_get_pubkey 0xa0a295abdc3469c9
17 # Depends: gpg 2.2.12
18 local longid output;
19 longid="$1";
20
21 output="$(gpg --export --armor --export-options export-minimal "$longid")";
22 echo "$output";
23} # Output ascii-armored gpg pubkey via stdout
24main() {
25 # Create list of primary key fingerprints
26 list_longid="$(gpg --list-keys | grep "^pub" | awk '{print $2}' | cut -d'/' -f2)";
27
28 # Create output dir
29 path_out="$(readlink -f "$dir_out")";
b605b5b5 30 #yell "DEBUG:path_out:$path_out";
1da0db33
SBS
31 if [[ ! -d "$path_out" ]]; then
32 mkdir -p "$path_out";
33 yell "NOTICE:Creating output directory:$path_out";
34 fi;
35
36 # Iterate through list
37 while read -r longid; do
38 yell "STATUS:Exporting $longid";
39
40 # Export file
41 pubkey_ascii="$(try gpg_get_pubkey "$longid")";
b605b5b5 42 #yell "DEBUG:pubkey_ascii:$pubkey_ascii";
1da0db33
SBS
43 echo "$pubkey_ascii" > "$path_out"/"$longid".asc
44
45 done < <( echo "$list_longid" );
46}; # main program
47
48main "$@";
49
50# Author: Steven Baltaktei Sandoval
51# License: GPLv3+