/*
	Module containing low level I/O routines for BDM.
*/

#include <stdio.h>
#include "bdm.h"

#ifdef VMS
#define PIF_LOCATION	"UW$CEA:"
#else
#define PIF_LOCATION	"/sy99/dat/"
#endif VMS

#define PIF_NAME	"pif.dat"
#define PIF_SCRATCH	"pif.scr"



FILE *fp1,*fp2;                    /* PIF */
char f1[100],f2[100];
extern struct pif_record record;
extern struct plist_type plist;
extern int pif_eof;

/*    List of Routines   */
void open_pif();
void close_pif();
int  read_pif();
void put_pif();
void delete_scr();
void rename_pif();


/*********** Start of Routines ***********************************************/
/*
	Open_pif - Routine to open the PIF for input and create a scratch
	pif.
*/
void open_pif() {
  /* Open the PIF for reading. (Use write mode to lock the file.) */
  sprintf(f1,"%s%s",PIF_LOCATION,PIF_NAME);
  if(!(fp1 = fopen(f1,"r+","rfm=var", "rat=cr"))) {
    perror(f1);
    exit(1);
  }

  pif_eof = FALSE;	/* Set flag indicating that we are not at the eof */

  /* Open a scratch version of the PIF. */
  sprintf(f2,"%s%s",PIF_LOCATION,PIF_SCRATCH);
  if(!(fp2 = fopen(f2,"w"))) {
    perror(f2);
    exit(1);
  }

  /* Initialize the record */
  record.rtype = ' ';

  return;
}



/*
	Close_pif - Routine to close the PIF.
*/
void close_pif()
{
  if(fp1) fclose(fp1);
  if(fp2) fclose(fp2);
  return;
}

/*
	READ_pif - Routine to read a record from the PIF. If the input flag
	is set to true then the record is copied to the scratch pif.

	Return - TRUE is success
		 FALSE if failure.
*/
int read_pif(string,write_flag)
  char *string;
  int write_flag;
{
  int len;
  char *cptr;

  /*  First clear out the record */
  record.pr_union.acc.accountname[0]   = '\0';
  record.pr_union.acc.access = '\0';
  record.pr_union.part.part_name[0] = '\0';
  record.pr_union.part.part_desc[0] = '\0';
  record.pr_union.loc.location[0] = '\0';
  record.pr_union.loc.vol_name[0] = '\0';
  record.pr_union.id.ident[0] = '\0';
  record.pr_union.id.idtype = '\0';

  /* Get the record into a buffer and write it to the temporary file */
  if(fgets(string,BUFSIZ,fp1)) {
    if(write_flag) fputs(string,fp2);
    /* Strip off any LF */
    len = strlen(string);
    if(len > 0) len--;
    if(string[len] == '\n') string[len] = '\0';
    /* Now copy it to the structure */
    record.rtype = string[0];
    if(len > 2) strcpy(&record.pr_union,&string[2]);
    return(TRUE);
  }
  pif_eof = TRUE;	/* Set flag indicating at eof of pif */
  return(FALSE);

}

/*
	Put_pif - Routine to write a string to the temporary PIF file.
*/
void put_pif(string)
  char *string;
{
  int  len;

  len = strlen(string);
  if(string[len-1] != '\n') {
    string[len] = '\n';
    if(len < BUFSIZ) string[len+1] = '\0';
  }
  fputs(string,fp2);
  return;
}

/*
	Delete_scr - Routine to delete the scratch pif file.
*/
void delete_scr()
{
  if(fp2) fclose(fp2);		/* Make sure the file is closed. */

  sprintf(f2,"%s%s",PIF_LOCATION,PIF_SCRATCH); /* Point to file. */
#ifdef VMS
  remove(f2);			/* Remove the file ignoring errors */
#else
  unlink(f2);			/* Unlink the file */
#endif VMS

  return;
}

/*
	Rename_pif - Routine to rename the scratch file to the PIF file.
*/
void rename_pif()
{
#ifdef VMS
  sprintf(f1,"%s%s;-1",PIF_LOCATION,PIF_NAME);
  sprintf(f2,"%s%s;",PIF_LOCATION,PIF_SCRATCH);
#else
  sprintf(f1,"%s%s",PIF_LOCATION,PIF_NAME);
  sprintf(f2,"%s%s",PIF_LOCATION,PIF_SCRATCH);
#endif VMS

  if(rename(f2,f1) != 0) {
    perror(f2);
    exit(1);
  }
}
