#!/bin/sh

# Generate new development directory for image format
#
# Usage:
#  $ ./generate <format>
#  


name=$1
mkdir $name
mkdir include > /dev/null 2>&1
cd $name

cat << EOF > Makefile.am
INCLUDES = -I.. -I../include

lib_LTLIBRARIES = lib${name}.la

lib${name}_la_SOURCES = fmt_codec_${name}.cpp fmt_codec_${name}_defs.h

lib${name}_la_LDFLAGS = -version-info 0:0:0
EOF

cat << EOF > ../include/fmt_codec_${name}.h
/*  This file is part of ksquirrel-libs (http://ksquirrel.sf.net)

    Copyright (c) 2005 Dmitry Baryshev <ksquirrel@tut.by>

    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_LIBS_CLASS_DEFINITION_${name}_H
#define KSQUIRREL_LIBS_CLASS_DEFINITION_${name}_H

#include "fmt_codec_base.h"

class fmt_codec : public fmt_codec_base
{
    public:

        fmt_codec();
        ~fmt_codec();

        virtual std::string     fmt_version();
        virtual std::string     fmt_quickinfo();
        virtual std::string     fmt_filter();
        virtual std::string     fmt_mime();
        virtual std::string     fmt_pixmap();
        virtual std::string     fmt_extension(const s32 bpp);

	virtual bool    fmt_readable() const;
        virtual s32     fmt_read_init(const std::string &file);
        virtual s32     fmt_read_next();
        virtual s32     fmt_read_next_pass();
        virtual s32     fmt_read_scanline(RGBA *scan);
        virtual void    fmt_read_close();

        virtual bool    fmt_writable() const;
        virtual void    fmt_getwriteoptions(fmt_writeoptionsabs *);

        virtual s32     fmt_write_init(const std::string &file, const fmt_image &image, const fmt_writeoptions &opt);
	virtual s32     fmt_write_next();
	virtual s32     fmt_write_next_pass();
	virtual s32     fmt_write_scanline(RGBA *scan);
        virtual void    fmt_write_close();

    private:
	// define variables you need here
};

#endif
EOF

cat << EOF > fmt_codec_${name}_defs.h
/*  This file is part of ksquirrel-libs (http://ksquirrel.sf.net)

    Copyright (c) 2005 Dmitry Baryshev <ksquirrel@tut.by>

    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_CODEC_DEFS_${name}
#define KSQUIRREL_CODEC_DEFS_${name}

// define constants here

#endif
EOF

cat << EOF > fmt_codec_${name}.cpp
/*  This file is part of ksquirrel-libs (http://ksquirrel.sf.net)

    Copyright (c) 2005 Dmitry Baryshev <ksquirrel@tut.by>

    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 <iostream>

#include "fmt_types.h"
#include "fileio.h"

#include "fmt_codec_${name}_defs.h"
#include "fmt_codec_${name}.h"

#include "error.h"
EOF

cat << EOF >> fmt_codec_${name}.cpp

fmt_codec::fmt_codec() : fmt_codec_base()
{}

fmt_codec::~fmt_codec()
{}

std::string fmt_codec::fmt_version()
{
    return std::string("0.1.0");
}

std::string fmt_codec::fmt_quickinfo()
{
    return std::string("");
}

std::string fmt_codec::fmt_filter()
{
    return std::string("*. ");
}

std::string fmt_codec::fmt_mime()
{
    return std::string();
}

std::string fmt_codec::fmt_pixmap()
{
    return std::string("");
}

s32 fmt_codec::fmt_read_init(const std::string &file)
{
    frs.open(file.c_str(), ios::binary | ios::in);

    if(!frs.good())
        return SQE_R_NOFILE;

    currentImage = -1;
    read_error = false;

    finfo.animated = false;

    return SQE_OK;
}

s32 fmt_codec::fmt_read_next()
{
    currentImage++;

    if(currentImage)
        return SQE_NOTOK;

    fmt_image image;

/*
    image.w = 
    image.h = 
    image.bpp = 
*/

    image.compression = "";
    image.colorspace = "";

    finfo.image.push_back(image);

    return SQE_OK;
}

s32 fmt_codec::fmt_read_next_pass()
{
    return SQE_OK;
}

s32 fmt_codec::fmt_read_scanline(RGBA *scan)
{
    RGB rgb;
    RGBA rgba;
    fmt_image *im = image(currentImage);

    memset(scan, 255, im->w * sizeof(RGBA));


    return SQE_OK;
}

void fmt_codec::fmt_read_close()
{
    frs.close();

    finfo.meta.clear();
    finfo.image.clear();
}

void fmt_codec::fmt_getwriteoptions(fmt_writeoptionsabs *opt)
{
    opt->interlaced = false;
    opt->passes = 1;
    opt->compression_scheme = CompressionNo;
    opt->compression_min = 0;
    opt->compression_max = 0;
    opt->compression_def = 0;
    opt->needflip = false;
    opt->palette_flags = 0 | fmt_image::pure32;
}

s32 fmt_codec::fmt_write_init(const std::string &file, const fmt_image &image, const fmt_writeoptions &opt)
{
    if(!image.w || !image.h || file.empty())
        return SQE_W_WRONGPARAMS;

    writeimage = image;
    writeopt = opt;

    fws.open(file.c_str(), ios::binary | ios::out);

    if(!fws.good())
        return SQE_W_NOFILE;

    return SQE_OK;
}

s32 fmt_codec::fmt_write_next()
{
    return SQE_OK;
}

s32 fmt_codec::fmt_write_next_pass()
{
    return SQE_OK;
}

s32 fmt_codec::fmt_write_scanline(RGBA * /*scan*/)
{
    return SQE_OK;
}

void fmt_codec::fmt_write_close()
{
    fws.close();
}

bool fmt_codec::fmt_writable() const
{
    return false;
}

bool fmt_codec::fmt_readable() const
{
    return true;
}

std::string fmt_codec::fmt_extension(const s32 /*bpp*/)
{
    return std::string("");
}

#include "fmt_codec_cd_func.h"
EOF

echo
echo "All done!"
echo
echo "Don't forget to insert your copyrights"
echo
