/*
 *
 *  AM --- AmigaMail
 *  (C) 1991, 1992 by Christian Riede
 *
 *  AM is distributed in the hope that it will be useful, but WITHOUT ANY
 *  WARRANTY.  No author or distributor accepts responsibility to anyone
 *  for the consequences of using it or for whether it serves any
 *  particular purpose or works at all, unless he says so in writing.
 *  Refer to the GNU General Public License, Version 1, for full details.
 *  
 *  Everyone is granted permission to copy, modify and redistribute AM,
 *  but only under the conditions described in the GNU General Public
 *  License, Version 1.  A copy of this license is supposed to have been 
 *  given to you along with AM so you can know your rights and responsi-
 *  bilities.  It should be in a file named COPYING.  Among other things,
 *  the copyright notice and this notice must be preserved on all copies.
 *
 *  
 *
 */

#include "am.h"

UBYTE *vers = (UBYTE *)("\0$VER: makecontents:"AMIGAMAIL_VERSION);

struct List Mailbox;

char Username[MAIL_FIELDLENGTH]="";

char *MyFolder;

int main(int argc, char *argv[])
{
	struct Mail *Mail;
	ULONG num;
	BPTR lock;
	struct FileInfoBlock *FileInfoBlock;

	if (argc) /* startet from CLI ? */
	{
		/* print copyright notice */
		printf("makecontents (%s - AmigaMail)\n",AMIGAMAIL_VERSION);
		puts("© 1991, 1992 by Christian Riede");
		puts("Use and distribute under the terms of the");
		puts("GNU General Public Licence, Version 1!");
	}
	else
		exit(5); /* no workbench start allowed */

	/* hello server, are you there?? */
	if (!(ServerPort = FindPort(SERVERNAME)))
	{
		puts("Server not running");
		exit(21);
	}

	/* check version of server */
	if (!CmpVersion())
	{
		puts("Wrong version of server");
		exit(22);
	}

	/* create NIL: filehandles for System() */
	if (!(AMSystemTags[0].ti_Data = (ULONG)
		Open((UBYTE *)"NIL:",MODE_OLDFILE)))
		exit(5);
	if (!(AMSystemTags[1].ti_Data = (ULONG)
		Open((UBYTE *)"NIL:",MODE_NEWFILE)))
		exit(5);

	if (argc!=2)
	{
		puts("Usage: makecontents  FOLDER");
		exit(5);
	}

	ReadConfig(0);

	/* init the mailbox-list */
	Mailbox.lh_Head = (struct Node *)&Mailbox.lh_Tail;
	Mailbox.lh_TailPred = (struct Node *)&Mailbox.lh_Head;
	Mailbox.lh_Tail = 0;
	Mailbox.lh_Type = AMNT_MAILBOX;

	/* Lock contents */
	MyFolder = argv[1];
	LockContents(MyFolder);
	
	GetContents(&Mailbox,MyFolder);

	/* delete all items from list, but not physically on disk */
	while (Mail=(struct Mail *)RemHead(&Mailbox))
	{
		FreeMail(Mail); /* delete in our list */
		DeleteItem(Mail->Number,MyFolder); /* tell server to delete it in his list */
	}
	
	/* add all items */

	/* Lock Directory */
	if (!(lock = Lock((UBYTE *)MyFolder,ACCESS_READ)))
	{
		puts("Can't Lock Directory");
		UnLockContents(MyFolder);
		exit(5);
	}

	/* allocate FileInfoBlock */
	if (!(FileInfoBlock = AllocMem(sizeof(struct FileInfoBlock),MEMF_CLEAR)))
	{
		puts("Can't get FileInfoBlock, out of Memory");
		UnLockContents(MyFolder);
		UnLock(lock);
		exit(5);
	}


	/* Examine Directory */
	if (!Examine(lock,FileInfoBlock))
	{
		puts("Can't examine Directory");
		UnLockContents(MyFolder);
		UnLock(lock);
		exit(5);
	}

	/* scan directory */
	while (ExNext(lock,FileInfoBlock))
		if (sscanf(FileInfoBlock->fib_FileName,"%d",&num)==1 && 
			FileInfoBlock->fib_DirEntryType <0 ) /* is mailfile? */
		{
			/* protect file (older version of am didn't) */
			SetProtection((UBYTE *)FileInfoBlock->fib_FileName,7L);

			/* tell server to parse the header and add it to his list */
			if (Mail = AddItem(num,MyFolder)) /* add it to list */
			{
				/* makecontents marks all mails as old/read */
				MarkRead(num,MyFolder);
				MarkOld(num,MyFolder);
				FreeMail(Mail);
			}
		}

	/* unlock directory */
	UnLock(lock);

	/* free FileInfoBlock */
	FreeMem(FileInfoBlock,sizeof(struct FileInfoBlock));
	
	/* server saves correct list */
	UnLockContents(MyFolder);

	exit(0);
}
