#!/bin/bash
#######################################################################
# mkalbum by Jonathan Pennington <john@coastalgeology.org>
#
# Specifically for use with Cthumb (but easily adapted to other stuff).
# See http://puchol.com/cpg/software/cthumb/ for details.
#
# This script automatically builds a file for seperate .html pages 
# based on the contents of the current directory and its 
# subdirectories. It *should* read all .jpg files in the current and 
# subdirectories and spit out an album file which will build a seperate
# .html page for each directory with the subdirectory name as the name 
# the .html page. 
#
# NOTE: This script effectively deals with additional directories 
# *within* the individual subdirectories. For example, I have a main
# directory named ~/pics and many sub- and subsubdirectories under it. 
# This script will build seperate pages for:
#
# ~/pics/*.jpg
# ~/pics/geology/*.jpg
# ~/pics/geology/rocks/*.jpg
# ~/pics/geology/rocks/igneous/*.jpg
# ~/pics/geology/rocks/igneous/pretty/*.jpg
# ~/pics/geology/rocks/igneous/ugly/*.jpg
# ~/pics/geology/rocks/sedimentary/*.jpg
#
# The directory tree is limited to 8 levels, but more can be added if
# you really need > 8 levels of subdirectories. 
#  
# The script can automatically call cthumb (provided it is in your 
# $PATH) with the name of the generated album file as its only 
# argument. This will happen if no argument is given. The possible 
# arguments are N (for "run cthumb? NO") and a desired top level 
# directory. eg:
#
# % mkalbum N ~/your_dir  #uses ~/your_dir as dir, doesn't run cthumb
#
# % mkalbum N             #uses current dir, doesn't run cthumb
#
# % mkalbum ~/your_dir    #uses ~/your_dir and runs cthumb
#
# % mkalbum               #uses current dir and runs cthumb 
#
# The album file will be called DATE.album, where date is the current
# date (according to your computer). Example: Oct-31-2000.album.
# I do everything by date, if you don't like that, change it.
#
# This script requires a working ~/.cthumbrc file that has ALL of the 
# variables defined.
########
# HINT: This is a kludge. I wrote it very fast to do something on my 
# own box. It's GPL'd and all that jazz.
########################################################################

getErr () {
    if test -n $OPTARG; then
    putErr
    fi
    exit 0
}

function putErr {
cat <<EOF
MkAlbum v0.1 (Sep 14, 2000)
A shell script to make a multiple directory album for Cthumb

Options:

    -a [name]    Use current system date in title [Default=y]
    -c [dir]     Cthumb executable directory [Default=\$PATH]
    -d dir       Use 'dir' as top level directory for tree 
		    [Default=$dir]
    -e [type]    Comma seperated list (no spaces!) of file extensions
		    [Default: '*.jpg,*.gif,*.pnm'] (BROKEN)
    -f [y|n]     Use current system date in filename [Default=y]
    -h, ?        Display this screen
    -l [lang]    Primary language [Default=English]
    -L [lang]    Secondary language [Default=none]
    -r [y|n]     Run Cthumb automatically after page creation [Default=y] 
		    (Note: Cthumb must be in \$PATH unless -c is used)
    -t title     Use 'title' as main index title [Default="Picture Index"]

Usage: mkalbum [options]
EOF
}

# Set some defaults

dir=`pwd`
run_cthumb="y"
title="Picture Index"
use_date_file="y"
use_date_title="y"
p_lang="English"
s_lang=""
f_types="*.jpg,*.gif,*.pnm"
c_dir=""

# Check and parse options

while getopts ":a:c:d:f:l:L:t:r:h" opt; do
    case $opt in
	a  ) use_date_title="$OPTARG" ;;
	c  ) c_dir="$OPTARG" ;;
	e  ) f_types="$OPTARGS" ;;
	f  ) use_date_file="$OPTARG" ;;
	d  ) dir="$OPTARG" ;;
	r  ) run_cthumb="$OPTARG" ;;
	t  ) title="$OPTARG" ;;
	l  ) p_lang="$OPTARG" ;;
	L  ) s_lang="$OPTARG" ;;
	h | \? | * ) putErr
                     exit 1 ;;
    esac
