#! /bin/bash

#*
# Map students' directories between those made from the university
# list, and those obtained from the Moodle downloads. The mapping will
# transform student's names to upper-case ASCII letters, without
# diacritics and spaces, and use these strings as matching keys.

# USAGE:
#   $0 unpacked/1st-attempt .
#**

set -ue
#set -x

MOODLE_DIR="$1"
TARGET_DIR="$2"

join -t "$(echo -e "\t")" \
     <(find "${TARGET_DIR}" \
            -maxdepth 1 \
            -mindepth 1 \
            -name '[A-Z]*' \
            -print \
       | perl -CS \
              -MUnicode::Normalize \
              -ne 'chomp;
                   $d = $_;
                   s/^.*\///; 
                   s/\s/_/g; 
                   s/-/_/g;
                   s/__*/_/g;
                   $_ = NFD($_);
                   s/[^\0-\x80]//g;
                   print uc, "\t", $d, "\n"' \
       | sort -t "$(echo -e "\t")" -k 1,1) \
     <(find "${MOODLE_DIR}" \
            -maxdepth 1 \
            -mindepth 1 \
            -print \
       | perl -CS \
              -MUnicode::Normalize \
              -ne 'chomp;
                   $d = $_;
                   s/^.*\///;
                   s/^(.*?[_\s].*?)_.*/$1/;
                   s/\s/_/g;
                   s/-/_/g;
                   s/__*/_/g;
                   $_ = NFD($_);
                   s/[^\0-\x80]//g;
                   print uc, "\t", $d, "\n"' \
        | sort -t "$(echo -e "\t")" -k 1,1) \
| awk -F"\t" '{print $2"\t"$3}'
