#!/usr/bin/env bash
#
# Author: Lilian BESSON
# Email: Lilian.BESSON[AT]ens-cachan[DOT]fr
# Date: 01/05/2024
# Web version: https://perso.crans.org/besson/bin/generateglisse.sh
# Web version (2): https://github.com/Naereen/bin/src/master/generateglisse.sh
# Web version (2): https://bitbucket.org/lbesson/bin/src/master/generateglisse.sh
#
# Auto generate an 'index.html' page to show photos with glisse.js
# Also generate a 'index.htm' page, which works on Windows by fetching the CSS and JS online (https://perso.crans.org/besson/_static/) and not locally (http://0.0.0.0/).
#
# FUTURE: use http://dimsemenov.com/plugins/magnific-popup/documentation.html#gallery instead, seems better.
#
# A demo is here : https://perso.crans.org/besson/generateglisse.sh/
# Last version is here : https://perso.crans.org/besson/bin/generateglisse.sh
# with stylesheets and templates is here : https://perso.crans.org/besson/bin/generateglisse/
#
version='1.2'
GenerateGlisse() {
# Go to the directory.
p=$(pwd)
if [ -d "$*" ]; then cd "$*"; fi
echo -e "Working for the directory ${magenta}$(pwd)${white}."
# if [ -f index.html ]; then
# cp -vf index.html $(tempfile)
# mv -f index.html index.html~
# fi
# Header
currentdir="$(basename "$(pwd -P)")"
currentdir="${currentdir#./}"
cat ~/bin/generateglisse/header.html \
| sed s%VERSION%"$version"% \
| sed s%CURRENTDIR%"$currentdir"% \
| sed s%VERSION%"$version"% \
| sed s_DATE_"$(date +" %d %b %Y, à %Hh:%Mm:%Ss")"_ > index.html
# Listing of sub directories (without .. ou .)
targets=$(find . -maxdepth 1 -type d -iname '*'[A-Za-z]'*' 2>/dev/null)
targets=${targets//' '/%20}
targets=${targets//'?'/%3F}
targets="${targets//'&'/&}"
echo -e "$red$targets$white" > /dev/stderr
if [ "X$targets" != "X" ]; then
nombre=$(echo "$targets" | grep -c -o ./)
if (( nombre > 1 )); then
echo -e "
Liste des sous-dossiers (au nombre de $nombre) :
" >> index.html
else
echo -e "
Liste du sous-dossier :
" >> index.html
fi
echo -e "
" >> index.html
echo -e "- .. (dossier parent)
" >> index.html
for d in $targets; do
dossier=${d//'%20'/ }
dossier=${dossier//'%3F'/?}
dossier=${dossier//'&'/&}
subphotos=$(find "${dossier}" -maxdepth 1 -type f -iname '*'.jpg -o -iname '*'.png -o -iname '*'.gif -o -iname '*'.jpeg 2>/dev/null)
nombrephotos=$(echo "$subphotos" | grep -c -o "\(jpg\|JPG\|png\|PNG\|gif\|GIF\|jpeg\|JPEG\)")
subdirs=$(find "${dossier}" -maxdepth 1 -type d -iname '*'[A-Za-z]'*' 2>/dev/null)
nombredirs=$(echo "$subdirs" | grep -c -o ./)
nombredirs=$(( nombredirs / 2 )) # does not count itself ? FIXME
# Adapt what to print according to the number of subdirs and photos
if (( nombrephotos > 1 )); then
if (( nombredirs > 1 )); then
echo -e "- ${dossier} (${nombrephotos} photos, ${nombredirs} sous-dossiers)
" >> index.html
elif (( nombredirs == 1 )); then
echo -e "- ${dossier} (${nombrephotos} photos, 1 sous-dossier)
" >> index.html
else
echo -e "- ${dossier} (${nombrephotos} photos)
" >> index.html
fi
elif (( nombrephotos == 1 )); then
if (( nombredirs > 1 )); then
echo -e "- ${dossier} (1 photo, ${nombredirs} sous-dossiers)
" >> index.html
elif (( nombredirs == 1 )); then
echo -e "- ${dossier} (1 photo, 1 sous-dossier)
" >> index.html
else
echo -e "- ${dossier} (1 photo)
" >> index.html
fi
else
if (( nombredirs > 1 )); then
echo -e "- ${dossier} (${nombredirs} sous-dossiers)
" >> index.html
elif (( nombredirs == 1 )); then
echo -e "- ${dossier} (1 sous-dossier)
" >> index.html
fi
fi
echo -e "For ${u}${dossier}${U}: ${yellow}${nombrephotos}${white} photos, ${magenta}${nombredirs}${white} subdirs."
done
echo -e "
" >> index.html
fi
# Listing of photos (and gallery) (with glisse.js)
targets=$(find . -maxdepth 1 -type f -iname '*'.jpg -o -iname '*'.png -o -iname '*'.gif -o -iname '*'.jpeg 2>/dev/null)
if [ "X${targets//'%20'/}" != "X" ]; then
targets="${targets//' '/%20}"
targets="${targets//'?'/%3F}"
targets="${targets//'&'/&}"
nombre=$(echo "$targets" | grep -c -o "\(jpg\|JPG\|png\|PNG\|gif\|GIF\|jpeg\|JPEG\)")
# Print the list of files.
if (( nombre > 1 )); then
echo -e "
Liste des photos (au nombre de $nombre) :
" >> index.html
else
echo -e "
Une seule photo :
" >> index.html
fi
# I prefer to use the gallery style, it is REALLY better than the stack one (and less cpu consuming)
echo -e "
" >> index.html
# echo -e "" >> index.html
for i in $targets; do
title=${i//'%20'/ }
title=${title//'%3F'/?}
# Other metadata about the file.
# metadata=$(file -b -p "$title")
metadata=$(identify "$title")
# echo "metadata = $metadata"
size=$(expr "$metadata" : ".*\( [0-9]\+x[0-9]\+\)")
size=${size# }
# echo "size = $size"
# read
# echo du: $(du -h "$title")
filesize=$(du -h "$title" | grep -o "^[,0-9]\+[KMGT]b\?")
# echo "filesize = $filesize"
# read
title=${title#./}
# Remove extension
title=${title%.jpg}
title=${title%.JPG}
title=${title%.gif}
title=${title%.png}
title=${title%.PNG}
title=${title%.jpeg}
title=${title%.JPEG}
# Try to find the date of the photo. title=2013-11-19_14-06-41_657
# Could use exiftool, but it is REALLY too slow !
# jour=$(echo -e "${title}" | grep -o "20[0-9][0-9]-[0-9][0-9]-[0-9][0-9]")
jour=${title:0:10}
#jour=${jour:-2013-11-30}
# heure=$(echo -e "${title}" | grep -o "_[0-9][0-9]-[0-9][0-9]-[0-9][0-9]_" | tr - : )
heure=${title:10:9}
heure=${heure//-/:}
heure=${heure//_/}
#heure=${heure:-00:00:00}
title=${title//_/ }
# Check if jour, heure are valid, otherwise use the picture's name to title
if ( echo -e "${jour}T${heure}Z" | grep -o "20[0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9]Z" >/dev/null); then
echo -e "- Le ${jour} à ${heure}.
" >> index.html
echo -e " Adding ${u}${i}${U}, jour: ${blue}${jour}${white}, heure: ${black}${heure}${white}, size: ${magenta}${size}${white}, filesize: ${cyan}${filesize}${white}"
else
echo -e "- « ${title} »
" >> index.html
echo -e " Adding ${u}${i}${U}, title: ${blue}${title}${white}, size: ${magenta}${size}${white}, filesize: ${cyan}${filesize}${white}"
fi
done
echo -e "
" >> index.html
fi
# Footer starts with a JS line and then a closed balise
pathtobechanged="$(pwd)"
pathtobechanged="${pathtobechanged#/home/lilian/Music/}"
cat ~/bin/generateglisse/footer.html \
| sed s%PATHTOBECHANGED%"$pathtobechanged"% \
>> index.html
echo -e "index.html have been generated in ${magenta}$(pwd)${white}."
# Come back.
cd "$p"
}
targets=$(find . -type d)
targets=${targets//' '/%20}
targets=${targets//'?'/%3F}
echo -e "${blue}${targets}${white}" | less -r
# To find every concerned directory
for i in $targets; do
direction=${i//'%20'/ }
direction=${direction//'%3F'/?}
echo -e "For the directory ${blue}'${direction}'${white}........."
( time GenerateGlisse "${direction}" ) 2>&1 | tee "${direction}/generateglisse.log"
grep -a "^real[0-9a-z \t\.]*" "${direction}/generateglisse.log" > "${direction}/generateglisse.time"
# Coloring the log.
cat "${direction}/generateglisse.log" \
| sed s_"./"_"http://./"_ \
| sed s%"/home/lilian/photos/"%"http://0.0.0.0/lns_photos/"% \
| ansi2html \
| sed s_"http://./"_"./"_ \
| sed s_"http://./"_"./"_ \
| sed s_"http://.http://"_"http://"_ \
| sed s_"http://.http://"_"http://"_ \
> "${direction}/generateglisse.html"
cp "${direction}/index.html" "${direction}/index.html~"
cat "${direction}/index.html~" \
| sed s_TIMESPENT_"$(cat "${direction}/generateglisse.time")"_ \
> "${direction}/index.html"
# Make the index.htm page for Windows
cat "${direction}/index.html" \
| sed s_"0\.0\.0\.0"_"perso.crans.org/besson/"_g \
| sed s_"/index.html"_"/index.htm"_g \
| sed s_"Programme pour GNU/Linux"_"Page pour Windows®"_ \
> "${direction}/index.htm"
done
# END