#!/bin/bash

# Utility
# ############################################################

# Filter stdin to stdout.  Stdin must be wide-format (no wrap) LDIF;
# the output contains decoded bytes instead of base64.
decode_base64_in_ldif ()
{
    while read line; do
        if echo "$line" | grep -q '^[a-zA-Z0-9]*:: '; then
            attributename=$(echo "$line" | sed 's/:: .*$//')
            encodedattributevalue=$(echo "$line" | sed 's/^[^:]\+:: //')
            decodedattributevalue=$(echo "$encodedattributevalue" | base64 --decode)
            printedline="$attributename: $decodedattributevalue"
            # echo "attributename: >$attributename<" > /dev/stderr
            # echo "encodedattributevalue: >$attributevalue<" > /dev/stderr
            # echo "decodedattributevalue: >$decodedattributevalue<" > /dev/stderr
        else
            printedline="$line"
        fi
        echo "$printedline"
    done
}

# Main program
# ############################################################

ldapsearch -o ldif-wrap=no "$@" | decode_base64_in_ldif
