/* TABLIX, PGA highschool timetable generator                              */
/* Copyright ( C ) 2002 Tomaz Solc                                         */

/* 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 */
/*                                                                         */
/* Module contributed by Nick Robinson <np@bottlehall.co.uk                */
/* $Id: export_csv.c,v 1.4 2004/10/17 09:37:06 avian Exp $                 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

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

#ifdef HAVE_ICONV_H
#include <iconv.h>
#include <langinfo.h>
#endif

#include "data.h"
#include "xmlsup.h"
#include "gettext.h"

#define BUFFSIZE	256
char buff[BUFFSIZE];

#ifdef HAVE_ICONV
char buff2[BUFFSIZE];

char *get_dayname(int n) {
	struct tm t;
	char *a,*b;
	size_t c, d;
	iconv_t cd;

	cd=iconv_open("UTF-8", nl_langinfo(CODESET));

	t.tm_wday=n%5+1;
	strftime(buff, BUFFSIZE, "%a", &t);

	if(((int) cd)==-1) {
		return(buff);
	}

	a=buff;
	b=buff2;
	c=BUFFSIZE;
	d=BUFFSIZE;

	iconv(cd, &a, &c, &b, &d);

	iconv_close(cd);

	return(buff2);
}
#else
char *get_dayname(int n) {
	struct tm t;

	t.tm_wday=n+1;
	strftime(buff, BUFFSIZE, "%a", &t);

	return(buff);
}
#endif

void output_function( chromo *t, int *cpnt, int *tpnt, char *options, FILE* out, outputext *opnt )
{
	outputext
		*pnt2;

	int	cid, *pnt1, tid, sid, rid, day, per, time;

	fprintf( out, "\"%s\"\n", school_name );
	fprintf( out, "\"%s\"\n", school_address );
	fprintf( out, "\"%s\"\n", author );
	fprintf( out, "\"Grade\",%d\n", t->grade );
	fprintf( out, "\"Class\",\"Day\",\"Period\",\"Subject\",\"Teacher\",\"Room\"\n" );

	for ( day = 0; day < DAYS; day++ )
	{
		for ( cid = 0; cid < cmapnum; cid++ )
		{
			pnt1 = cpnt + cid * TIMES;
			pnt2 = opnt + cid * TIMES;

			for( per = 0; per < PERIODS; per++ )
			{
				time = day * PERIODS + per;

				if ( pnt1[time] != -1 )
				{
					if ( pnt2[time].status == 1 )
					{
						int i;
						for ( i = 0; i < pnt2[time].num; i++ )
						{
       							tid = tuplemap[pnt2[time].tuples[i]].tid;
							rid = t->inf[pnt2[time].tuples[i]].room;
							sid = tuplemap[pnt2[time].tuples[i]].sid;
							fprintf( out, "\"%d-%s\",\"%s\",%d,\"%s\",\"%s\",\"%s\"\n",
								cmap[cid].year, cmap[cid].name, get_dayname(day), per,
								smap[sid].title, tmap[tid].name, rmap[rid].id );
						}
					}
					else
					{
						tid=tuplemap[pnt1[time]].tid;
						rid=t->inf[pnt1[time]].room;
						sid=tuplemap[pnt1[time]].sid;

						fprintf( out, "\"%d-%s\",\"%s\",%d,\"%s\",\"%s\",\"%s\"\n",
							cmap[cid].year, cmap[cid].name, get_dayname(day), per,
							smap[sid].title, tmap[tid].name, rmap[rid].id );
					}
				}
			}
		}
	}
}

