#!/bin/sh

# this script is useful to generate galleries for OOo from Open Clip Art
# Library source tarball that it available at
# http://www.openclipart.org/downloads/index.php

#
# See setup for user tweakables.
#
. ./setup

# where to install the OOo-related files (thumbnails & points at the files, ...)
if test "z$1" != "z"; then
    OOINSTDIR="$1"
fi

if test "z$OPENCLIPART_DIR" = "z" ; then
    # where to install the generated bitmaps
    OPENCLIPART_DIR=$OODESTDIR/usr/share/openclipart
    # where are the unpackaged openclipart sources
    OPENCLIPART_SRCDIR=$BUILDDIR/openclipart-$OPENCLIPART_VER
fi

# where to install the OOo-related files (thumbnails & points at the files, ...)
GAL_DIR=$OOINSTDIR/share/gallery
# It requires the gengal binary
# The binary is actually available in OOo only when built with ooo-build
GAL_BIN=$OOINSTDIR/program/gengal
# start number for the new galleries
GAL_NUMBER_FROM=70

if (! test -e $GAL_BIN); then
    echo "Cannot find $GAL_BIN; make sure you installed OOo first"
    exit 1;
fi

if test "z$OPENCLIPART_VER" != "z" ; then

    echo "Generating .png files..."

    if test ! -d $OPENCLIPART_SRCDIR ; then
	echo "Cannot find Open Clip Art sources in $OPENCLIPART_SRCDIR"
        echo "Try to run ./download and ./bin/unpack"
	exit 1;
    fi

    rm -rf $OPENCLIPART_DIR
    for pict_svg in `find $OPENCLIPART_SRCDIR -name "*.svg" -type f ` ; do
	pict_dir=${pict_svg#$OPENCLIPART_SRCDIR/}
        pict_dir=${pict_dir%/*}
	pict_png=${pict_svg##*/}
        pict_png=${pict_png%.svg}.png
	mkdir -p $OPENCLIPART_DIR/$pict_dir
        inkscape -f $pict_svg -e $OPENCLIPART_DIR/$pict_dir/$pict_png
    done

    echo "Removing some blacklisted files..."

    for path in signs_and_symbols/flags \
		unsorted ; do
	echo "  remove $path"
	rm -rf $OPENCLIPART_DIR/$path
    done

    #file list
    if test "z$OODESTDIR" != "z" ; then
	rm -f $BUILDDIR/openclipart.txt
	for dir in `find $OPENCLIPART_DIR -type d` ; do
	    echo "%dir ${dir#$OODESTDIR}" >>$BUILDDIR/openclipart.txt
	done
	for file in `find $OPENCLIPART_DIR -type f` ; do
	    echo "${file#$OODESTDIR}" >>$BUILDDIR/openclipart.txt
	done
    fi

    echo "done"

fi

if test ! -e /tmp/.X0-lock && which Xvfb >/dev/null 2>&1 ; then
    # X server probably is not available, try to use Xvfb
    export DISPLAY=":98"
    Xvfb -ac $DISPLAY &
    trap "kill $! || true" EXIT
    # let server to start
    sleep 10
fi

mkdir -p "$GAL_DIR"
for dir in `find $OPENCLIPART_DIR -mindepth 1 -maxdepth 1 -type d | LC_CTYPE=C sort` ; do
    # get the gallery name from the directory name
    # and make the first character uppercase
    gal_name=${dir##*/}
    gal_name=`echo $gal_name | tr "_-" "  "`
    gal_name_rest=${gal_name#?}
    gal_name_first_char=${gal_name%$gal_name_rest}
    gal_name_first_char=`echo $gal_name_first_char | tr "a-z" "A-Z"`
    gal_name=$gal_name_first_char$gal_name_rest

    echo "Doing gallery $gal_name..."
    # xargs is necessary because I 
    find $dir -name "*.png" | { xargs $GAL_BIN --name "$gal_name" --path "$GAL_DIR" --destdir "$OODESTDIR" --number-from "$GAL_NUMBER_FROM" || exit 1; }
done

# guess the filelist
if test "z$OODESTDIR" != "z" ; then
    gal_num=$GAL_NUMBER_FROM
    rm -f $BUILDDIR/galleries.txt
    while test -f $GAL_DIR/sg$gal_num.sdg -a \
               -f $GAL_DIR/sg$gal_num.sdv -a \
	       -f $GAL_DIR/sg$gal_num.thm ; do
	echo ${GAL_DIR#$OODESTDIR}/sg$gal_num.sdg >>$BUILDDIR/galleries.txt
	echo ${GAL_DIR#$OODESTDIR}/sg$gal_num.sdv >>$BUILDDIR/galleries.txt
	echo ${GAL_DIR#$OODESTDIR}/sg$gal_num.thm >>$BUILDDIR/galleries.txt
	gal_num=$(($gal_num + 1))
    done
fi
