#!/bin/sh
#
# TODO:
# ricorsione.
# [xunil@elwood foto_root]$ tutte=`ls -bR|grep "^./"|sed -e s/://g|sed -e s@' '@'\\ '@g|grep -v thub`
#[xunil@elwood foto_root]$ retu=$PWD ;for a in $tutte; do cd $retu; cd $a; galleria.sh --html; echo "fatto in $a"; done
#
# fatta ricorsione
#
#---------
# si può mandare il calcolo su un altra macchina!
#
#
newSize="130x130"
output=index.php
scaledDir=thub/
remoteDir=/tmp/
RET=`echo -e "\n"`
CORPO=""

#export LISTAFILES_ORDER
#DIRECTORIES="`ls -bR|grep ./|sed -e s/://`"
#=`ls -bR|grep "^./"|sed -e s/://g|sed -e s@' '@'\\ '@g|grep -v $scaledDir`
#=`ls -d */.|sed -e s@' '@'\\ '@g |grep -v $scaledDir`
DIRECTORIES=`ls -1d */. |grep -v $scaledDir`
HEADER="<?php 
?>
<html>
 <head>
<script type='text/javascript' src='/js/prototype.js'></script>
<script type='text/javascript' src='/js/scriptaculous.js?load=effects,builder'></script>
<script type='text/javascript' src='/js/lightbox.js'></script>
<link rel='stylesheet' href='/css/lightbox.css' type='text/css' media='screen' />
  <title>Galleria $PWD </title>
  <meta name=\"author\" content=\"Porcelinux.adg\" ></head>
<STYLE type=\"text/css\" >
<!--
.pitc  {

        cursor:pointer;
	}

.head {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	color: Black;
	font-weight: bold;
}
.foot {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 9px;
	color: Black;
	font-weight: bold;
}

-->
</STYLE>
<body bgcolor=\"dddddd\"  marginwidth=\"0\" marginheight=\"0\" link=\"222222\" vlink=\"333333\">

<p align=\"center\" id=\".head\">
<br>
 <a href=\"..\">Directory UP</a><br>"

#	width:300;
#	height:300;
help_out  () {
echo "Generatore di gallerie:"
echo -e "\t -R\t\t ricorsivo "
echo -e "\t --html\t\t Genera solo il codice HTML"
echo -e "\t -t\t\t Crea un file tar.gz con con i files e crea un link"
echo -e "\t -i\t\t calcola il tempo"
echo -e "\t -h=host\t esegue convert in host e copia il file via ssh,"
echo -e "\t\t\t scambiare le chiavi ssh prima per evitare la richiesta di pass."

exit 0
}
ricorsiva () {
#if [ $# -ne 2 ];then echo "recursive error"; exit 1 ;fi
#echo "++++++++++++++++++++ $parametri"

tutte=`ls -1 -bR|grep "^./"|sed -e s/://g|sed -e s@' '@'\\ '@g|grep -v $scaledDir`
retu=$PWD
IFS="
" 
for a in $tutte
do 
	cd $retu
	cd $a
	echo "entrando in $a"
	galleria.sh $parametri
	echo "uscito da $a"
done
}

for par in $@ 
do
	case $par in 
		"-R") recursive="-R"
			echo "recursive..."
			#new_par=`echo "$@"| sed 's/-R//'`
			#ricorsiva "$new_par"
		;;
		"--html") ONLY_HTML="--html" ;;
		"-t") TAR_IT="-t" ;;
		"-i") LISTAFILES_ORDER='-r' ;;
		"--simula") ;;
		-h=*) REMOTE=`echo $par | sed 's/-h=//'` ;;
		"--help") help_out ;;
	esac
done

parametri="$ONLY_HTML $TAR_IT $LISTAFILES_ORDER -h=$REMOTE"

if ! [ -d $scaledDir ]; then mkdir $scaledDir ; fi

IFS="
"

if [ "X$recursive" == "X-R" ];then ricorsiva $parametri; fi

for dirChild in $DIRECTORIES; do
	
	dirChildhtml=`echo "$dirChild" |sed -e s@' '@%20@g`
#echo "$dirChildhtml $dirChild"
	TAGLINK="<a href=\"$dirChildhtml\">$dirChild</a>"
	HEADER="$HEADER
