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

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

XVFB=`which Xvfb 2>/dev/null`
if ! test -x "$XVFB"; then
	# Fall back to some default locations if which didn't work
	XVFB=/usr/X11R6/bin/Xvfb
	if ! test -x "$XVFB"; then
		XVFB=/usr/bin/Xvfb
	fi
fi
if ! test -x "$XVFB" ; then
    if test "$BUILD_OOO_GAL_FROM_OPENCLIPART" = "yes" ; then
	echo "Error: $XVFB not found" >&2
	exit 1
    else
	echo "Warning: $XVFB not found,"
	echo "         skipping the optional gallery stuff..."
	exit 0;
    fi
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/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

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

XDISPLAY=98
echo "Starting Xvfb..."
# try only 10-times to avoid infinite loop
while test "$XDISPLAY" != "108" ; do 
	if [ ! -f /tmp/.X$XDISPLAY-lock ]; then
		echo "    trying display :$XDISPLAY ..."
		sleep 2s
		$XVFB -ac :$XDISPLAY &
		trap "kill $! > /dev/null 2>&1 || true" EXIT
		# let server to start
		sleep 10s 
		if [ -f /tmp/.X$XDISPLAY-lock ]; then
			break;
		fi
	fi
	XDISPLAY=$(($XDISPLAY+1))
done

if ! test -f /tmp/.X$XDISPLAY-lock ; then
    echo "Error: Unable to start Xvfb" >&2
    exit 1
fi

export DISPLAY=":$XDISPLAY"
echo "Using DISPLAY="$DISPLAY
echo "Xvfb processes:"
ps -ef | grep -i Xvfb

# 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 --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 "$SRCDIR/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*.???
    # LANG=en_US.UTF-8 is necessary to make sure that the name "Backgrounds" is compared with the right localization
    LANG=en_US.UTF-8 $GAL_BIN --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
