#!/bin/bash

extraBin=$(
    for ii in kpsewhich ; do
        /usr/bin/dirname $(/usr/bin/which $ii)
    done | xargs | tr -s ' ' ':'
        )

PATH=/usr/bin:/bin:$extraBin

{
    tt=$(for ii in TEXMFLOCAL TEXMFHOME ; do
             kpsewhich --var-value $ii
         done |
             tr -s '{}!,' '\n' |
             while read jj ; do
                 [ -d $jj ] && find $jj -not -type d -not -iname ls-R
             done | sort -u
      )
    for ii in $tt ; do
        jj=$(basename $ii)
        kk=$(kpsewhich -a $jj | grep -v $ii)
        echo
        if [ -n "$kk" ] ; then
            for ll in $kk ; do
                ls -l $ii $ll
                kpsewhich -a $ii $ll | xargs openssl md5 | awk -F"= " '{print $2, $1}'
                cmp $ii $ll
                if [ $? -eq 0 ] ; then
                    echo remove suggestion for $jj as rm $ii
                else
                    echo Check for $jj
                fi
            done
        else
            printf "maybe OK only %40s as %s\n" $jj $(kpsewhich -a $jj)
        fi
    done
}
