#include "ctdl.h"
#pragma hdrstop

#include "auxtab.h"
#include "pathdat.h"
#include "msg.h"

void CreatePathDat(void)
	{
	Message *Msg = new Message;

	if (Msg)
		{
		FILE *fl;
		const m_slot size_table = sizetable();

		mPrintfCR(getsysmsg(266));

		char fullPathToPathDat[128];
		sprintf(fullPathToPathDat, sbs, cfg.homepath, getmsg(1557));

		if ((fl = fopen(fullPathToPathDat, FO_W)) != NULL)
			{
			for (m_slot mcnt = 0; mcnt <= size_table; mcnt++)
				{
				long loc = getLocation(mcnt);
				int i, j;
				label lastlocID;

				bufferedSeek(msgfl, loc);
				Msg->ReadHeader(RMC_NORMAL, NULL);

				if (*Msg->GetFromPath())
					{
					fprintf(fl, pcts, Msg->GetFromPath());

					// Get last locID, this stuff probably not necessary
					for (i = strlen(Msg->GetFromPath());
							i && Msg->GetFromPath()[i] != '.'; i--);

					for (j = 0, i++;
							Msg->GetFromPath()[i] &&
							Msg->GetFromPath()[i] != '!' && j < 4;
							i++, j++)
						{
						lastlocID[j] = Msg->GetFromPath()[i];
						}
					lastlocID[j] = '\0';

					/* if last locID was same as current locID just use alias */
					if (SameString(lastlocID, cfg.locID))
						{
						fprintf(fl, getmsg(1556), cfg.alias);
						}
					else
						{
						fprintf(fl, getmsg(1556), cfg.Address);
						}

					fprintf(fl, bn);
					}
				}

			fclose(fl);
			}
		else
			{
			mPrintf(getmsg(78), fullPathToPathDat);
			}

		delete Msg;
		}
	else
		{
		mPrintf(getmsg(188), getsysmsg(8));
		}
	}
