#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2006, TUBITAK/UEKAE
#
# 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.
#
# Please read the COPYING file.

import os
import sys
import inary

def show_info(filename):
    metadata, files = inary.api.info_file(filename)

    paths = [fileinfo.path for fileinfo in files.list]
    paths.sort()
    return paths

def uniq(alist):
    set = {}
    return [set.setdefault(e, e) for e in alist if e not in set]

def usage(errmsg):
    print(("""
    Error: %s

    Usage:
      lsinary INARY_package.INARY         (lists the content of package)
      lsinary dirs INARY_package.INARY    (lists directories in the package for the package developer)
    """ % (errmsg)))

    sys.exit(1)


def main():
    if len(sys.argv) < 2 or ("dirs" in sys.argv and len(sys.argv) < 3):
        usage("INARY package required...")

    if sys.argv[1] == "dirs":
        dirlist = []
        for file in show_info(sys.argv[2]):
            dirlist.append(os.path.dirname(file))

        for dir in uniq(dirlist):
            print(("<Path fileType=\"\">/%s</Path>" % dir))

    elif not os.path.exists(sys.argv[1]):
        print("File %s not found" % sys.argv[1])

    else:
        for file in show_info(sys.argv[1]):
            print(("/%s" % file))

if __name__ == "__main__":
    sys.exit(main())
