#!/bin/bash

NOME_CARTELLA="./piccole/"
NOME_FILE_CONF="conf.php"
NOME_ARRAY="foto"
SCALE="640X480"

function error() { echo -e $1 ; exit 1 ; }

test -z "$1" && error "Inserire una directory\n"`basename $0`" <directory>"
cd $1 >& /dev/null
[ $? == 1 ] && error "Inserire una directory valida!\nDirectory: $1"

test -d "$NOME_CARTELLA" && error "Cartella gia' esistente"
mkdir $NOME_CARTELLA >& /dev/null
[ $? == 1 ] && error "Non ho potuto creare la cartella di destinazione $NOME_CARTELLA\n"

echo "<?php //file di conf immagini" > "$NOME_CARTELLA$NOME_FILE_CONF"
echo "\$$NOME_ARRAY = array(" >> "$NOME_CARTELLA$NOME_FILE_CONF"
for f in *
do 
	if ! test -d "$f"
	then
		echo -n "Sto modificando $f..."
		convert -resize $SCALE "$f" "$NOME_CARTELLA$f" >& /dev/null
		if [ $? == 0 ]
		then
			echo "OK -> $NOME_CARTELLA$f"
			echo -e "\t\"$f\" => \"\"," >> "$NOME_CARTELLA$NOME_FILE_CONF"
		else
			echo "FAIL -> $f non e' un immagine"
		fi
	fi
done

echo -e "\t);\n?>" >> "$NOME_CARTELLA$NOME_FILE_CONF"

exit 0

