/*

UniMod V5 exporter V1.0, written by Tinic Urou

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mikmod.h"
#include "bemain.h"

void SaveUNI(UNIMOD *m, char *filename)
{
/*	FILE *file;
	UWORD len;
	
	extern SBYTE *Samples[];
	
	if(file=fopen(filename,"w"))
	{
		_mm_write_UBYTE('U',file);
		_mm_write_UBYTE('N',file);
		_mm_write_UBYTE('0',file);
		_mm_write_UBYTE('5',file);
		
		_mm_write_UBYTE(m->numchn,file);
		_mm_write_I_UWORD(m->numpos,file);
		_mm_write_I_UWORD(m->reppos,file);
		_mm_write_I_UWORD(m->numpat,file);
		_mm_write_I_UWORD(m->numtrk,file);
		_mm_write_I_UWORD(m->numins,file);
		_mm_write_UBYTE(m->initspeed,file);
		_mm_write_UBYTE(m->inittempo,file);
		_mm_write_UBYTES(m->positions,256,file);
		_mm_write_UBYTES(m->panning,32,file);
		_mm_write_UBYTE(m->flags,file);
	
		if(m->songname)len=strlen(m->songname); else len=0;
		_mm_write_I_UWORD(len,file);
		if(len)fwrite(m->songname,len,1,file);
		if(m->modtype)len=strlen("UniMod V5"); else len=0;
		_mm_write_I_UWORD(len,file);
		if(len)fwrite("UniMod V5",len,1,file);
		if(m->comment)len=strlen(m->comment); else len=0;
		_mm_write_I_UWORD(len,file);
		if(len)fwrite(m->comment,len,1,file);
		
		for(int t=0;t<m->numins;t++)
		{
			INSTRUMENT	*i=&m->instruments[t];	
			
			_mm_write_UBYTE(i->numsmp,file);
			_mm_write_UBYTES(i->samplenumber,96,file);
			
			_mm_write_UBYTE(i->volflg,file);
			_mm_write_UBYTE(i->volpts,file);
			_mm_write_UBYTE(i->volsus,file);
			_mm_write_UBYTE(i->volbeg,file);
			_mm_write_UBYTE(i->volend,file);

			for(int u=0;u<12;u++)
			{
				_mm_write_I_SWORD(i->volenv[u].pos,file);
				_mm_write_I_SWORD(i->volenv[u].val,file);
			}

			_mm_write_UBYTE(i->panflg,file);
			_mm_write_UBYTE(i->panpts,file);
			_mm_write_UBYTE(i->pansus,file);
			_mm_write_UBYTE(i->panbeg,file);
			_mm_write_UBYTE(i->panend,file);

			for(int u=0;u<12;u++)
			{
				_mm_write_I_SWORD(i->panenv[u].pos,file);
				_mm_write_I_SWORD(i->panenv[u].val,file);
			}

			_mm_write_UBYTE(i->vibtype,file);	
			_mm_write_UBYTE(i->vibsweep,file);	
			_mm_write_UBYTE(i->vibdepth,file);	
			_mm_write_UBYTE(i->vibrate,file);	

			_mm_write_I_UWORD(i->volfade,file);
			
			if(i->insname)len=strlen(i->insname);else len=0;
			_mm_write_I_UWORD(len,file);
			if(len)fwrite(i->insname,len,1,file);

			for(int u=0;u<i->numsmp;u++)
			{
				SAMPLE *s=&i->samples[u];

				_mm_write_I_UWORD(s->c2spd,file);
				_mm_write_SBYTE(s->transpose,file);
				_mm_write_UBYTE(s->volume,file);
				_mm_write_UBYTE(s->panning,file);
				_mm_write_I_ULONG(s->length,file);
				_mm_write_I_ULONG(s->loopstart,file);
				_mm_write_I_ULONG(s->loopend,file);	
				
				// Only interesting for importing, so clear them
				s->flags&=~SF_BIG_ENDIAN;
				s->flags&=~SF_DELTA;
				s->flags|=SF_SIGNED;
				
				_mm_write_I_UWORD(s->flags,file);

				if(s->samplename)len=strlen(s->samplename); else len=0;
				_mm_write_I_UWORD(len,file);
				if(len)fwrite(s->samplename,len,1,file);
			}
		}
				
		_mm_write_I_UWORDS(m->pattrows,m->numpat,file);
		_mm_write_I_UWORDS(m->patterns,m->numpat*m->numchn,file);
		
		for(int t=0;t<m->numtrk;t++)
		{
			UBYTE *trk=m->tracks[t];
			
			len=TrkLen(m->tracks[t]);
			_mm_write_I_UWORD(len,file);
			_mm_write_UBYTES(trk,len,file);
		}		
		
		for(int t=0;t<of.numins;t++)
		{
			INSTRUMENT	*i=&m->instruments[t];	
			for(int u=0;u<i->numsmp;u++)
			{
				SAMPLE *s=&i->samples[u];	
				if(s->length>0)
				{
					if(s->flags&SF_16BITS)
					{
						_mm_write_I_UWORDS((UWORD *)Samples[s->handle],s->length,file);
					}
					else
					{
						_mm_write_UBYTES((UBYTE *)Samples[s->handle],s->length,file);
					}
				}
			}
		}		
		fclose(file);
	}
	else
	{
		BAlert *alert=new BAlert("BeMikMod",TheLocale->strings[EX_FAILED],TheLocale->strings[EX_SUCKER]);
		alert->Go();
	}*/
}