#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Started 18/05/2011 by Seblu <seblu@seblu.net>

'''
InstallSystems Installation Tool
'''

import os
import argparse
import installsystems
import installsystems.tools as istools
from installsystems.printer import *
from installsystems.repository import RepositoryCache
from installsystems.image import PackageImage

class DebugAction(argparse.Action):
    '''Set installsystems in debug mode. Argparse callback'''
    def __call__(self, parser, namespace, values, option_string=None):
        if installsystems.debug == False:
            installsystems.debug = True
            debug("Debug on")


# Top level argument parsing
p_main = argparse.ArgumentParser()
p_main.add_argument("-V", "--version", action="version", version=installsystems.version,
                    help="show installsystems version")
p_main.add_argument('-d', "--debug", action=DebugAction, nargs=0,
                    help="active debug mode")
p_main.add_argument('-q', "--quiet", action="store_false", dest="verbose", default=True,
                    help="active quiet mode")
p_main.add_argument("-I", "--image-path", dest="image_path", type=str, default=None,
                    help="remote image repository path")
p_main.add_argument("-D", "--data-path", dest="data_path", type=str, default=None,
                    help="remote data repository path")
p_main.add_argument("-C", "--cache-path", dest="cache_path", type=str,
                    default="/var/cache/isinstall",
                    help="local cache repository path")
p_main.add_argument("-v", "--image-version", dest="image_version", type=int, default=None,
                    help="specific image version")
p_main.add_argument('-u', "--update", action="store_true", dest="update", default=False,
                    help="update repository cache")
p_main.add_argument("image_name", type=str,
                    help="image to install (path or name)")

# program entry point
try:
    # Partial parse
    args = p_main.parse_known_args()[0]
    # looks if arguments is a file or image name
    image_name_type = istools.get_path_type(args.image_name)
    if image_name_type == "file":
        pkg = PackageImage(istools.complete_path(args.image_name))
    elif image_name_type == "name":
        # init repo cache object
        repocache = RepositoryCache(args.cache_path, verbose=args.verbose)
        # register command ligne repo
        if args.image_path is not None:
            repocache.register("cmdline", args.image_path, args.data_path)
        # update remote info if -u options is present
        if args.update:
            repocache.update()
        # get image package
        pkg = repocache.get(args.image_name, args.image_version)
    else:
        raise Exception("Invalid image name")
    # run parser scripts
    pkg.run_parser({ "parser": p_main })
    # call parser again, with extended attributes
    args = p_main.parse_args()
    # run setup scripts
    pkg.run_setup({"args": args})
except Exception as e:
    error(e)
except KeyboardInterrupt:
    warn("Keyboard Interrupted")