$TAGLINK"
done
IFS="
"
HEADER="$HEADER <br><br>"
#exit 1
#if [ "x$ONLY_HTML" == "x--html" ];then
	if [ "x$TAR_IT" == "x-t" ];then
		if ! [ -e zippate.tar.gz ];then
		echo "tar-ring..."
		if [ "x$REMOTE" == "x" ];then
			tar vczf zippate.tar.gz ./*.[jJ][pP][gG]
		else # 
			#ssh  "$REMOTE" tar czf zippate.tar.gz ./*.*
			DIR__=`echo "$PWD"|sed 's/ /\ /g'`
			tt=`ssh $REMOTE ls "$DIR__"`
			a=$?
			if [ $a -eq 0 ];then
			#== "X`ssh $REMOTE mount |grep $HOSTNAME`" ];then
			#	echo "$PWD"
				ssh "$REMOTE" cd "$PWD" ; tar vczf "${remoteDir}"zippate.tar.gz ./*.[jJ][pP][gG]
				scp "$REMOTE":"${remoteDir}zippate.tar.gz" .
				ssh "$REMOTE" rm -f "${remoteDir}zippate.tar.gz"
			fi
		fi
		fi
		HEADER="$HEADER <br>
		<a href=zippate.tar.gz>All tar gizipped download</a>
		<br><br>"
	fi


LISTAFILES=`ls -1 -b $LISTAFILES_ORDER *.[jJ][pP][gG]|grep -v [jJ][pP][gG]:|grep -v $scaledDir`
LISTAFILES="$LISTAFILES
`ls -1 -b $LISTAFILES_ORDER *.[pP][nN][gG]|grep -v [pP][nN][gG]:`"
IFS="
"
for imageFileb in $LISTAFILES; do

	imageFile=`echo "$imageFileb"|sed 's/\\\ /\ /g'`
#	echo "--------- $imageFileb"|tr -s '\\ ' ' ' 
#	echo "+++++++++ $imageFile"
	imageFileScaled=$scaledDir$newSize-$imageFile
	imageFileScaledb=$scaledDir$newSize-$imageFileb
	RemoteImageFileScaledb=$remoteDir$newSize-$imageFileb
#	echo "--------- $imageFileScaledb"
	if ! [ "x$ONLY_HTML" == "x--html" ];then
		if ! [ -e $imageFileScaled ];then
			if [ "x$REMOTE" == "x" ];then
				echo "converting..."
				convert -scale $newSize $imageFile $imageFileScaled
				echo "scalata $imageFile a $newSize in $imageFileScaled"
			else
				#echo -n "copiando in $REMOTE -+- "
				echo "scp "$imageFile" $REMOTE:"$remoteDir$imageFileb""
				scp "$imageFile" $REMOTE:"$remoteDir$imageFileb"

				echo "converting in $REMOTE ... $remoteDir$imageFileb $RemoteImageFileScaledb"
				ssh  $REMOTE "convert -scale $newSize $remoteDir$imageFileb $RemoteImageFileScaledb"
				#echo -n "copiando scalata da $REMOTE "
				scp  $REMOTE:$RemoteImageFileScaledb "./$imageFileScaled"
				ssh  $REMOTE "rm -f $RemoteImageFileScaledb ; rm -f $remoteDir$imageFileb"
				#echo "$REMOTE -> removed: $RemoteImageFileScaledb , $remoteDir$imageFileScaled"
			fi
		fi
	fi
	TAGIMAGE="
	<a href=\"$imageFile\" rel=\"lightbox[sett]\" target=\"_blank\"> 
	<img class=\"pitc\" src=\"$imageFileScaled\"> 
	</a>"
	CORPO=`echo "$CORPO $TAGIMAGE"`

done


#fi

FOOTER="
<br><br>
 <a href=\"..\">Directory UP</a><br>
	<p align=\"right\" id=\".foot\">
		<a href=\"/scripts/galleria.sh\" > Page created by galleria.sh porcelinux script<br>
at `date +%Y"/"%m"/"%d" "%H":"%M`
		</a>
	</p>
</body></html>
"

PAGINA="$HEADER $CORPO $FOOTER"
#exit 0
echo "$PAGINA" > $output

