#!/bin/bash
# location : /var/www/html/bulle/web/pj/previews_bulles/bash_thumbs.sh
# projet : https://liens.vincent-bonnefille.fr
# source : https://legacy.imagemagick.org/Usage/thumbnails/
# note : petit script pour convertir mes images en miniatures
#
# FOLDER 
# /var/www/html/bulle/web/pj/previews_bulles/
#
# EXE
# /var/www/html/bulle/web/pj/previews_bulles/bash_thumbs.sh
# ALIAS
# prevbulle
#
# BETA devenue
# /var/www/nodejs_apps/geo_ip/public/bash_thumbs_captures.sh (etc)

URL="https\:\/\/bulle\.vincent\-bonnefille\.fr\/pj\/previews\_bulles\/thumbs\/"
URLORIGINAL="https\:\/\/bulle\.vincent\-bonnefille\.fr\/pj\/previews\_bulles\/originals\/"
i=0

cd /var/www/html/bulle/web/pj/previews_bulles/
echo
pwd

sleep 0.5

echo
# sleep 10

# echo $1
echo

i=0

if [ "$1" ]
then
    for arg in "$@"
    do
    echo
    echo \[\[ "$arg"
	num=$arg
	echo
	echo ----
	echo
		fp=$(echo $(dirname "$num"))
		echo $fp
		fn=$(echo $(basename "$num"))
		echo $fn
		ext='(.*)\.(jpg|jpeg|png|gif)$'
		[[ "$fn" =~ $ext ]] 
		# fnonly=${BASH_REMATCH[1]}
		# echo -- $fnonly
		extis=${BASH_REMATCH[2]}
		echo -- $extis
		fnfix=$(echo ${fn// /_})
		echo fnfix -- $fnfix
		# sleep 30
		cp "$num" ./${fnfix}
		sudo chown pi:www-data ./${fnfix}
		sleep 0.3
		cp -p ./${fnfix} ./originals/${fnfix}
		# sleep 1
			fnfixreg='(.*)\.(jpg|jpeg|png|gif)$'
			[[ "$fnfix" =~ $fnfixreg ]] 
			fnfixname=${BASH_REMATCH[1]}
			echo fnfixname -- $fnfixname
			mogrify -format gif -path thumbs -thumbnail 300x300 ${fnfix}
			### NB ICI pas de conversion thumbnail (todo add)
			echo
			echo $i
			ls ${fnfixname}*
			echo
			sudo chown pi:www-data "./thumbs/${fnfixname}.gif"
			((i=i+1))
    done
	sleep 3

else

	notify-send-all -t 1000 -u low "Go PrevBulle" "/var/www/html/bulle/web/\n/pj/previews_bulles/bash_thumbs.sh"
	echo "Go prevbulle (sans fichier spécifique, en \$1)"

	cd /var/www/html/bulle/web/pj/previews_bulles/
	echo
	echo "-- PNG --"
	count=`ls -1 *.png 2>/dev/null | wc -l`
	if [ $count != 0 ]
	then 
		for num in *.png;
		do 
    	fnfix=$(echo ${num// /_})
		ext0=$(echo "$num" | rev | cut -d"." -f1 | rev)
		echo $ext0
		# sleep 10
		echo
		echo ${num}
		echo ${fnfix}
		echo
		mv -f "${num}" ./${fnfix}
		sudo chown pi:www-data ./${fnfix}
		sleep 0.3
		mogrify -format gif -path thumbs -thumbnail 300x300 ${fnfix}
		sudo rm ./originals/${fnfix}
		sudo mv -f ./${fnfix} ./originals/${fnfix}
		# convert "$p" "${fnfix}.gif" && echo "-- ${fnfix}.png" && mv "${p}.png" "./originals/${fnfix}.png";
		((i=i+1))
		done;
		notify-send-all -t 3000 -u low "PNG TO JPG" "🍰"
	else
		echo "0"
	fi 




	echo
	echo "-- JPG --"
	count=`ls -1 *.jpg 2>/dev/null | wc -l`
	if [ $count != 0 ]
	then 
		for num in *.jpg;
		do 
    	fnfix=$(echo ${num// /_})
		ext0=$(echo "$num" | rev | cut -d"." -f1 | rev)
		echo $ext0
		# sleep 10
		echo
		echo ${num}
		echo ${fnfix}
		echo
		mv -f "${num}" ./${fnfix}
		sudo chown pi:www-data ./${fnfix}
		sleep 0.3
		mogrify -format gif -path thumbs -thumbnail 300x300 ${fnfix}
		sudo rm ./originals/${fnfix}
		sudo mv -f ./${fnfix} ./originals/${fnfix}
		# convert "$p" "${fnfix}.gif" && echo "-- ${fnfix}.jpg" && mv "${p}.jpg" "./originals/${fnfix}.jpg";
		((i=i+1))
		done;
		notify-send-all -t 3000 -u low "JPG" "🍌"
	else
		echo "0"
	fi 




	echo
	echo "-- JPEG --"
	count=`ls -1 *.jpeg 2>/dev/null | wc -l`
	if [ $count != 0 ]
	then 
		for num in *.jpeg;
		do 
    	fnfix=$(echo ${num// /_})
		ext0=$(echo "$num" | rev | cut -d"." -f1 | rev)
		echo $ext0
		# sleep 10
		echo
		echo ${num}
		echo ${fnfix}
		echo
		mv -f "${num}" ./${fnfix}
		sudo chown pi:www-data ./${fnfix}
		sleep 0.3
		mogrify -format gif -path thumbs -thumbnail 300x300 ${fnfix}
		sudo rm ./originals/${fnfix}
		sudo mv -f ./${fnfix} ./originals/${fnfix}
		# convert "$p" "${fnfix}.gif" && echo "-- ${fnfix}.jpeg" && mv "${p}.jpeg" "./originals/${fnfix}.jpeg";
		((i=i+1))
		done;
		notify-send-all -t 3000 -u low "JPEG" "🍐"
	else
		echo "0"
	fi 



	echo
	echo "-- GIF --"
	count=`ls -1 *.gif 2>/dev/null | wc -l`
	if [ $count != 0 ]
	then 
		for num in *.gif;
		do 
    	fnfix=$(echo ${num// /_})
		ext0=$(echo "$num" | rev | cut -d"." -f1 | rev)
		echo $ext0
		# sleep 10
		echo
		echo ${num}
		echo ${fnfix}
		echo
		mv -f "${num}" ./${fnfix}
		sudo chown pi:www-data ./${fnfix}
		sleep 0.3
		mogrify -format png -path originals ${fnfix}
		sudo mv -f ./${fnfix} ./thumbs/${fnfix}
		# convert "$p" "${fnfix}.gif" && echo "-- ${fnfix}.gif" && mv "${p}.gif" "./originals/${fnfix}.gif";
		((i=i+1))
		done;
		notify-send-all -t 3000 -u low "GIF" "🍍"
	else
		echo "0"
	fi 

fi



echo
echo
echo "TOTAL ="
echo "------- $i"
echo


if [ $i != 0 ]
then
echo "-- reset converted / orignals _files.txt"

rm converted_files_noturl.txt
touch converted_files_noturl.txt
rm converted_files.txt
touch converted_files.txt
rm originals_files_noturl.txt
touch originals_files_noturl.txt
rm originals_files.txt
touch originals_files.txt

echo

ls ./thumbs/ -t | head -n $i > converted_files_noturl.txt
ls ./thumbs/ -t | head -n $i | python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.stdin.read()))" | sed "s/%0A/\n/g" | sed "s/\n\n/\n/g" > converted_files.txt

ls ./originals/ -t | head -n $i > originals_files_noturl.txt
ls ./originals/ -t | head -n $i | python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.stdin.read()))" | sed "s/%0A/\n/g" | sed "s/\n\n/\n/g" > originals_files.txt
## python3 html url encoding source :
## Post and comment :
## https://stackoverflow.com/questions/11876353/url-encoding-a-string-in-bash-script#comment104233192_56321886

## CLEANUP empty lines... Source : https://serverfault.com/a/252927
sed -i '/^$/d' converted_files.txt
sed -i '/^$/d' originals_files.txt
## ADD URL source on every beginning lines
sed -i -e "s|^|$URL|" converted_files.txt
sed -i -e "s|^|$URLORIGINAL|" originals_files.txt

echo "---- ORIGINALS :"
echo
cat originals_files.txt
echo
echo "---- CONVERTED :"
echo
cat converted_files.txt
echo
echo

notify-send-all -t 3000 -u low "FINITO" "🍎_files.txt"
#find ./ -maxdepth 1 -type f -print0 | xargs -0 mv -t ./originals/ && mv ./originals/bash_thumbs.sh ./bash_thumbs.sh
notify-send-all -t 10000 -u low "Images Thumbs ok" "JPG 🍰🍌🍐🍍🍎"


sudo chown pi:www-data -R /var/www/html/bulle/web/pj/previews_bulles/
sudo chmod +r -R /var/www/html/bulle/web/pj/previews_bulles/

else 

	echo "NEED FILES TO CONVERT"

fi 

#echo "-- all converted\n"
echo
echo "all in :"
echo "/var/www/html/bulle/web/pj/previews_bulles/"
echo
echo "https://bulle.vincent-bonnefille.fr/pj/previews_bulles/"
