#!/usr/bin/env bash
# Desc: Gets gpg fingerprint but replaces uid email addresses with hashes
# Usage: bk-gpgfp-noemail 0xdc3469c9 74810B012346C9A6
# Depends: gpg, b2sum
# Version: 0.0.3

yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
try() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
check_resembles_gpg_fingerprint() {
    # Desc: Checks if input string looks like gpg fingerprint
    # Usage: check_resembles_gpg_fingerprint arg1
    # Input: arg1: string
    # Output: exit code: 0 if arg1 is fingerprint, 1 otherwise
    # Depends: yell(), die(), try()
    # Version: 0.0.1
    local pattern1 pattern2 input input_length

    # Check args
    if [[ $# -ne 1 ]]; then
	die "ERROR:Invalid number of arguments:$#";
    else
	input="$1";
    fi;

    ## Trim leading `0x`
    pattern1="(0x)(.*)";
    if [[ $input =~ $pattern1 ]]; then
	input="${input:2}";
	#yell "DEBUG:input:$input";
    fi;
    
    ## Check if char count multiple of 8
    input_length="${#input}";
    if [[ ! $(( input_length % 8 )) -eq 0 ]]; then
	yell "DEBUG:Length not a multiple of 8:$input_length:$input";
	return 1;
    fi;

    ## Check if hexadecimal
    pattern2="[0-9A-Fa-f]{8,40}";
    if [[ $1 =~ $pattern2 ]]; then
	#yell "DEBUG:is a fingerprint:$arg";
	return 0;
    else
	#yell "DEBUG:Not a fingerprint:$arg";
	return 1;
    fi;
}; # Checks if input string looks like gpg fingerprint
main() {
    # Desc: main program
    # Depends: gpg, b2sum
    # Ref/Attrib: [1] Manipulating Strings. https://tldp.org/LDP/abs/html/string-manipulation.html
    declare -a fingerprints
    local gpg_text gpg_text_buffer pattern email
    
    # Check arguments
    n=0;
    for arg in "$@"; do
	((n++));
	#yell "DEBUG:n:$n";
	#yell "DEBUG:arg:$arg";

	# Check if arg resembles gpg fignerprint
	if check_resembles_gpg_fingerprint "$arg"; then
	    #yell "DEBUG:Resembles a gpg fingerprint:$arg";
	    fingerprints+=("$arg");
	    #yell "DEBUG:fingerprints:$(declare -p fingerprints)";
	else
	    die "ERROR:Doesn't resemble a gpg fingerprint:$arg";
	fi;
    done;

    # Process fingerprints
    for arg in "${fingerprints[@]}"; do
	# Get gpg fingerprint text
	gpg_text="$(gpg --fingerprint --fingerprint "$arg" 2>&1)";
	#yell "DEBUG:gpg_text:$gpg_text";

	# Trim emails
	gpg_text_buffer="";
	while IFS= read -r line; do
	    #yell "DEBUG:line:$line";
	    ## Read $gpg_text line-by-line

	    # Skip lines that don't start with 'uid'
	    pattern="^uid";
	    if [[ ! $line =~ $pattern ]]; then
		#yell "DEBUG:line doesn't start with \"uid\":$line";
		gpg_text_buffer="$(printf "%s\n%s" "$gpg_text_buffer" "$line")";
		continue;
	    fi;

	    # Trim email from $line
	    ## Get email
	    email="$(expr match "$line" '.*\(<.*$\)')";
	    #yell "DEBUG:email1:$email";
	    email="${email%>*}";
	    #yell "DEBUG:email2:$email";
	    email="${email#*<}";
	    #yell "DEBUG:email3:$email";

	    ## Strip email from $line
	    line="${line% <*}"; # See [1]
	    #yell "DEBUG:line1:$line";
	    
	    ## Add hashed email if $email length non-zero
	    if [[ -n $email ]]; then
		email_hash="$(echo -n "$email" | b2sum -l32 | cut -d' ' -f1)"; # hash email using b2sum
		#yell "DEBUG:email_hash:$email_hash";
		line="$line <$email_hash>";
		#yell "DEBUG:line3:$line";
	    fi;
	    
	    ## Append $line to $gpg_text_buffer
	    gpg_text_buffer="$(printf "%s\n%s" "$gpg_text_buffer" "$line")";
	done < <(echo "$gpg_text");
	echo "$gpg_text_buffer";
    done;
}; # main program

main "$@";

# Author: Steven Baltakatei Sandoval
# License: GPLv3+

# Dependencies

#1
    # b2sum (GNU coreutils) 8.32
    # Copyright (C) 2020 Free Software Foundation, Inc.
    # License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
    # This is free software: you are free to change and redistribute it.
    # There is NO WARRANTY, to the extent permitted by law.

    # Written by Padraig Brady and Samuel Neves.

#2
    # gpg (GnuPG) 2.2.20
    # libgcrypt 1.8.8
    # Copyright (C) 2020 Free Software Foundation, Inc.
    # License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
    # This is free software: you are free to change and redistribute it.
    # There is NO WARRANTY, to the extent permitted by law.

    # Home: /home/baltakatei/.gnupg
    # Supported algorithms:
    # Pubkey: RSA, ELG, DSA, ECDH, ECDSA, EDDSA
    # Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,
    # 	    CAMELLIA128, CAMELLIA192, CAMELLIA256
    # Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
    # Compression: Uncompressed, ZIP, ZLIB, BZIP2

#3
    # GNU bash, version 5.1.8(1)-release (x86_64-pc-linux-gnu)
    # Copyright (C) 2020 Free Software Foundation, Inc.
    # License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

    # This is free software; you are free to change and redistribute it.
    # There is NO WARRANTY, to the extent permitted by law.