#!/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

# source ooenv if it exists
if [ -f "$OOINSTDIR/program/ooenv" ] ; then
    pushd "$OOINSTDIR/program" > /dev/null
    . ooenv
    popd > /dev/null
fi

BUILD_OOO_GAL_FROM_OPENCLIPART=no
if test -n "$OPENCLIPART_VER" -o -n "$OPENCLIPART_DIR" ; then
    BUILD_OOO_GAL_FROM_OPENCLIPART=yes
fi

if test "$BUILD_OOO_GAL_FROM_OPENCLIPART" = "no" -a \
        "$VENDORNAME" != "Novell" ; then
    echo "    ${0##*/}: Nothing to do"
    exit 0
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 are installed the OOo-related files (thumbnails & points at the files, ...)
GAL_DIR=$OOINSTDIR/basis$VERSION/share/gallery
# It requires the gengal binary
# The binary is actually available in OOo only when built with ooo-build
GAL_BIN=$OOINSTDIR/basis$VERSION/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

echo -n "checking whether find supports the option -L... "
if find -L / -maxdepth 0 >/dev/null 2>&1 ; then
    FIND_SUPPORT_SYMLINKS="-L"
    echo "yes"
else
    FIND_SUPPORT_SYMLINKS=
    echo "no"
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

# clean a previous gallery installation in DESTDIR
if test "z$OODESTDIR" != "z" ; then
    gal_idx=$GAL_NUMBER_FROM

    while test -f $GAL_DIR/sg$gal_idx.thm -o \
		-f $GAL_DIR/sg$gal_idx.sdg -o \
		-f $GAL_DIR/sg$gal_idx.sdv; do
			rm -f $GAL_DIR/sg$gal_idx.thm \
			      $GAL_DIR/sg$gal_idx.sdg \
			      $GAL_DIR/sg$gal_idx.sdv
	gal_idx=$(($gal_idx + 1))
    done
fi

mkdir -p "$GAL_DIR"

if test "$BUILD_OOO_GAL_FROM_OPENCLIPART" = "yes" ; then
    echo "Building extra galleries from openclipart..."
    for dir in `find $FIND_SUPPORT_SYMLINKS $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" -print0 | LC_CTYPE=C sort -z | { xargs -0 $GAL_BIN -headless --name "$gal_name" --path "$GAL_DIR" --destdir "$OODESTDIR" --number-from "$GAL_NUMBER_FROM" || exit 1; }
    done
fi

# 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.thm; do
	if [ -e $GAL_DIR/sg$gal_num.sdg -a -e $GAL_DIR/sg$gal_num.sdv ]; then
		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
	else
		# remove incomplete galleries (exists .thm but not .sdg)
		rm -f $GAL_DIR/sg$gal_num.sdv \
		      $GAL_DIR/sg$gal_num.thm
	fi
	gal_num=$(($gal_num + 1))
    done
fi

if test "$VENDORNAME" = "Novell" ; then
    # We want to update an existing gallery. Therefore we do it after the
    # galleries.txt filelist is generated. This way we can detect problems
    # via the RPM check for installed but unpackaged files
    echo "Adding Novell specific Draft.jpg to Backgrounds gallery..."
    cp "$TOOLSDIR/src/Draft.jpg" "$GAL_DIR/www-back/" || exit 1;
    chmod a+r $GAL_DIR/www-back/Draft.jpg
    # make sure that the existing galleries can be modified
    chmod u+rw $GAL_DIR/sg*.???
    # LC_ALL=en_US.UTF-8 is necessary to make sure that the name "Backgrounds" is compared with the right localization
    LC_ALL=en_US.UTF-8 $GAL_BIN -headless --name "Backgrounds" --path "$GAL_DIR" --destdir "$OODESTDIR" "$GAL_DIR/www-back/Draft.jpg" || exit 1;

    if test "z$OODESTDIR" != "z" ; then
	echo "${GAL_DIR#$OODESTDIR}/www-back/Draft.jpg" >$BUILDDIR/novell-gallery-addon
    fi
fi
