File loconvert of Package libreoffice-converter

#! /bin/bash

# This script converts a document from one office format to another by
# connecting to an LibreOffice instance via Python-UNO bridge.
#
# Uses DocumentConverter.py from http://www.artofsolving.com/files/DocumentConverter.py
#
# Copyright (C) 1996-2008 SUSE Linux Products GmbH, Nuernberg, Germany.
#
# Author:  Jan Holesovsky <kendy@suse.cz>, 2008
#          Petr Mladek <kendy@suse.cz>, 2008, 2011
#
# Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl-2.1.html
# - or any later version.

document_converter_py=/usr/share/libreoffice/program/DocumentConverter.py

function usage()
{
    cat << EOF
Converts the input file to the output file using LibreOffice.

Usage: ${0##*/} [--help] [--force] input output

Options:

    input   - input file
    output  - output file
    --help  - print this help and exit
    --force - force an already existing output file to be replaced

See "man loconvert" for more details.
EOF
}

OOCONV_INPUT=
OOCONV_OUTPUT=
OOCONV_FORCE=
while test ${#} -gt 0 ; do
    case "$1" in
	"--help")
	    usage
	    exit 1
	    ;;
	"--force")
	    OOCONV_FORCE=1
	    ;;
	*)
	    if test -n "$OOCONV_OUTPUT" ; then
		echo "Error: Too many argumetns!"
		exit 1;
	    fi
	    if test -z "$OOCONV_INPUT" ; then
		OOCONV_INPUT="$1"
	    else
		OOCONV_OUTPUT="$1"
	    fi
	    ;;
    esac
    shift
done

if test -z "$OOCONV_INPUT" ; then
    echo "Error: Input file is not defined."
    exit 1;
fi

if test -z "$OOCONV_OUTPUT" ; then
    echo "Error: Output file is not defined."
    exit 1;
fi

if test ! -f "$OOCONV_INPUT" ; then
    echo "Error: The input file does not exist: $OOCONV_INPUT"
    exit 1;
fi

if test -f "$OOCONV_OUTPUT" -a -z "$OOCONV_FORCE" ; then
    echo "Error: The out file already exists: $OOCONV_OUTPUT"
    echo "You might use the option \"--force\""
    exit 1;
fi

# detect the OOo installation
ooo_home=
soffice=`which soffice 2>/dev/null`
if test -L $soffice ; then
    soffice_path=`readlink $soffice`
    ooo_home=`dirname $soffice_path`/..
    # deal with relative links
    echo $ooo_home | grep -q -v "^\/" && ooo_home="$(dirname $soffice)/$ooo_home"
fi

# try some fallbacks
if test -z "$ooo_home" -o ! -d "$ooo_home" ; then
    for dir in /usr/lib64/libreoffice \
               /usr/lib/libreoffice \
               /usr/lib64/ooo3 \
	       /usr/lib/ooo3 ; do
	test -f "$dir/program/soffice" && ooo_home="$dir" && continue
    done
fi

if test -z "$ooo_home" ; then
    echo "Error: Unable to find LibreOffice instalation"
    exit 1;
fi

# start the OOo
echo "Starting LibreOffice..."
RUNNING=`ps | grep soffice.bin | grep -v grep`
[ -z "$RUNNING" ] || { echo "OOo is running, please close it first" ; exit 1 ; }

$ooo_home/program/soffice -accept="socket,port=8100;urp;" -norestore -nofirststartwizard -nologo -headless >/dev/null 2>&1 &

sleep 10

# run the tests
echo "Doing the conversion..."
export PYTHONPATH="$ooo_home/basis-link/program"
python $document_converter_py "$OOCONV_INPUT" "$OOCONV_OUTPUT"

# kill the OOo
killall -9 soffice.bin
openSUSE Build Service is sponsored by