#!/usr/bin/python
# -*- python -*-
# -*- coding: utf-8 -*-
## Copyright (C) 2005 Red Hat, Inc.
## Copyright (C) 2005 Harald Hoyer <harald@redhat.com>

## 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 2 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., 675 Mass Ave, Cambridge, MA 02139, USA.


"""Parser for RPM Specfiles.

Usage: rpmspecfile.py [-v --verbose] [-l --sections] [-s --section=sectionname] [-n --name] [-p --packages] specfile ...
"""

__author__ = "Harald Hoyer <harald@redhat.com>"

import sys, string, os.path

PYRPMDIR = "/usr/share/pyrpm"
if not PYRPMDIR in sys.path:
    sys.path.append(PYRPMDIR)
from pyrpm.specfile import RpmSpecFile

def Usage():
    print """%s - Simple Specfile Parser
Usage: %s [-v --verbose] [-l --sections] [-s --section=sectionname] [-n --name] [-p --packages] specfile ...""" % (sys.argv[0], sys.argv[0])

if __name__ == '__main__':
    import getopt
    class BadUsage: pass
    filename = None
    section = None
    cmdline = sys.argv[1:]
    print_name = None
    print_packages = None
    print_sections = None

    try:
        opts, args = getopt.getopt(cmdline, "vhnlps:",
                                   [
                                    "verbose",
                                    "help",
                                    "packages",
                                    "name",
                                    "sections",
                                    "section=",
                                    ])
        for opt, val in opts:
            if opt == '-v' or opt == '--verbose':
                #Not used yet: verbose += 1
                continue

            if opt == '-s' or opt == '--section':
                section = val
                continue

            if opt == '-n' or opt == '--name':
                print_name = 1
                continue

            if opt == '-l' or opt == '--sections':
                print_sections = 1
                continue

            if opt == '-p' or opt == '--packages':
                print_packages = 1
                continue

            if opt == '-h' or opt == "?" or opt == '--help':
                Usage()
                sys.exit(0)

            raise BadUsage

    except (getopt.error, BadUsage):
        Usage()
        sys.exit(1)

    if len(args) == 0:
        Usage()
        sys.exit(1)

    for filename in args:
        spec = RpmSpecFile(filename)
        if print_name:
            print spec.getName()
        if print_packages:
            print string.join(spec.getPackages(), "\n")
        if print_sections:
            print string.join(spec.getSections(), "\n")
        if section:
            sys.stdout.write(spec.getSection(section))

# vim:ts=4:sw=4:showmatch:expandtab
