#!/usr/bin/env python

import io
import sys
import os
import shutil
import argparse
import ConfigParser


def load_config_file( configfile ):
    with open(configfile) as fd:
        rc = fd.read()
        config = ConfigParser.RawConfigParser(allow_no_value=True)
        config.readfp(io.BytesIO(rc))
        return config

def get_all_pkg_list( config ):

    '''CMD: debootstrap --print-debs --no-check-gpg --include=%s --components=main,non-free,contrib --arch=amd64 kui /tmp/123 http://10.1.10.21/server-dev '''
    cmd='debootstrap                       \
        --print-debs                       \
        --no-check-gpg                     \
        --include={str_pkgs}               \
        --components=main,non-free,contrib \
        --arch={str_arch} {str_codename} /tmp/123 {str_mirror} '.format( str_pkgs=config.pkgs , str_arch=config.arch , str_codename=config.codename, str_mirror=config.mirror )
    return os.popen( cmd ).read()

def download_pkg():

    APTTMP="/data/tmp"
    CODENAME="kui"
    ARCH="amd64"
    REPO_LIST="/etc/apt/sources.list"
    KEY_FILE="/etc/apt/trusted.gpg"
    isobuilder_dir=[ "apt", "apt-state", "apt/trusted.gpg.d", "apt/sources.list.d", "apt-state/lists", "apt-cache/archives" ]
    for d in isobuilder_dir:
        os.makedirs("/data/tmp/kui-amd64/"+d)

    os.mknod("/data/tmp/kui-amd64/"+"status")
    shutil.copy(REPO_LIST, "/data/tmp/kui-amd64/apt/")
    shutil.copy(KEY_FILE, "/data/tmp/kui-amd64/apt/")


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Deepin-ISO-builder - Create Custom install ISO')

    parser.add_argument("-c", "--config", dest="config", help="read the config files",                                         action="store")
    parser.add_argument("-a", "--arch" ,  dest="arch",   default="amd64", help="specify architecture ( i386,amd64 ...)",       action="store")
    parser.add_argument("-s", "--dist" ,  dest="dist",   default="kui",   help="specify distribution ( kui,sid... )",          action="store")
    parser.add_argument("-m", "--mirror", dest="repo",   default="http://10.1.10.21/server-dev", help="specify apt-mirror (http://url file://path )", action="store")

    parser.add_argument("--action",choices=["download_pkg", "create_repo", "create_iso"], help="download_pkg:download pkgs from soft repo; create_repo:create repo use download pkgs; create_iso:use local repo generate iso")

    args   = parser.parse_args()
    config = load_config_file("/data/codes/project/isobuilder/PY/isobuilder.conf")

    if args.action:
        if args.action == "download_pkg":
            #pkg_list = get_all_pkg_list( "gcc,deepin-minimal", "amd64", "kui", "http://10.1.10.21/server-dev/" )
            #print pkg_list
            #download_pkg()
        if args.action == "create_repo":
            print "create_repo:create repo use download pkgs;"
        if args.action == "create_iso":
            print "create custom install iso file"
    else:
        print "please give action an value"
