| 1 |
#!/bin/bash |
| 2 |
# Generic anonymization script which would anonymize sid based on what it had |
| 3 |
# seen in the past or simply what the translation dict already has. |
| 4 |
|
| 5 |
set -eu |
| 6 |
|
| 7 |
debug() { |
| 8 |
: echo "DEBUG: $*" >&2 |
| 9 |
} |
| 10 |
|
| 11 |
# Translation file location |
| 12 |
# Store under .git by default to guarantee that it is not committed or locked by git-annex etc |
| 13 |
# But it might not fit some usecases where there is no .git |
| 14 |
anon_file_default=$(dirname "$0")/../.git/anon_sid_map.csv |
| 15 |
anon_file="${AC_ANON_FILE:-$anon_file_default}" |
| 16 |
anon_fmt="${AC_ANON_FMT:-%03d}" |
| 17 |
|
| 18 |
sid="$1" |
| 19 |
|
| 20 |
# harmonize since elderly awk on rolando seems to have no clue about IGNORECASE |
| 21 |
sid=$(echo "$sid" | tr '[:lower:]' '[:upper:]') |
| 22 |
|
| 23 |
debug "Using $anon_file to map $sid" |
| 24 |
|
| 25 |
if [ ! -e "$anon_file" ]; then |
| 26 |
touch "$anon_file" # initiate it |
| 27 |
fi |
| 28 |
|
| 29 |
# apparently heudiconv passes even those we provided in `-s` CLI option |
| 30 |
# to anonymization script. So, we will have to match those by our format |
| 31 |
# and then give back if matches. That would forbid plain remapping though if |
| 32 |
# original ids are in the same format, so some folks might want to disable that! |
| 33 |
sid_input_fmted=$(echo "$sid" | sed -e 's,^0*,,g' | xargs printf "$anon_fmt" 2>&1 || :) |
| 34 |
if [ "$sid" = "$sid_input_fmted" ]; then |
| 35 |
debug already in the anonymized format |
| 36 |
echo "$sid" |
| 37 |
exit 0 |
| 38 |
fi |
| 39 |
|
| 40 |
res=$(grep "^$sid," "$anon_file" | head -n 1) |
| 41 |
if [ -n "$res" ]; then |
| 42 |
ann="${res##*,}" |
| 43 |
debug "Found $ann in '$res'" |
| 44 |
else |
| 45 |
echo "We have all sids mapped already! Will not create a new one for $sid" >&2; exit 1 |
| 46 |
# need to take the latest one |
| 47 |
largest=$(sed -e 's/.*,//g' "$anon_file" | sort -n | tail -n1 | sed -e 's,^0*,,g') |
| 48 |
next=$((largest+1)) |
| 49 |
# shellcheck disable=SC2059 |
| 50 |
ann=$(printf "$anon_fmt" $next) |
| 51 |
debug "Found $largest and $next to get $ann, storing" |
| 52 |
echo "$sid,$ann" >> "$anon_file" |
| 53 |
fi |
| 54 |
echo "$ann" |