#!/usr/bin/python
# Copyright 2010 Canonical Ltd.
#
# This file is part of desktopcouch.
#
# desktopcouch is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation.
#
# desktopcouch 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with desktopcouch.  If not, see <http://www.gnu.org/licenses/>.
#
# Authors: Chad Miller <chad.miller@canonical.com>
#          Eric Casteleijn <eric.casteleijn@canonical.com>
#          Vincenzo Di Somma <vincenzo.di.somma@canonical.com>

"""Return on stdout the port that the personal couchdb is listening on.
As always, success returns a zero exit code, and an exception in Python
returns an exit code of one.  On error, stderr should have information
useful for debugging."""

# pylint: disable=C0103

from desktopcouch.application import platform
import sys
import os

DEVNULL = open(os.devnull, "w")
SAVED_STDOUT = sys.stdout
try:
    # Don't let any of our output interfere
    sys.stdout = DEVNULL
    PORT = platform.find_port()
finally:
    sys.stdout = SAVED_STDOUT
    DEVNULL.close()

print PORT
