#!/usr/bin/python
# -*- 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 sys
import os
from zipfile import BadZipfile

import pisi
from pisi.archive import ArchiveZip, ArchiveTar
import pisi.context as ctx
import pisi.util as util

def usage(errmsg):
    print """
    Error: %s
    
    Usage:
      unpisi PiSi_package.PiSi
    """ % (errmsg)

    sys.exit(1)

def main():

    if len(sys.argv) < 2:
        usage("PiSi package required..")

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

    try:
        arc = ArchiveZip(sys.argv[1], 'zip', 'r')
    except BadZipfile, e:
        print e
        sys.exit(1)

    arc.unpack_files(['files.xml', 'metadata.xml'], '.')
    arc.unpack_files("install.tar.lzma", ctx.config.tmp_dir())
    arc.unpack_dir('comar', '.')

    tar_file = util.join_path(ctx.config.tmp_dir(), "install.tar.lzma")
    tar = ArchiveTar(tar_file, 'tarlzma')
    tar.unpack_dir('.')

    os.unlink(tar_file)
    return 0

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