done



# Change to proper directory

cd $dir

# If dates are used (default), build file name with them

set $(date)

if [ $use_date_file == n ]; then
    file=cthumb.album
else
    date=$2-$3-$6
    file=$date.album
fi

# Set nice date format for use inside files 

date_nice="$2 $3, $6"

# test for and save previous run

if [ -e $file ]
    then
	mv $file $file-bak
fi

# Parse the .cthumbrc file and generate a header change these values 
# as needed. This is the hard part because the cthumbrc files can be
# really ugly. This is bound to be buggy.

echo "# CThumb album file. Created $date_nice by the mkalbum script." > $file
echo "# This script requires a working ~/.cthumbrc file." >> $file
echo "# These are the values of your ~/.cthumbrc file used here." >> $file 
echo "" >> $file

# Use date in the album title unless specified

if [ $use_date_title == y ]; then
    echo "AlbumTitle: "$title "(created on $date_nice)" >> $file
else
    echo "AlbumTitle: "$title >> $file
fi

# Set languages

if [ s_lang != none ]; then
    languages=$p_lang" "$s_lang
else
    languages=$p_lang
fi

echo "Languages: "$languages >> $file

# Cleanup the cthumbrc file for local use

cat $HOME/.cthumbrc |
sed -e 's/\$//g' -e '/Languages/d' \
    -e 's/\;/ /g' -e 's/"//g' \
    -e 's/=/:/g' -e '/cthumb/d' \
    -e 's/ /!/g' |
awk -F! ' { print $1 } ' >> $file

# Find all the files, clean-up the list, remove thumb dirs, sort, 
# organize, and remove sub-subdirectory listings while pretty-printing 
# and then dump it to the file.

find ./* -type f -regex '.*\.[(jpg)|(gif)]+' | 
sed -e 's|\.\/||g' |
awk -F/ ' !/thumb/ { print $0 } ' |
sort | 
awk -F/ ' 
{
if ( NF == 1 ) { 
    page_path = ENVIRON["PWD"]
    pic_path = ENVIRON["PWD"]
    var = 0
} else if ( NF == 2 ) {
    page_path = 1
    pic_path = 1
    var = 2
} else if ( NF == 3 ) {
    page_path = 2
    $pic_path = $1"/"$2
    var = 3
} else if ( NF == 4 ) {
    page_path = 3
    $pic_path = $1"/"$2"/"$3
    var = 4
} else if ( NF == 5 ) {
    page_path = 4
    $pic_path = $1"/"$2"/"$3"/"$4
    var = 2
} else if ( NF == 6 ) {
    page_path = 5
    $pic_path = $1"/"$2"/"$3"/"$4"/"$5
    var = 2
} else if ( NF == 7 ) {
    page_path = 6
    $pic_path = $1"/"$2"/"$3"/"$4"/"$5"/"$6
    var = 2
} else if ( NF == 8 ) {
    page_path = 7
    $pic_path = $1"/"$2"/"$3"/"$4"/"$5"/"$6"/"$7
    var = 2
}
}

$pic_path == LastPath {
	print "\t-" $pic_path"/"$var "\n\t\t,@," $pic_path"/"$var "\n"
}

$pic_path != LastPath { 
	LastPath = $pic_path
	print "Page: "$page_path "\n\t\t,@,Picture index of "$pic_path
	print "\t-" $pic_path"/"$var "\n\t\t,@," $pic_path"/"$var "\n"
}' |
awk -F, '
/(Page)/ {
	print $0
}
/(^\t-)/ {
	print $0
}
/(^\t+.*@)/ {
	print $1$3 "\n"
} ' |
sed -e '/^\-/s/\\n//' >> $file 

# Run cthumb if instructed

if [ $run_cthumb == y ]; then
    if [ -z $c_dir ]; then
	eval cthumb $file &
    else 
	eval $c_dir/cthumb $file &
    fi

fi

exit 0