#!/bin/sh

name=$1
mkdir $name
cd $name

cat << EOF > Makefile.am
lib_LTLIBRARIES = libSQ_read_$name.la
libSQ_read_${name}_la_SOURCES = read_$name.cpp read_$name.h
libSQ_read_${name}_la_LIBDADD = 
EXTRA_DIST = read_$name.cpp read_$name.h
AM_LDFLAGS = -version-info 1:1:1
EOF

cat << EOF > read_$name.h
/*  This file is part of KSquirrel's (http://ksquirrel.sf.net) libraries

    Copyright (c) 2004 Dmitry Baryshev <squirrel-sf@yandex.ru>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation;
    either version 2 of the License, or (at your option) any later
    version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.
*/

#ifndef KSQUIRREL_READ_IMAGE_$name
#define KSQUIRREL_READ_IMAGE_$name

#include "../defs.h"
#include "../err.h"

extern "C" {

const char*	fmt_version();
const char*	fmt_quickinfo();
const char*	fmt_filter();
const char*	fmt_mime();
const char*	fmt_pixmap();

int 	fmt_init(fmt_info **finfo, const char *file);
int	fmt_read_info(fmt_info *finfo);
int	fmt_read_scanline(fmt_info *finfo, RGBA *scan);
int	fmt_readimage(const char*, RGBA **scan, char **);
int	fmt_close();

void    fmt_next(fmt_info *finfo);
}

#endif
EOF

cat << EOF > read_$name.cpp
/*  This file is part of KSquirrel's (http://ksquirrel.sf.net) libraries

    Copyright (c) 2004 Dmitry Baryshev <squirrel-sf@yandex.ru>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation;
    either version 2 of the License, or (at your option) any later
    version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.
*/

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

#include "read_mng.h"
#include <libmng.h>

FILE *fptr;
int bytes;

const char* fmt_version()
{
    return (const char*)"0.1";
}

const char* fmt_quickinfo()
{
    return (const char*)"Multiple Network Graphics";
}

const char* fmt_filter()
{
    return (const char*)"*.mng *.jng";
}

const char* fmt_mime()
{
/*  QRegExp pattern  */
    return (const char*)"\x008A\x004D\x004E\x0047\x000D\x000A\x001A\x000A";
}

const char* fmt_pixmap()
{
    return (const char*)"";
}

void fmt_next(fmt_info *finfo)
{
	finfo = finfo;
}

int fmt_init(fmt_info **finfo, const char *file)
{
    *finfo = (fmt_info*)calloc(1, sizeof(fmt_info));

    if(!*finfo)
        return SQERR_NOMEMORY;

    (*finfo)->images = 1;
    (*finfo)->image = (fmt_image *)calloc(1, sizeof(fmt_image));

    if(!(*finfo)->image)
        return SQERR_NOMEMORY;

    fptr = fopen(file, "rb");

    if(!fptr)
    {
        free((*finfo)->image);
        free(*finfo);
        return SQERR_NOFILE;
    }
    
    return SQERR_OK;
}

int fmt_read_info(fmt_info *finfo)
{
    bytes = (finfo->image[0].w * finfo->image[0].h) << 2;

    char type[25];

    asprintf(&finfo->image[0].dump, "%s\n%dx%d\n%d\n%s\nJPEG\n%d\n%d\n",
    fmt_quickinfo(),
    finfo->image[0].w, finfo->image[0].h,
    finfo->image[0].bpp,type,
    finfo->images,
    bytes);


    return SQERR_OK;
}

/*  
 *    reads scanline
 *    scan should exist, e.g. RGBA scan[N], not RGBA *scan  
 */
int fmt_read_scanline(fmt_info *finfo, RGBA *scan)
{

    return SQERR_OK;
}

int fmt_readimage(const char *file, RGBA **image, char **dump)
{
    return SQERR_OK;
}

int fmt_close(fmt_info *finfo)
{
    fclose(fptr);

    return SQERR_OK;
}
EOF

echo
echo "All done!"
echo
