#!/bin/bash
# A quick and dirty Bash script to create a random gallery image from
# Gallery v1 for Drupal. I run it in a cron job every minute as Drupal
# refuses to run this on every page load.
# You can then use PHP code in the blocks to post the image to the drupal
# page. Follow the examples for running custom shell scripts here:
# http://drupal.org/node/17142
# I suggest something like what I have below:
# $full = shell_exec("cat $HOME/.drupal-scripts/.urlfile");
# $thmb = shell_exec("cat $HOME/.drupal-scripts/.urlthumbfile");
# $albm = shell_exec("cat $HOME/.drupal-scripts/.albumfile");
# This has been released into the Public Domain on 6/2/05 by Bill Roehl
# contact me via http://lazylightning.org/
# setup paths for the files
PHOTO_DIR="/var/www/photos"
WWW_DIR="/var/www"
PARTIAL_PHOTO_DIR="www/photos"
THUMB_LIST="$HOME/.drupal-scripts/.thumblist"
RAND_OUTPUT="$HOME/.drupal-scripts/randomoutput"
LISTING_WITH_PATHS="$HOME/.drupal-scripts/pathoutput"
URLTHUMBFILE="$HOME/.drupal-scripts/.urlthumbfile"
URLFILE="$HOME/.drupal-scripts/.urlfile"
ALBUMFILE="$HOME/.drupal-scripts/.albumfile"
function get_random_file {
TOTAL_LINES=`wc $THUMB_LIST | awk '{ print $1 }'`
# generate a random line to pull the file from
RAND_LINE=0 #initialize
while [ "$RAND_LINE" -le 2 ]; do
RAND_LINE=$RANDOM
# Scales $RAND_LINE down within our range.
let "RAND_LINE %= $TOTAL_LINES"
done
head -n $RAND_LINE $THUMB_LIST > $RAND_OUTPUT
RANDOM_PHOTO=`tail -n 1 $RAND_OUTPUT`
cd $WWW_DIR
# give us the full pathname of the files starting w/our web directory
find $PARTIAL_PHOTO_DIR -name $RANDOM_PHOTO > $LISTING_WITH_PATHS
# we need to know if it pulled a list with multiple lines of the
# same name, for pictures with the same filename (my mobile pics)
TOTAL_LINES=`wc $LISTING_WITH_PATHS | awk '{ print $1 }'`
if [ $TOTAL_LINES -gt 1 ]; then
RAND_LINE=1
# well, if we do have multiple lines, pick another from
# the list randomly
while [ $RAND_LINE -le 2 ]; do
RAND_LINE=$RANDOM
let "RAND_LINE %= $TOTAL_LINES"
done
head -n $RAND_LINE $LISTING_WITH_PATHS > $RAND_OUTPUT
RANDOM_PHOTO_PATH=`tail -n 1 $RAND_OUTPUT`
else
# we don't need a random pull here it's only 1 filename
RANDOM_PHOTO_PATH=`cat $LISTING_WITH_PATHS`
fi
}
# inappropriate text that we don't want included in the list
# obviously more can be added
N1="dscn013"
N2="dscn0436"
# List images here that do not have a sized thumbnail
BASECK1="sc_dscn"
BASECK2="part"
# initialize the variables for the main loop
RANDOM_PHOTO_PATH="dscn013"
FAIL=1
while [ $FAIL != 0 ]; do
# if the path is an illegal file keep looking for another
if [[ $RANDOM_PHOTO_PATH = *$N1* ]] || [[ $RANDOM_PHOTO_PATH = *$N2* ]]; then
FAIL=1
get_random_file
else
# once we have a good one we need to create some workarounds
# b/c Drupal wouldn't output this any other way
FAIL=0
echo "$RANDOM_PHOTO_PATH" > $URLTHUMBFILE
PATH=${RANDOM_PHOTO_PATH%/*}
FILE=${RANDOM_PHOTO_PATH##*/}
BASE=${FILE%%.*}
# If the photo can be resized give us the .sized.jpg instead
if [[ $BASE = *$BASECK1* ]] || [[ $BASE = *$BASECHK2* ]]; then
LINKED_URL=$PATH/$BASE.jpg
else
LINKED_URL=$PATH/$BASE.sized.jpg
fi
# Give us a link to the fullsized image
echo "$LINKED_URL" > $URLFILE
# Give us a link to the gallery itself
ALBUM_PATH=`echo $PATH/| /usr/bin/cut -d "/" -f3-`
echo $ALBUM_PATH > $ALBUMFILE
fi
done