#! /bin/bash

# img_2790.jpg -> 20051015-190548-img_2790.jpg
echo "Renaming: img_2790.jpg -> 20051015-190548-img_2790.jpg"
for PIC in *.jpg ; do mv $PIC $(exif -m -t "Datum und Uhrzeit" $PIC | sed -e "s/://g" -e "s/ /-/")-$PIC ; done

# 20051015-190548-img_2790.jpg -> 20051015-190548-2790.jpg
echo "Renaming: 20051015-190548-img_2790.jpg -> 20051015-190548-2790.jpg"
mmv "*-img_*" "#1-#2"

# Create the upload directory
# This will contain all files to be uploaded to the server
mkdir upload

# Make Backup
mkdir Untouched
cp *.jpg Untouched/

# Stamp the pictures 
for PIC in *.jpg; do \
    echo -n $PIC ... ; \
    mogrify -font ./VANDIJKB.TTF \
            -pointsize 96 \
            -fill white \
            -draw 'text 115,1775 "http://stefan.waidele.info"' \
            -fill black \
            -draw 'text 113,1773 "http://stefan.waidele.info"' \
            $PIC ; \
    echo " stamped" ; 
done

# Create smaller Versions for the web
mkdir upload/800
for PIC in *.jpg ; do \
    echo -n $PIC ... ; \
    convert -resize 800x800 $PIC ./upload/800/$PIC ; \
    echo " converted to 800x800px" ; \
done

# Create thumbnails
# http://www.cit.gu.edu.au/~anthony/graphics/imagick6/thumbnails/
mkdir upload/thumbs
for PIC in *.jpg ; do \
    echo -n $PIC ... ; \
    convert -size 400x400 $PIC \
            -thumbnail '200x400>' \
	    -bordercolor white  -border 6 \
            -bordercolor grey60 -border 1 \
            -background  none   -rotate $(perl -e 'print rand() * 30 - 15') \
            -background  black  \( +clone -shadow 60x4+4+4 \) +swap \
            -background  none   -flatten \
            -depth 8 -colors 256 -quality 95    \
	    ./upload/thumbs/tn_$PIC.png ;
    echo " created thumbnail" ; 
done  

# tn_*.jpg.png -> tn_*.png
echo "Renaming: 20051015-190548-img_2790.jpg -> 20051015-190548-2790.jpg"
mmv "./upload/thumbs/*.jpg.png" "./upload/thumbs/#1.png"

# Create the overview-page
cp before.html upload/index.html
for PIC in *.jpg ; do 
    echo >>upload/index.html '<div class="float"><a href="800/'$PIC'"><img src="thumbs/tn_'$(echo $PIC | sed -e "s/.jpg/.png/")'"></a></div>' ; 
done  

cat after.html >>upload/index.html

cp style.css upload/

