#include "config.h"

#if !defined HAVE_GETOPT_LONG
#   include "mygetopt.h"
#elif defined HAVE_GETOPT_H
#   include <getopt.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <pipi.h>

#if defined HAVE_GETOPT_LONG
#   define mygetopt getopt_long
#   define myoptind optind
#   define myoptarg optarg
#   define myoption option
#endif

#define DEFAULT_WIDTH 120
#define DEFAULT_HEIGHT 90

#define MOREINFO "Try `%s --help' for more information.\n"

static void usage(void);

int main(int argc, char *argv[])
{
    char *srcname = NULL, *dstname = NULL;
    pipi_image_t *src, *dst;

    int i, w = DEFAULT_WIDTH, h = DEFAULT_HEIGHT, bpp = 24;

    for(;;)
    {
        int option_index = 0;
        static struct myoption long_options[] =
        {
            { "bpp",         1, NULL, 'b' },
            { "geometry",    1, NULL, 'g' },
            { "help",        0, NULL, 'h' },
            { NULL,          0, NULL, 0   },
        };
        int c = mygetopt(argc, argv, "b:g:h", long_options, &option_index);

        if(c == -1)
            break;

        switch(c)
        {
        case 'b':
            bpp = atoi(myoptarg);
            if(bpp != 32 && bpp != 24 && bpp != 16)
            {
                fprintf(stderr, "%s: invalid bpp -- %s\n", argv[0], myoptarg);
                return EXIT_FAILURE;
            }
            break;
        case 'g':
            w = atoi(myoptarg);
            if(strchr(myoptarg, 'x'))
                h = atoi(strchr(myoptarg, 'x') + 1);
            break;
        case 'h':
            usage();
            return EXIT_SUCCESS;
        default:
            fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
            printf(MOREINFO, argv[0]);
            return EXIT_FAILURE;
        }
    }

    for(i = myoptind; i < argc; i++)
    {
        if(!srcname)
            srcname = argv[i];
        else
            dstname = argv[i];
    }

    if(!srcname || !dstname)
    {
        fprintf(stderr, "%s: too few arguments\n", argv[0]);
        printf(MOREINFO, argv[0]);
        return EXIT_FAILURE;
    }

    src = pipi_load(srcname);
    if(!src)
    {
        fprintf(stderr, "%s: could not load `%s'\n", argv[0], srcname);
        return EXIT_FAILURE;
    }

    dst = pipi_resize_bicubic(src, w, h);
    if(bpp == 16)
        pipi_dither_24to16(dst);
    pipi_save(dst, dstname);
    pipi_free(src);
    pipi_free(dst);

    return 0;
}

static void usage(void)
{
    printf("Usage: genethumb [-b bpp] [-g geometry] SOURCE DESTINATION\n");
    printf("       genethumb -h | --help\n");
    printf("Create an image thumbnail.\n");
    printf("\n");
    printf("Mandatory arguments to long options are mandatory for short options too.\n");
    printf("  -b, --bpp <bits>          bits per pixel (32 or 16)\n");
    printf("  -g, --geometry <geometry> target geometry (default %ix%i)\n",
           DEFAULT_WIDTH, DEFAULT_HEIGHT);
    printf("  -h, --help                display this help and exit\n");
    printf("\n");
    printf("Written by Sam Hocevar. Report bugs to <sam@zoy.org>.\n");
}