#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright 2013-2016 Mir Calculate. http://www.calculate-linux.org
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import sys
import __future__

import gobject

import dbus
import os
from os import path
import dbus.mainloop.glib
from calculate.lib.datavars import DataVars, VariableError
from calculate.client.client import Client
from argparse import ArgumentParser
from time import sleep


class ResourceRemounter:
    """
    Object contains one method for handle 'Resuming' dbus signal.
    
    'handle_resuming' method checks and remounts user remote resources.
    Also the object refreshes NSS-cache.
    """
    nscd_refresh = "/usr/sbin/nscd-refresh"

    def __init__(self, dv):
        self.dv = dv
        self.dv.defaultModule = 'client'
        self.client = Client()
        self.client.clVars = dv
        self.check_server(True)

    def check_server(self, refresh_nscd=False):
        """
        Check the available of domain server and refresh nscd cache
        """
        if self.client.checkDomainServer(
                self.dv.Get('cl_remote_host'),
                self.dv.Get('os_net_domain')):
            if refresh_nscd and path.exists(self.nscd_refresh):
                os.system(self.nscd_refresh)
            return True
        return False

    def remount_remote(self):
        """
        Remount remote resource of the domain
        """
        try:
            self.client.mountRemoteRes(self.dv.Get('cl_remote_pw'),
                                       self.dv.Get('cl_client_remote_path'),
                                       self.dv.Get('cl_remote_host'))
        except Exception:
            pass

    def remount_user_resources(self):
        """
        Remount user resource of the domain
        """
        try:
            self.client.mountUserDomainRes(self.dv.Get('ur_login'),
                                           self.dv.Get('desktop.ur_password'),
                                           int(self.dv.Get('ur_uid')),
                                           int(self.dv.Get('ur_gid')),
                                           "unix", "share", "homes", "ftp")
        except Exception:
            pass

    def handle_resuming(self, statename):
        """
        Handle PrepareForSleep statename(True) sleep, statename(False) resume
        """
        resume = not bool(statename)
        print("Restoring remote mounts for user %s" % self.dv.Get('ur_login'))
        if resume:
            # lazy umount domain user resources, and remote
            for rpath in self.dv.Get('client.cl_client_user_mount_path'):
                if rpath:
                    os.system("/bin/umount -l %s &>/dev/null"%rpath)
            os.system("/bin/umount -l %s &>/dev/null"%"/var/calculate/remote")

            # waiting for the domain
            for wait in [1, 2, 5, 5, 10, 10, 15]:
                try:
                    if self.check_server(True):
                        break
                except Exception:
                    pass
                sleep(wait)

            # wait for netmount
            netmount_stopping = '/run/openrc/stopping/netmount'
            for wait in range(0, 20):
                if not path.exists(netmount_stopping):
                    break
                sleep(1)

            # check and remount remote resources
            self.remount_remote()
            self.remount_user_resources()


def main(argv):
    # processing the user argument
    argp = ArgumentParser(add_help=True)
    argp.add_argument("user", help="tracked user", metavar="USER")
    namespace = argp.parse_args(argv[1:])

    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

    bus = dbus.SystemBus()
    # initialization calculate datavars and quitting on an error
    try:
        dv = DataVars()
        dv.importData()
        dv.flIniFile()
        dv.Set('ur_login', namespace.user)
        # pre-calculate values
        for vn in ("ur_uid", "ur_gid",
                   "client.cl_client_user_mount_path",
                   "client.cl_client_user_mount_resource",
                   "client.cl_client_user_mount_host",
                   "client.cl_client_user_mount_name"):
            dv.Get(vn)
        if dv.Get('desktop.ur_domain_set') == 'off':
            print("%s isn't a domain user" % namespace.user)
            sys.exit(0)
    except VariableError as e:
        sys.stderr.write(str(e) + '\n')
        sys.exit(1)

    rm = ResourceRemounter(dv)
    bus.add_signal_receiver(rm.handle_resuming,
                            dbus_interface="org.freedesktop.ConsoleKit.Manager",
                            signal_name="PrepareForSleep")
    loop = gobject.MainLoop()
    context = loop.get_context()
    while True:
        # need for dbus processing
        context.iteration(1)
        sleep(1)


if __name__ == '__main__':
    main(sys.argv)
