#!/usr/bin/env python
#
#  MATLAB plugin, a search plugin for the OpenDiamond platform
#
#  Copyright (c) 2011 Carnegie Mellon University
#  All rights reserved.
#
#  MATLAB plugin 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, version 2.
#
#  MATLAB plugin is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with MATLAB plugin. If not, see <http://www.gnu.org/licenses/>.
#
#  Linking MATLAB plugin statically or dynamically with other modules is
#  making a combined work based on MATLAB plugin. Thus, the terms and
#  conditions of the GNU General Public License cover the whole
#  combination.
#
#  In addition, as a special exception, the copyright holders of
#  MATLAB plugin give you permission to combine MATLAB plugin with free
#  software programs or libraries that are released under the GNU LGPL or
#  the Eclipse Public License 1.0. You may copy and distribute such a system
#  following the terms of the GNU GPL for MATLAB plugin and the licenses of
#  the other code concerned, provided that you include the source code of
#  that other code when and as the GNU GPL requires distribution of source
#  code.
#
#  Note that people who make modified versions of MATLAB plugin are not
#  obligated to grant this special exception for their modified versions;
#  it is their choice whether to do so. The GNU General Public License
#  gives permission to release a modified version without this exception;
#  this exception also makes it possible to release a modified version
#  which carries forward this exception.
#

import os
from opendiamond.bundle import make_zipfile
from optparse import OptionParser
import sys

def clean_macro_name(name):
    if name is None:
        return '1'
    else:
        return os.path.splitext(os.path.basename(name))[0]


if __name__ == '__main__':
    parser = OptionParser(
        usage = '%prog [-h] [options] eval-macro [supporting-files...]',
        description = 'Package a Diamond filter written in MATLAB into an OpenDiamond filter bundle.',
    )
    parser.add_option('-o', dest = 'outfile', metavar = 'outfile',
            help = 'path to output file')
    parser.add_option('-n', dest = 'name', metavar = 'display-name',
            default = 'MATLAB Filter',
            help = 'display name for filter')
    parser.add_option('-i', dest = 'init_file', metavar = 'init-macro',
            help = 'path to initialization macro')
    opts, args = parser.parse_args()
    if len(args) < 1:
        parser.error('no eval macro specified')
    eval_file = args[0]
    init = clean_macro_name(opts.init_file)
    eval = clean_macro_name(eval_file)
    outfile = opts.outfile
    if outfile is None:
        outfile = os.path.splitext(eval_file)[0] + '.zip'
    files = list(args)
    if opts.init_file is not None:
        files.append(opts.init_file)
    files = dict([(os.path.basename(f), f) for f in files])

    manifest = {
        'Plugin': 'MATLAB',
        'Name': opts.name,
        'InitFunction': init,
        'EvalFunction': eval,
    }
    manifest = ''.join(['%s: %s\n' % (k, v) for k, v in manifest.items()])

    try:
        make_zipfile(outfile, manifest, files)
    except Exception, e:
        parser.error(str(e))
