#!/usr/bin/python3
# -*- coding: utf-8 -*-

# MyConnector
# Copyright (C) 2014-2024 Evgeniy Korneechev <ek@myconnector.ru>

# This program is free software; you can redistribute it and/or
# modify it under the terms of the version 2 of the GNU General
# Public License as published by the Free Software Foundation.

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

from subprocess import check_output
from os import system
from sys import exit
import gettext
import locale

APP         = "myconnector"
MO_FOLDER   = "/usr/share/locale"
MSG         = "zenity --notification --window-icon=myconnector --text='X2GO"

locale.bindtextdomain(  APP, MO_FOLDER )
gettext.bindtextdomain( APP, MO_FOLDER )
gettext.textdomain( APP )
_ = gettext.gettext

def main():
    LOG = "~/.myconnector/logs/all.log"
    log = check_output( "sleep 7; tail -n1 %s 2>/dev/null; exit 0" % LOG,
                        shell=True, universal_newlines=True ).strip()
    if "interactive login" in log or "EOFError" in log:
        system( "%s\n%s'" % ( MSG, _( "Invalid username or password." ) ) )
        system( "pkill pyhoca-cli" )
    if "IndexError" in log:
        system( "%s\n%s'" % ( MSG, _( "The password was not specified." ) ) )
    exit( 0 )

if __name__ == "__main__":
    main()
