#!/usr/bin/env bash
#
# Author: Lilian BESSON
# Email: Lilian.BESSON[AT]ens-cachan[DOT]fr
# Date: 16/06/2017
# Web version: http://perso.crans.org/besson/bin/generatejplayer.sh
# Web version (2): https://bitbucket.org/lbesson/bin/src/master/generatejplayer.sh
#
# Auto generate an 'index.html' page to show and play music with jplayer.
# Also generate a 'index.htm' page, which works on Windows by fetching the CSS and JS online (http://perso.crans.org/besson/_static/) and not locally (http://0.0.0.0/).
#
# A demo is here : http://perso.crans.org/besson/generatejplayer.sh
# Last version is here : http://perso.crans.org/besson/bin/generatejplayer.sh
# with stylesheets and templates is here : http://perso.crans.org/besson/bin/generatejplayer/
#
version='1.9'
GeneratejPlayer() {
# Go to the directory.
p="$(pwd)"
if [ -d "$*" ]; then cd "$*"; fi
pathtobechanged="$(pwd)"
pathtobechanged="${pathtobechanged// /%20}"
echo -e "Working for the directory ${cyan} : ${pathtobechanged}${white}."
# Header
currentdir="(basename "$(pwd -P)")"
currentdir="${currentdir#./}"
cat ~/bin/generatejplayer/header.html \
| sed s%VERSION%"$version"% \
| sed s%CURRENTDIR%"$currentdir"% \
| sed s_DATE_"$(date +"%d %b %Y, à %Hh:%Mm:%Ss")"_ > index.html
# Listing of music (and playlist) (with jquery.jplayer.js)
targets=$(find . -maxdepth 1 -type f -iname '*'.mp3 -o -iname '*'.ogg -o -iname '*'.wav -o -iname '*'.wma 2>/dev/null)
taille="$(du -kc -h ./*.mp3 2>/dev/null| tail -n 1 | grep -o -m 1 "^[0-9][,0-9]*[KMG]\?")"
echo -e "Total size for all musics (*.mp3) = $taille."
if [ "X${targets//'%20'/}" != "X" ]; then
# New: a search bar for the playlist
echo -e "
Liste des sous-dossiers (au nombre de $nombre, $taille) :
" >> index.html
else
echo -e "
Un sous-dossier ($taille) :
" >> index.html
fi
echo -e "
" >> index.html
# New: a search bar for the list of folders
echo -e "\n
Search through the list of folder?\n (Thanks to the QuickSearch jQuery plugin)\n
\n
\n
\n" >> index.html
echo -e "- .. (dossier parent)
" >> index.html
# For every sub directories
for d in $targets; do
dossier="${d//'%20'/ }"
dossier=${dossier//'&'/&}
taille="$(du -c -kh "${dossier}/" | tail -n 1 | grep -o -m 1 "^[0-9][,0-9]*[KMG]\?")"
echo -e "Total size for the sub-directory ${dossier}/ = $taille."
subchansons=$(find "${dossier}" -maxdepth 1 -type f -iname '*'.mp3 -o -iname '*'.ogg -o -iname '*'.wav -o -iname '*'.wma 2>/dev/null)
nombrechansons=$(echo "$subchansons" | tr '[:upper:]' '[:lower:]' | grep -c -o "\(mp3\|ogg\|wav\|wma\)")
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
dossier="${dossier%/}"
dossier="${dossier#./}"
dossier="${dossier//_/ }"
d="${d}/index.html"
# Adapt what to print according to the number of subdirs and chansons
if (( nombrechansons > 1 )); then
if (( nombredirs > 1 )); then
echo -e "- ${dossier} (${nombrechansons} chansons, ${nombredirs} sous-dossiers, $taille)
" >> index.html
elif (( nombredirs == 1 )); then
echo -e "- ${dossier} (${nombrechansons} chansons, 1 sous-dossier, $taille)
" >> index.html
else
echo -e "- ${dossier} (${nombrechansons} chansons, $taille)
" >> index.html
fi
elif (( nombrechansons == 1 )); then
if (( nombredirs > 1 )); then
echo -e "- ${dossier} (1 chanson, ${nombredirs} sous-dossiers, $taille)
" >> index.html
elif (( nombredirs == 1 )); then
echo -e "- ${dossier} (1 chanson, 1 sous-dossier, $taille)
" >> index.html
else
echo -e "- ${dossier} (1 chanson, $taille)
" >> index.html
fi
else
if (( nombredirs > 1 )); then
echo -e "- ${dossier} (${nombredirs} sous-dossiers, $taille)
" >> index.html
elif (( nombredirs == 1 )); then
echo -e "- ${dossier} (1 sous-dossier, $taille)
" >> index.html
fi
fi
echo -e "For ${u}${dossier}${U}: ${yellow}${nombrechansons}${white} chansons, ${magenta}${nombredirs}${white} subdirs."
#echo -e "- ${title}/index.html
" >> index.html
# FIXME here we force the destination to be ${d}/index.html
done
echo -e "
" >> index.html
fi
# To include pictures like AlbumArtSmall.jpg, AlbumCover.jpg, Folder.jpg (FIXME still experimental)
pictures=$(find ./ -maxdepth 1 -type f -iname Folder'*'.jpg -o -iname Album'*'.jpg 2>/dev/null)
echo -e "${cyan}Potential AlbumArt: ${yellow}${u}${pictures}${U}${white}..."
# Find the images
for image in $pictures; do
metadata=$(identify "$image")
size=$(expr "$metadata" : ".*\( [0-9]\+x[0-9]\+\)")
size="${size# }"
image=${image//' '/%20}
echo -e "${magenta}Size for the AlbumArt ${cyan}${image}: ${yellow}${u}${size}${U}${white}..."
done
# Add the last one. FIXME update height widht according to the right size.
echo -e "
\
\n\t
" >> index.html
# Conclude
echo -e "\t balise
pathtobechanged="$(pwd)"
pathtobechanged="${pathtobechanged#/home/lilian/Music/}"
cat ~/bin/generatejplayer/footer.html \
| sed s%PATHTOBECHANGED%"$pathtobechanged"% \
>> index.html
pathtobechanged="$(pwd)"
pathtobechanged="${pathtobechanged// /%20}"
echo -e "index.html have been generated in ${cyan} : ${pathtobechanged}${white}."
# Come back.
cd "$p"
}
# Find every folder. Warning: in a folder like / or /home/user/ the script can run for a VERY LONG TIME !
pathtobechecked="$(pwd)"
pathtobechecked="${pathtobechecked#/home/lilian/Music}"
if [ ! /home/lilian/Music/"${pathtobechecked}" = "$(pwd)"/ ]; then
echo -e "${red} Warning: seems to be launched from elsewhere than ${u}/home/lilian/Music/${U} (and that's bad).${white}"
read -t 10 -p "Cancel ? [Y/n] " ANSWER
case "$ANSWER" in
n*)
echo -e "${red}Not cancelling...${white}"
;;
*)
echo -e "${red}Cancelling...${white}"
exit 1
;;
esac
fi
targets=$(find . -type d)
targets=${targets//' '/%20}
echo -e "${blue}${targets}${white}"
# TO find every concerned directory
for i in $targets; do
direction=${i//'%20'/ }
echo -e "For the directory ${blue}'${direction}'${white}........."
( time GeneratejPlayer "${direction}" ) 2>&1 | tee "${direction}/generatejplayer.log"
grep -a "^real[0-9a-z \t\.]*" "${direction}/generatejplayer.log" > "${direction}/generatejplayer.time"
# Coloring the log.
cat "${direction}/generatejplayer.log" \
| sed s_"./"_"http://./"_ \
| sed s_"/home/lilian/Music/"_"http://0.0.0.0/music/"_ \
| ansi2html -a \
| sed s_"http://./"_"./"_g \
| sed s_"http://.http://"_"http://"_g \
> "${direction}/generatejplayer.html"
cp "${direction}/index.html" "${direction}/index.html~"
cat "${direction}/index.html~" \
| sed s_TIMESPENT_"$(cat "${direction}/generatejplayer.time")"_ \
> "${direction}/index.html"
rm -f "${direction}/index.html~" "${direction}/generatejplayer.time"
# 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 of generatejplayer.sh