#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# cppman.py
#
# Copyright (C) 2010 -  Wei-Ning Huang (AZ) <aitjcize@gmail.com>
# All Rights reserved.
#
# This file is part of cppman.
#
# 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, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#



import os
import sys
from optparse import OptionParser, make_option


program = sys.argv[0]
LAUNCH_DIR = os.path.dirname(os.path.abspath(sys.path[0]))

# If launched from source directory
if program.startswith('./') or program.startswith('bin/'):
    sys.path.insert(0, LAUNCH_DIR)

from cppman.main import Cppman
from cppman.environ import config
from cppman.util import update_mandb_path, update_man3_link

program_name = sys.argv[0]
program_version = '0.5.9'


def version():
    sys.stderr.write(
        """%s Ver %s
Copyright (C) 2010 Wei-Ning Huang
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.\n
Written by Wei-Ning Huang (AZ) <aitjcize@gmail.com>.\n"""
        % (program_name, program_version))


def main():
    option_list = [
        make_option('-s', '--source', action='store', dest='source',
                    help="Select source, either 'cppreference.com' or "
                         "'cplusplus.com'. Default is 'cppreference.com'."),
        make_option('-c', '--cache-all', action='store_true',
                    dest='cache_all', default=False,
                    help='Cache all available man pages from cppreference.com '
                         'and cplusplus.com to enable offline browsing.'),
        make_option('-C', '--clear-cache', action='store_true',
                    dest='clear_cache', default=False,
                    help='Clear all cached files.'),
        make_option('-f', '--find-page', action='store', type='string',
                    dest='keyword', default=None,
                    help='Find man page.'),
        make_option('-o', '--force-update', action='store_true',
                    dest='force', default=False,
                    help="Force cppman to update existing cache when "
                    "'--cache-all' or browsing man pages that were already "
                    "cached."),
        make_option('-m', '--use-mandb', action='store', dest='mandb',
                    help="Accepts 'true' or 'false'. If true, cppman adds "
                    "manpage path to mandb so that you can view C++ manpages "
                    "with `man' command. The default value is 'false'."),
        make_option('-p', '--pager', action='store', dest='pager',
                    help="Select pager to use, accepts 'vim', 'nvim', 'less'"
                    "or 'system'. 'system' uses $PAGER environment as pager. "
                    "The default value is 'vim'."),
        make_option('-r', '--rebuild-index', action='store_true',
                    dest='rebuild_index', default=False,
                    help="rebuild index database for the selected source, "
                    "either 'cppreference.com' or 'cplusplus.com'."),
        make_option('-v', '--version', action='store_true', dest='version',
                    default=False, help='Show version information.'),
        make_option('--force-columns', action='store', dest='force_columns',
                    type=int, default=-1, help='Force terminal columns.')
    ]

    parser = OptionParser(
        usage='Usage: cppman [OPTION...] PAGE...', option_list=option_list)

    options, args = parser.parse_args()

    if options.version:
        version()
        sys.exit(0)

    if options.cache_all:
        cm = Cppman(options.force)
        cm.cache_all()
        sys.exit(0)

    cm = Cppman()

    if options.clear_cache:
        cm.clear_cache()
        sys.exit(0)

    if options.keyword:
        cm.find(options.keyword)
        sys.exit(0)

    if options.source:
        if options.source not in config.SOURCES:
            raise Exception("invalid value `%s' for option `--source'" %
                            options.source)
        else:
            config.Source = options.source
            update_man3_link()
            print("Source set to `%s'." % options.source)
            sys.exit(0)

    if options.pager:
        if options.pager not in config.PAGERS:
            raise Exception("invalid value `%s' for option `--pager'" %
                            options.pager)
        else:
            config.Pager = options.pager
            print("Pager set to `%s'." % options.pager)
            sys.exit(0)

    if options.mandb:
        if options.mandb not in ('true', 'false'):
            raise Exception("invalid value `%s' for option `--use-mandb'" %
                            options.mandb)
        config.UpdateManPath = config.parse_bool(options.mandb)
        update_mandb_path()
        update_man3_link()
        sys.exit(0)

    if options.rebuild_index:
        cm.rebuild_index()
        sys.exit(0)

    if not args or len(args) == 0:
        sys.stderr.write('What manual page do you want?\n')
        sys.exit(1)

    keyword = cm.fuzzy_find(args[0])
    if not keyword:
        sys.exit(1)

    try:
        pid = cm.man(keyword)
    except RuntimeError as e:
        print(e, file=sys.stderr)
        sys.exit(16)
    else:
        os.waitpid(pid, 0)

if __name__ == '__main__':
    try:
        main()
    except BrokenPipeError:
        sys.exit()
    except (Exception, KeyboardInterrupt) as e:
        if type(e) == KeyboardInterrupt:
            print('\nAborted.', file=sys.stderr)
        else:
            print('error:', e, file=sys.stderr)
