#!/bin/bash

LSswf=$(ls | grep galerie.swf)
if [ -z $LSswf ]; then
    echo -e "CHYBA! \n V adresari chybi galerie.swf  \n Mám jej stahnout - je nutne pripojeni k internetu! (A/n)"
    read SWF
    if [ -n "$SWF" ]; then
	case $SWF in
	    "a") wget http://physics.muni.cz/~petos/other/linux/galerie.swf ;;
	    "A") wget http://physics.muni.cz/~petos/other/linux/galerie.swf ;;
	    "n") echo "Chybi soubor. Doplnte jej a pak script spustte znovu" && exit ;;
	    "N") echo "Chybi soubor. Doplnte jej a pak script spustte znovu" && exit ;;
	    *) wget http://physics.muni.cz/~petos/other/linux/galerie.swf ;;
	esac
    else
	wget http://physics.muni.cz/~petos/other/linux/galerie.swf	
    fi
fi


LSjs=$(ls | grep galerie.js)
if [ -z $LSjs ]; then
    echo -e "CHYBA! \n  V adresari chybi galerie.js  \n Mám jej stahnout - je nutne pripojeni k internetu! (A/n)"
    read JS
    if [ -n "$JS" ]; then
	case $JS in
	    "a") wget http://physics.muni.cz/~petos/other/linux/galerie.js ;;
	    "A") wget http://physics.muni.cz/~petos/other/linux/galerie.js ;;
	    "n") echo "Chybi soubor. Doplnte jej a pak script spustte znovu" && exit ;;
	    "N") echo "Chybi soubor. Doplnte jej a pak script spustte znovu" && exit ;;
	    *) wget http://physics.muni.cz/~petos/other/linux/galerie.js ;;
	esac
    else
	wget http://physics.muni.cz/~petos/other/linux/galerie.js
    fi
fi

GALERYNAME=$(pwd | awk -F '/' '{print $(NF)}')

LSimg=$(ls | grep images)
if [ -z $LSimg ]; then
mkdir images
else
mv images/* .
fi


echo -e "------------------------------------------------"
echo -e "Následuje seznam obrázků, jenž bude začleněn do galerie:"
ls | grep --color [Jj][Pp][Gg]
echo -e "Stisknete-li \"a\", jména se automaticky upraví dle vzoru: jmeno-adresare_cislo.jpg."
echo -e "Stiskem klavesy \"f\" se script bude chovat tak, ze prejmenuje fotky podle vzoru: jmeno-adresare_puvodni-jmeno-fotky.jpg"
echo -e "Stisknete-li \"g\", spusti se geeqie a je mozne jmeno upravit rucne"
echo -e "Stisknete-li \"k\" nebo ^C, script se ukonci a je mozne upravit seznam fotografii"
echo -e "Stiskem libovolné jiné klávesy bude pokracovat generovani galerie dal"
read SEZNAMexit

case $SEZNAMexit in
    "g") geeqie ;;
    "k") exit ;;
    "a") for fotka in *.[Jj][Pp][Gg]; do cislo=$(echo $fotka | awk -F '_' '{print $2}')&& novejmeno="$GALERYNAME"_"$cislo" && mv $fotka $novejmeno; done ;;
    "f") for fotka in *.[Jj][Pp][Gg]; do mv $fotka "$GALERYNAME"_"$fotka"; done ;;
    *) echo -e "Nebyly provedeny zadne upravy se jmeny, pokracuje se dal";;
esac



echo "Galerie se bude jmenovat $GALERYNAME. Prejete-li si jine jmeno, napiste jej. V opacnem pripade stisknete enter"

read jmenogalerie
if [ -n "$jmenogalerie" ]; then
    GALERYNAME="$jmenogalerie"
else
    GALERYNAME=$GALERYNAME
fi

echo "Jmeno galerie je nastaveno na $GALERYNAME"


cat > index.html << EOF
<!-- saved from url=(0014)about:internet -->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Petosovy fotky</title>
<!-- Download SimpleViewer at www.airtightinteractive.com/simpleviewer -->
<script type="text/javascript" src="galerie.js"></script>
<style type="text/css">	
	/* hide from ie on mac \*/
	html {
		height: 100%;
		overflow: hidden;
	}
	
	#flashcontent {
		height: 100%;
	}
	/* end hide */

	body {
		height: 100%;
		margin: 0;
		padding: 0;
		background-color: #181818;
		color:#ffffff;
		font-family:sans-serif;
		font-size:20;
	}
	
	a {	
		color:#cccccc;
	}
</style>
</head>
<body>
	<div id="flashcontent">SimpleViewer requires JavaScript and the Flash Player. <a href="http://www.macromedia.com/go/getflashplayer/">Get Flash.</a></div>	
	<script type="text/javascript">
	
		var fo = new SWFObject("galerie.swf", "viewer", "100%", "100%", "8", "#181818");
		
		// SIMPLEVIEWER CONFIGURATION OPTIONS
		// To use an option, uncomment it by removing the "//" at the start of the line
		// For a description of config options, go to: 
		// http://www.airtightinteractive.com/simpleviewer/options.html
		
		//fo.addVariable("xmlDataPath", "gallery.xml");
		//fo.addVariable("firstImageIndex", "5");	
		//fo.addVariable("langOpenImage", "Open Image in New Window");
		//fo.addVariable("langAbout", "About");	
		//fo.addVariable("preloaderColor", "0xFFFFFF");
		
		fo.write("flashcontent");	
	</script>	
</body>
</html>
EOF


cat > gallery.xml <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<simpleviewergallery maxImageWidth="480" maxImageHeight="480" textColor="0xFFFFFF" frameColor="0xFFFFFF" frameWidth="20" stagePadding="40" navPadding="40" thumbnailColumns="4" thumbnailRows="3" navPosition="left" vAlign="center" hAlign="center" title="$GALERYNAME" enableRightClickOpen="true" backgroundImagePath="" imagePath="" thumbPath="">

EOF


LSimg=$(ls | grep thumbs)
if [ -z $LSimg ]; then
mkdir thumbs
else
rm -f thumbs/*
fi



for JMENO in *.[Jj][Pp][Gg]
do

ISO=$(jhead "${JMENO}" | grep ISO | awk -F ':' '{print $2}')
TIME=$(jhead "${JMENO}" | grep time | awk -F '(' '{print $2}' | awk -F ')' '{print $1}')
APERTURE=$(jhead "${JMENO}" | grep Aperture | awk -F ':' '{print $2}')

echo "<image>" >> gallery.xml
echo "<filename>$JMENO</filename>" >> gallery.xml
echo "<caption>" >> gallery.xml
echo "ISO: $ISO, Cas: $TIME, Clona: $APERTURE" >> gallery.xml
echo "</caption>" >> gallery.xml
echo "</image>" >> gallery.xml

convert $JMENO -resize 800x800 -adaptive-sharpen 1 $JMENO
cp $JMENO images
convert $JMENO -resize 65x65 -adaptive-sharpen 1 $JMENO
mv $JMENO thumbs

done

echo "</simpleviewergallery>" >> gallery.xml

