#!/bin/sh
#
# OpenOffice.org (2.3+) headless server startup script
#
# (C) Copyright 2010 Luc Saffre 
# (based on http://svn.nuxeo.org/nuxeo/tools/ooo/oooctl
#  and http://code.google.com/p/openmeetings/wiki/OpenOfficeConverter
#  See also :doc:`/tickets/16`)
#
# This file is part of the Lino project.
# Lino is free software; you can redistribute it and/or modify 
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
# Lino is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with Lino; if not, see <http://www.gnu.org/licenses/>.

PORT=8100
HOST=127.0.0.1
SOFFICE=/usr/bin/soffice
PIDFILE=/var/run/openoffice-server.pid

set -e

start() {
    if [ -f $PIDFILE ]; then
      echo "OpenOffice headless server has already started."
      sleep 5
      return 1
    fi
    echo "Starting OpenOffice.org server"
    $SOFFICE "-accept=socket,host=$HOST,port=$PORT;urp;StarOffice.ServiceManager" -nologo -headless -nofirststartwizard >/dev/null 2>&1 &
    touch $PIDFILE
    echo "Done."
}

stop() {
    if [ -f $PIDFILE ]; then
      echo "Stopping OpenOffice headless server."
      killall -9 soffice && killall -9 soffice.bin
      rm -f $PIDFILE
      return 0
    fi
    echo "Openoffice headless server is not running."
    return 1
}


case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        [ "$?" == 0 ] && start
        ;;
    *)
        echo "usage: `basename $0` (start|stop|restart|help)"
        exit 1
        ;;
esac
exit 0