Files

42 lines
1.5 KiB
Bash
Executable File

#!/bin/sh
# Stand-in for the AD-ProfilePicture-Sync.ps1 scheduled task, for testing the portal's picture
# feature. It does what the portal can see of the real script and nothing else: for every user
# it writes processed/<sam>.md5 holding the MD5 of the user's source file (or of the default
# image when there is none) in upper case hex with a trailing newline. It never touches AD.
#
# Usage: SOURCE=/srv/picture-test/source PROCESSED=/srv/picture-test/processed \
# USERS="ptest-user1 ptest-user2" ./picture-sync-standin.sh [seconds]
#
# Without a number of seconds it runs once. With one it repeats until stopped (Ctrl+C).
# The real task never compares MD5 values and rewrites every file on every run; so does this.
set -eu
: "${SOURCE:?set SOURCE to the test source folder}"
: "${PROCESSED:?set PROCESSED to the test processed folder}"
: "${USERS:?set USERS to the sAMAccountNames, separated by spaces}"
DEFAULT_IMAGE="${DEFAULT_IMAGE:-$PROCESSED/default.jpg}"
run_once() {
for sam in $USERS; do
file="$SOURCE/$sam.jpg"
[ -f "$file" ] || file="$DEFAULT_IMAGE"
if [ ! -f "$file" ]; then
echo "skip $sam: no $sam.jpg in the source folder and no default image" >&2
continue
fi
sum=$(md5sum "$file" | cut -d' ' -f1 | tr 'a-f' 'A-F')
printf '%s\n' "$sum" > "$PROCESSED/$sam.md5"
echo "$(date '+%H:%M:%S') $sam $sum"
done
}
if [ $# -eq 0 ]; then
run_once
else
while true; do
run_once
sleep "$1"
done
fi