/* PREFER.C

   Revised to return DOS errorlevels...
   Revised to set local clock to preferred server's time...
   Compiled with TURBO C.

   +-------------------------------------------------------------------+
   | PREFER is a simple program that can be copied to the              |
   | workstation's network driver subdirectory and included in the     |
   | AUTOEXEC.BAT file immediately after NET3 is executed. PREFER      |
   | breaks ALL connections with the server that NET3 attaches to and  |
   | establishes new connections with a user specified server. From    |
   | that point on the login directory, login utilities, and all       |
   | dependent connections are from the desired server.                |
   |                                                                   |
   | Exception:                                                        |
   |                                                                   |
   | If it is the user's intention to ATTACH to other servers after    |
   | logging in, dependent connections will be established to that     |
   | server until it is logged out.                                    |
   |                                                                   |
   | Operation:                                                        |
   |                                                                   |
   | PREFER is simply used by entering the command "PREFER servername" |
   | at the prompt or within a batch file, where "servername" is the   |
   | user's desired server.                                            |
   |                                                                   |
   | If PREFER is invoked without a server name it will do nothing and |
   | return to the DOS prompt.                                         |
   |                                                                   |
   | If PREFER is invoked with a server name where the dependent       |
   | connections are already established, it will indicate such and    |
   | return to the DOS prompt.                                         |
   |                                                                   |
   | If PREFER is invoked after the user has logged in, and the new    |
   | server name is different from the old server name, the user will  |
   | be logged out of the old server when the new connections are      |
   | established.                                                      |
   |                                                                   |
   | The prefer program file is PREFER.EXE. It can be invoked at the   |
   | command line or in a batch file, but cannot be invoked from a     |
   | system or user login script.                                      |
   |                                                                   |
   |                       AAC Associates                              |
   |                      (703) 415 - 4400                             |
   |-------------------------------------------------------------------|
   | NEW RELEASE "PREFER II" now includes fixes for multiple           |
   | erroneous attachments, and conditions causing the primary server  |
   | connection ID to be "0".                                          |
   |                                                                   |
   | If you run certain older LOGIN revs. and don't do a complete      |
   | login, the program will leave you attached to the server at       |
   | that connection ID. If you do this 8 times to 8 different servers |
   | you will fill your 8 connection ID slots and not be able to       |
   | to another server to login. The older version PREFER would not    |
   | take into account these multiple attachments in the connection    |
   | ID table and would leave them there. PREFER II clears any and     |
   | all additional attachments.                                       |
   |                                                                   |
   | If you login to a file server and make a mistake while logging    |
   | in, the program will set the Primary Connection ID to "0". This   |
   | created an error condition in the earlier version of PREFER that  |
   | would cause the workstation shell to loose all connections and    |
   | error out. PREFER II corrects for this.                           |
   |								       |
   | PREFER II by Dan Smith     9-5-90                                 |
   +-------------------------------------------------------------------+

*/

#include <stdio.h>
#include <dos.h>
#include <string.h>
#include <nit.h>
#include <niterror.h>

main(argc,argv)
int argc;
char *argv[];
{
void ttime(void);
void primary(void);
char serverName[48];
WORD 	connectID1,
	connectID2,
	DefaultID,
	PrimaryID;
int netReturn,localDrives;
char fileServerName[48];
char newDirHandle, mask;
char loginPath[]="SYS:LOGIN";
char letter,chr='A';

printf ("\nPREFER 2.1 By AAC Associates, Inc.\n");

if(argc<2)
    {
    printf("\nSyntax: PREFER servername\n\n");
    printf("Errorlevels returned: \n\n");
    printf("  0 - Success. Now attached to target server as the primary server.\n");
    printf("  1 - Already attached to target server as the primary server.\n");
    printf("  2 - Error attaching to the target server.\n");
    printf("  3 - Unknown file server was requested.\n");
    printf("  4 - No target server named specified on command line.\n\n");
    exit(4);
    }

  if (strlen(argv[1]) > 47)         /* hangs machine if this is the case */
    argv[1][47] = '\0';


strcpy(serverName,argv[1]);
strupr(serverName);


PrimaryID=GetPrimaryConnectionID();
DefaultID=GetDefaultConnectionID();
if(PrimaryID == 0)
		{
		SetPrimaryConnectionID(DefaultID);
		PrimaryID = DefaultID;
		}
GetFileServerName(PrimaryID,fileServerName);
printf("\nPrimary server is %s.\n",fileServerName);

netReturn=AttachToFileServer(&serverName, &connectID1);

switch (netReturn) {
    case 0   : break;
    case 248 : if (PrimaryID == connectID1)
			{
			printf("Already attached to server.\n");
			primary();
			printf("All other connections cleared.\n");
			exit(1);
			}
		break;
    case 249 : printf("No free connection slots.\n");
               exit(2);
               break;
    case 250 : printf("No more server slots.\n");
               exit(2);
               break;
    case 252 : printf("Unknown file server requested: %s.\n",serverName);
               exit(3);
               break;
    case 254 : printf("Server bindery locked.\n");
               exit(2);
               break;
    case 255 : printf("No response from server.\n");
               exit(2);
               break;
    default  : printf("Unknown error returned.\n",netReturn);
               exit(2);
               break;
    }

	primary();

	AttachToFileServer(&serverName, &connectID1);
	SetPrimaryConnectionID(connectID1);
	DetachFromFileServer(PrimaryID);

	ttime();

	localDrives=GetNumberOfLocalDrives();
        letter=(char)(chr+localDrives);

	netReturn=AllocPermanentDirectoryHandle(0x00,&loginPath,letter,newDirHandle,mask);

	printf("New primary server is %s.\n",serverName);
	printf("All other connections cleared.\n");
	exit(0);

}

void	ttime(void)

{
char dateTime[7];
union	REGS regs;

	GetFileServerDateAndTime(&dateTime);
	if (dateTime[0] < 80) regs.x.cx = dateTime[0] + 2000;
		else	regs.x.cx = dateTime[0] +1900;
		regs.h.dh = dateTime[1];
		regs.h.dl = dateTime[2];
		regs.h.ah = 0x2b;
		int86(0x21, &regs, &regs);
		regs.h.ch = dateTime[3];
		regs.h.cl = dateTime[4];
		regs.h.dh = dateTime[5];
		regs.h.ah = 0x2d;
		int86(0x21, &regs, &regs);
}

void primary(void)
{

WORD PrimaryID,ID;
int IDtest;

PrimaryID=GetPrimaryConnectionID();

/*______________________________________________*/
/*    Loop to test all eight connection ID's    */
/*----------------------------------------------*/

for(ID=1;ID<=8;++ID)
	{

	IDtest=IsConnectionIDInUse(ID);

	if(IDtest)
		{
			if(ID != PrimaryID)
				{
				DetachFromFileServer(ID);
				}
		}

	}

}