#!/bin/bash
 
main() {
    local missing_photo="02648b742ef6c77c1c2b7fc20c3aeadc"
    local username=
    ldapsearch -x -H ldap://ldap.corp.velocix.com:389 \
        -b ou=users,dc=cachelogic,dc=com -o ldif-wrap=no \
        -LLL jpegPhoto | while read line
    do
        if [[ "$line" =~ ^dn:\ uid=([a-zA-Z0-9\._-]+) ]] ; then
            username="${BASH_REMATCH[1]}"
        elif [[ "$line" =~ ^jpegPhoto:\ (.+) ]] ; then
            local md5sum="$(md5sum <<< "${BASH_REMATCH[1]}"|cut -d' ' -f1)"
            if [[ "$md5sum" == "$missing_photo" ]] ; then
                continue
            fi
            base64 -d <<< "${BASH_REMATCH[1]}" > "$username.jpg"
            echo "$username.jpg"
        fi
    done
 
    mkdir -p enabled disabled
    rm -f enabled/*.jpg disabled/*.jpg
    for username in $(disabled_users) ; do
        if [[ -e "$username.jpg" ]] ; then
            echo "$username is disabled"
            ln -s "../$username.jpg" "disabled/$username.jpg"
        fi
    done
    for file in *.jpg ; do
        if ! [[ -e "disabled/$file" ]] ; then
            echo "${file%.jpg} is enabled"
            ln -s "../$file" "enabled/$file"
        fi
    done
 
    echo "Generating montage images."
    montage -label '%f' *.jpg -tile 10x -frame 5 ../montage.jpg
    montage -label '%f' disabled/*.jpg -tile 10x -frame 5 ../montage-disabled.jpg
    montage -label '%f' enabled/*.jpg -tile 10x -frame 5 ../montage-enabled.jpg
}
 
main "$@"