/* $Id: cli.c,v 1.6 2004/04/08 20:27:29 play_fair Exp $
**
** 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.
**
** This program 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 this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
** 02111-1307, USA.
*/

#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>

#include "playfair.h";

#define OUTPUT_FILEXT ".m4a"

int die(void *udata, u_int8_t err, ...) {
    va_list argp;
    va_start(argp, err);
    vfprintf(stderr, ERROR_MESSAGES[err], argp);
    va_end(argp);
    fprintf(stderr, "\n");

    return err;
}

void logmsg(void *udata, u_int8_t level, char *str) {
    fprintf(stderr, "playfair(%d): %s\n", level, str);
}

void status(void *udata, char *status) {
    fprintf(stderr, "%s", status);
}

static const char* ERR_USAGE =
    "USAGE: %s [-at] "
    "<infile> <outfile>\n"
/*    "[-x ext] " */
/*    "<infile1> [ [infile2] ... [infileN] ] <destdir>\n" */
    "\t-a        don't copy artwork\n"
    "\t-t        don't copy tags\n"
/*    "\t-x [ext]  use ext as the file extension for output files" */
    "\n";
int usage(char *prog) {
    fprintf(stderr, ERR_USAGE, prog);
}

/*
char* get_outfile(char *infile, char *ext, char *destdir) {
    char *ext_start = rindex(outfile, '.') + 1;
}
*/

int main(int argc, char *argv[]) {
    playfair_callback_t *pfcb = NULL;
    int err, opt;
    bool copyart = TRUE;
    bool copytags = TRUE;
    char *prog = NULL, *infile = NULL, *outfile = NULL, *ext = OUTPUT_FILEXT;

    prog = argv[0];
    if (argc > 1) {
        while ((opt = getopt(argc, argv, "atx:")) != -1) {
            switch(opt) {
            case 'a':
                copyart = FALSE;
                break;
            case 't':
                copytags = FALSE;
                break;
            case 'x':
                ext = optarg;
                break;
            case 'h':
            case '?':
            default:
                return usage(prog);
            }
        }
        argc -= optind;
        argv += optind;

        infile = argv[0];
        outfile = argv[1];

        pfcb = malloc(sizeof(playfair_callback_t));
        pfcb->log = logmsg;
        pfcb->die = die;
        pfcb->status = status;
        pfcb->user_data = NULL;

        if ((err = convert(pfcb, infile, outfile, copytags, copyart))) {
            if (pfcb) free(pfcb);
            return die(NULL, err);
        }

    } else {
        if (pfcb) free(pfcb);
        return usage(prog);
    }

    if (pfcb) free(pfcb);
    return SUCCESS;
}

/* EOF */
