#! /usr/bin/python3

# releasechecker.py
#
#  Copyright (c) 2010 Jonathan Thomas <echidnaman@kubuntu.org>
#
#  Author: Jonathan Thomas <echidnaman@kubuntu.org>
#
#  This program 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 2 of
#  the License or (at your option) version 3 or any later version
#  accepted by the membership of KDE e.V. (or its successor approved
#  by the membership of KDE e.V.), which shall act as a proxy
#  defined in Section 14 of version 3 of the license.
#
#  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

import sys, time
sys.path.insert(0, '/usr/lib/python3/dist-packages/')

from UpdateManager.Core.MetaRelease import MetaReleaseCore
from UpdateManager.Core.utils import init_proxy

if __name__ == "__main__":
    """ check for updates, if there are any say so """

    init_proxy()

    #FIXME: implement command line options for MetaReleaseCore args
    metaRelease = MetaReleaseCore(False, False)

    # MetaReleaseCore internally runs .download() in a thraed. It has however no
    # meaningful signal for when it is finished, so following the GTK update-manger's
    # behavior we check ever second.
    # WARNING: You must *not* call .download() or really any other function
    # until download==False. Otherwise unprotected members may lead to wrong
    # results and/or data corruption!
    # FIXME: If muon-notifier and a $muonapp run releasechecker at the same time
    # file level race conditions may occur and lead to incorrectly reported
    # available upgrade when really there was none.
    while metaRelease.downloading:
        time.sleep(1)
    new_dist = metaRelease.new_dist

    if new_dist is not None:
        sys.exit(0)
    else:
        sys.exit(1)
