#!/bin/bash

# create symlinks to all pictures, including subfolders
# depends on Macports gsha1sum (port install md5sha1sum)

# 2020-11-03 New

PICTURES="${HOME}/Pictures"
WALLPAPERS="${HOME}/Desktop/Wallpapers"

echo "pictures  : ${PICTURES}"
echo "wallpapers: ${WALLPAPERS}"

xdialog --yesno "Create symlinks in: ${WALLPAPERS}?" 8 80
case $? in
1 | 255)
  echo "No symlinks created"; exit 1;;
esac

if [ ! -d ${WALLPAPERS} ]; then
  mkdir ${WALLPAPERS}
fi

# https://stackoverflow.com/questions/9612090/how-to-loop-through-file-names-returned-by-find
# instead of the basename, a hash is used, to handle duplicate filenames
# the folders with MEDium and THUMBnail images will be skipped
find ${PICTURES} -name '*.jpg' -o -name '*.JPG' -exec $SHELL -c '
    SHA="`gsha1sum -b \"$0\" | cut -d \" \" -f 1`";
    if [[ ! "$0" == *\/MED\/* ]] && [[ ! "$0" == *\/THUMB\/* ]]; then
      ln -s "$0" "${1}/${SHA}.jpg";
    fi;' \
    {} ${WALLPAPERS} \;
 
exit 0
 
