#!/bin/bash
if [ -e ~/.cronenv ]; then
. ~/.cronenv
fi
tgt="$1"
wiki="$2"
template="$3"
n="$4"
if [ -z "$n" ]; then
if [ -z "$wiki" ] || [ "$wiki" == '-' ]; then wiki="commons.wikimedia.org"; fi
if [ -z "$template" ] || [ "$template" == '-' ]; then template="Potd"; fi
u="http://$wiki/wiki/Template:$template?action=render"
s=`wget -q -O - "$u" | grep '<a href=".*/[^/]*:[^/"]*\.[^/"]*" class="image" title=""><img src=".*/></a>' | head -1`
if [ -z "$s" ]; then
echo "failed to find image on $u"
exit 3
fi
n=`echo "$s" | sed 's;^.*<a href=".*/[^/]*:\([^/"]*\)" class="image".*$;\1;'`
ext="${n##*.}"
base="${n%.*}"
if [ -z "$tgt" ] || [ "$tgt" == "-" ]; then
echo "$n";
exit
else
echo "Current Potd: $n ($ext)";
fi
mem="$tgt/potd.name"
if [ -e "$mem" ]; then
prev=`cat "$mem"`
if [ "$prev" == "$n" ]; then
echo "same as previous, nothing to do"
exit
fi
fi
else
ext="${n##*.}"
base="${n%.*}"
mem="$tgt/potd.name"
fi
get=`dirname "$0"`/image-get
$get "$wiki" "$n" "$tgt"/potd
if [ $? -ne 0 ]; then
echo "failed to fetch image"
exit 7
fi
f="potd-full.$ext"
rm "$tgt"/potd-*.* 2>/dev/null
mv "$tgt/potd.$ext" "$tgt/$f"
touch "$tgt/$f"
ulimit -v 128000 -d 128000 -t 120
if [ "$ext" == "svg" ]; then
which rsvg >/dev/null 2>&1;
if [ $? -ne 0 ]; then
echo "rsvg not found, can't render SVG!"
exit 11
fi
dim=`grep '<image size=' "$tgt/potd.meta.xml" | head -1 | sed 's/^.*<image size="[0-9]*" width="\([0-9]*\)" height="\([0-9]*\)".*$/\1 \2/'`
read width height <<< "$dim"
if [ $? -ne 0 ]; then
echo "image dimensions not found in $tgt/potd.meta.xml, can't render SVG!"
exit 12
fi
echo "rendering SVG (${width}x${height})"
calc=" width = $width;
height = $height;
scale=5;
fw = w/width;
fh = h/height;
if (fw < fh) f = fw else f = fh;
print width * f, \" \", height * f, \"\n\""
read w h <<< `echo " w = 200; h = 150; $calc" | bc`
rsvg -w ${w%.*} -h ${h%.*} "$tgt/$f" "$tgt/potd-200x150.png"
read w h <<< `echo " w = 400; h = 300; $calc" | bc`
rsvg -w ${w%.*} -h ${h%.*} "$tgt/$f" "$tgt/potd-400x300.png"
read w h <<< `echo " w = 800; h = 600; $calc" | bc`
rsvg -w ${w%.*} -h ${h%.*} "$tgt/$f" "$tgt/potd-800x600.png"
read w h <<< `echo " w = 1024; h = 768; $calc" | bc`
rsvg -w ${w%.*} -h ${h%.*} "$tgt/$f" "$tgt/potd-1024x768.png"
read w h <<< `echo " w = 1280; h = 1024; $calc" | bc`
rsvg -w ${w%.*} -h ${h%.*} "$tgt/$f" "$tgt/potd-1280x1024.png"
read w h <<< `echo " w = 1600; h = 1200; $calc" | bc`
rsvg -w ${w%.*} -h ${h%.*} "$tgt/$f" "$tgt/potd-1600x1200.png"
else
echo "creating thumbnails..."
nice -20 convert "$tgt/$f" -size '200x150' -resize '200x150>' -quality 90 "$tgt/potd-200x150.$ext"
nice -20 convert "$tgt/$f" -size '400x300' -resize '400x300>' -quality 90 "$tgt/potd-400x300.$ext"
nice -20 convert "$tgt/$f" -size '800x600' -resize '800x600>' -quality 90 "$tgt/potd-800x600.$ext"
nice -20 convert "$tgt/$f" -size '1024x768' -resize '1024x768>' -quality 90 "$tgt/potd-1024x768.$ext"
nice -20 convert "$tgt/$f" -size '1280x1024' -resize '1280x1024>' -quality 90 "$tgt/potd-1280x1024.$ext"
nice -20 convert "$tgt/$f" -size '1600x1200' -resize '1600x1200>' -quality 90 "$tgt/potd-1600x1200.$ext"
chmod a+r "$tgt"/potd-*."$ext" "$tgt"/potd.*
fi
echo "done."
potd-get
text/x-shellscript, 3399 bytes (load raw)

