#!/bin/bash
# Create a html galery
FILE="";
nbfiles=$#
tdeinstdir=`kde-config --prefix`
KIM_DIR=$tdeinstdir/share/apps/kim/galery
TITLE=`kdialog --inputbox "Kim - Title:" "My title ..."`;
AUTHOR=`kdialog --inputbox "Kim - Author:" "$USER"`;
DATE=`date '+%A %-d %B %Y'`;
SCALE_1="800x800";
SCALE_2="200x200";
K=`kdialog --inputbox "Kim - Number of columns:" "4"`;
DIR=`kdialog --getexistingdirectory /home/$USER`;
W="200";
H="150";

dcopRef=`kdialog --progressbar "Kim - Initialising ..." $nbfiles`
dcop "$dcopRef" showCancelButton true

# Creation of directories
mkdir $DIR/images;
mkdir $DIR/thumbs;
# Header of $DIR/index.html
cp $KIM_DIR/index.part1 $DIR/index.html;
echo '<h1>'$TITLE'</h1>' >> $DIR/index.html;
echo '<h5>Author: '$AUTHOR'<br>Date: '$DATE'</h5>' >> $DIR/index.html;
echo '<table><tr>' >> $DIR/index.html;

compteur=0;
tmp=0;
for i in "$@";do
    if [ -f "$i" ];then
        #test if cancel button has been pushed
        if test "true" = `dcop "$dcopRef" wasCancelled`;then
            dcop "$dcopRef" close
            exit 1
        fi
        ((compteur++))
        ((tmp++))
        FILE="$i";
        IMAGE=`basename "$FILE"`;
        convert -resize $SCALE_1 "$FILE" $DIR/images/"$IMAGE";
        convert -resize $SCALE_2 "$FILE" $DIR/thumbs/"$IMAGE";
        W2=`identify -format "%w" $DIR/images/"$IMAGE"`;
        H2=`identify -format "%h" $DIR/images/"$IMAGE"`;
        echo '<td align='center'>' >> $DIR/index.html;
        if [ "$H2" -lt "$W2" ];
        then echo '<a href="images/'"$IMAGE"'"><img class="photo" src="thumbs/'"$IMAGE"'" width="'$W'" height="'$H'" alt="thumbs/'"$IMAGE"'" title="'"$IMAGE"'"></a>' >> $DIR/index.html;
        else echo '<a href="images/'"$IMAGE"'"><img class="photo" src="thumbs/'"$IMAGE"'" width="'$H'" height="'$W'" alt="thumbs/'"$IMAGE"'" title="'"$IMAGE"'"></a>' >> $DIR/index.html;
        fi;
        echo '<p>'"$IMAGE"'</p>' >> $DIR/index.html;
        echo '</td>' >> $DIR/index.html;
        if [ "$tmp" -eq "$K" ];
        then    echo '</tr><tr>' >> $DIR/index.html;
            tmp=0;
        fi
        dcop "$dcopRef" setLabel "Kim - Treatment of file: "$IMAGE""
        dcop "$dcopRef" setProgress "$compteur"
    fi;
done

cat $KIM_DIR/index.part2 >> $DIR/index.html;
dcop "$dcopRef" close;

konqueror $DIR/index.html;
