#!/usr/bin/env python3

# ------------------------------------------------------------------------
# Copyright 2020 IBM Corp. All Rights Reserved.
#
# 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.
# ------------------------------------------------------------------------

""" List availabe overlay shares on NFS server """

# Global modules

import os
import sys

# Local modules

from modules.args    import getCommonArgsParser
from modules.command import CmdSsh
from modules.config  import getConfig
from modules.logger  import setupLogging

# Functions


def _getArgs():
    """ Get command line arguments """
    parser = getCommonArgsParser(
        'List availabe overlay shares on NFS server'
    )

    return parser.parse_args()

# ----------------------------------------------------------------------


def _main():

    args = _getArgs()

    setupLogging(args)

    config = getConfig(args.config_file)

    cmdSsh = CmdSsh(config.nfs.host, config.nfs.user)
    lssCmd = f"ls -ladtr --time-style=long-iso {config.nfs.overlayBase}/*-*-*-*-*"

    for line in cmdSsh.run(lssCmd, rcOk=(0, 1, 2)).out.split('\n'):
        if not line or line == '':
            continue
        (_d1, _d2, _d3, _d4, _d5, date, time, file) = line.split()
        print(f'{os.path.basename(file)} ({date} {time})')

# ----------------------------------------------------------------------


if __name__ == "__main__":
    _main()
    sys.exit(0)
