#!/usr/bin/env python
# -*- Mode: Python; tab-width: 4 -*-
#
# dir2inf.py directory to pebuilder inf format
# Copyright (C) 2003 Sherpya <sherpya@hotmail.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, 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 MERCHANTIBILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
# ======================================================================
__version__ = '0.1'

from sys import argv, exit, platform
from os import sep
from string import letters
import os.path

BS='\\'
LF='\n'

COMPACT=1

global dirs
dirs = []
base = "Programs"

def walk_callback(args, dirname, names):
    files = []
    for name in names:
        if os.path.isfile(os.path.join(dirname, name)):
            files.append(name)

    dirs.append((dirname, files))

### Linux/Windows cmdline interface
def cmdline(argv):
    if len(argv) < 2:
        print "Usage: %s plugin_directory" % argv[0]
        exit()

    print generate(argv[1])
    exit()

def generate(directory):
    if directory.endswith(sep):
        directory = directory[:-1]

    os.path.walk(directory, walk_callback, None)

    out = "; Automatic generated by dir2inf.py" + LF +LF
    out += "[WinntDirectories]" + LF
    files = []
    pkg = BS.join([base, os.path.basename(directory)])

    start = 0
    for dir in dirs:
        basedir = dir[0].replace(directory, '')
        basedir = basedir.split(sep)[1:]
        letter = letters[start % len(letters)]
        out += letter + '='
        out += BS.join([pkg]+basedir[1:])
        out += ',2' + LF

        if COMPACT:
            if len(dir[1]):
                files.append(BS.join(basedir + ['*.*']) + '=' + letter + ',,1')
        else:
            for file in dir[1]:
                filename = BS.join(basedir+[file]) + '=' + letter + ',,1'
                files.append(filename)

        start+=1

    out+= LF+"[SourceDisksFiles]"+LF

    for file in files:
        out+= file + LF
    return out
    
### Pseudo gui only on win32 + ActiveState python
def gui(argv):    
    from win32ui import CreateFileDialog
    from win32con import OFN_HIDEREADONLY, OFN_OVERWRITEPROMPT, OFN_PATHMUSTEXIST
    from win32com.shell import shell
    from codecs import utf_16_decode
    import os.path

    if len(argv) < 2:
        idl, name, temp = shell.SHBrowseForFolder(0, None , "Select the plugin folder" , 0 , None , 0 )
        if idl is None:
            exit()
        directory = shell.SHGetPathFromIDList(idl)
        filename = name + '.inf' 
    else:
        directory = argv[1]
        filename = os.path.dirname(directory) + inf

    
    dialog = CreateFileDialog(0, "inf" , filename, OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST, "PE Builder inf (*.inf)|*.inf|All Files (*.*)|*.*|", None )
    res = dialog.DoModal()
    if res != 1:
        exit()

    file_out = os.path.join(dialog.GetPathName(), dialog.GetPathName())
    del dialog
    inf = generate(directory)
    open(file_out, 'w').write(inf)        

if __name__ == '__main__':
    if platform != 'win32':
        cmdline(argv)
    gui(argv)
