#!/usr/bin/env python3.8
# zyxelprometheus
# Copyright (C) 2020 Andrew Wilkinson
#
# 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 3 of the License, or
# (at your option) any later version.
#
# 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

from zyxelprometheus import get_arguments, login, logout, prometheus, scrape_xdsl, scrape_traffic, serve
from zyxelprometheus import InvalidArguments

def main():
    try:
        args = get_arguments(sys.argv[1:])
    except InvalidArguments as e:
        sys.stderr.write(f"Invalid Arguments: {e.args[0]}\n")
        sys.exit(1)

    if args.serve:
        serve(args)
    else:
        session, sessionkey = login(args.host, args.user, args.passwd)
        xdsl = scrape_xdsl(session, args.host) if not args.traffic_only else None
        traffic = scrape_traffic(session, args.host) if not args.xdsl_only else None

        if args.raw:
            if xdsl is not None:
                print(xdsl)
            if traffic is not None:
                print(traffic)
        else:
            print(prometheus(xdsl, traffic))

        logout(session, args.host, sessionkey)

if __name__ == "__main__":
    main()
