#!/bin/bash

# Make Album.
#
# Use Wikipedia, Wikiquote, and Flickr to create
#   a random album cover composed in Image Magick.

# Get band name...
BANDNAME=$(lynx -dump "http://en.wikipedia.org/wiki/Special:Random" | sed -n '4p')
BANDNAME=$(echo $BANDNAME | sed 's/(.*$//g')

# Get quote fragment...
QUOTECHECK=1
while [ "$QUOTECHECK" -eq "1" ]
do
  QUOTEPAGE=$(lynx -dump "http://en.wikiquote.org/wiki/Special:Random" | grep -v "\[" | grep "\*" | sed 's/\*//g')
  QUOTEPAGELENGTH=$(echo "$QUOTEPAGE" | wc -l)
  if [ "$QUOTEPAGELENGTH" -ne "1" ]; then
    QUOTECHECK=0
  fi
done
QUOTE=$(echo "$QUOTEPAGE" | head -1)
QUOTE=$(echo "$QUOTE" | cut -d. -f1 | cut -d, -f1 | cut -d? -f1 | cut -d! -f1)
QUOTE=$(echo $QUOTE | sed 's/[\":;\-\(\)]//')
QUOTELENGTH=$(echo $QUOTE | wc -w)
if [ "$QUOTELENGTH" -gt "6" ]; then
  QUOTE=$(echo $QUOTE | cut -d" " -f-6)
fi

# Get image...
IMGURL=$(lynx -source "http://www.flickr.com/explore/interesting/7days/" | sed 's/ /\n/g' | grep "farm.\.static\.flickr" | head -1 | sed 's/src=\"//g' | sed 's/\"//g')
wget $IMGURL -O temp.jpg
convert temp.jpg -thumbnail x512 -resize '512x<' -gravity center -crop 512x512+0+0 +repage temp.jpg

# Pick random fonts...
ls /usr/share/fonts/TTF/*.ttf > temp_font_list
FONTLINE=$(($RANDOM % $(wc -l temp_font_list | cut -d" " -f1)))"p"
BANDFONT=$(sed -n $FONTLINE temp_font_list)
FONTLINE=$(($RANDOM % $(wc -l temp_font_list | cut -d" " -f1)))"p"
TITLEFONT=$(sed -n $FONTLINE temp_font_list)

# Create labels...
convert -background none -fill white -strokewidth 1 -stroke black -font $BANDFONT -pointsize 80 label:"$BANDNAME" bandtext.png
convert -background none -fill black -strokewidth 1 -stroke white -font $TITLEFONT -pointsize 80 label:"$QUOTE" titletext.png
convert bandtext.png -thumbnail '500x500>' bandtext.png
convert titletext.png -thumbnail '500x500>' titletext.png

# Apply labels...
composite -gravity north bandtext.png temp.jpg Album_Dada.jpg
composite -gravity south titletext.png Album_Dada.jpg Album_Dada.jpg

rm temp_font_list temp.jpg bandtext.png titletext.png

# echo "$BANDNAME"
# echo "$QUOTE"
# display Album_Dada.jpg

exit 0
