/* $Id: student.pc,v 1.6 2009-06-17 15:43:06 zacheiss Exp $
 *
 * Load data into Moira from Registrar's Office data file
 *
 * Copyright (C) 1990-1998 by the Massachusetts Institute of Technology
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 */

#include <mit-copyright.h>
#include <moira.h>
#include <moira_site.h>
#include <moira_schema.h>
#include "common.h"

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

EXEC SQL INCLUDE sqlca;

RCSID("$Header: /afs/athena.mit.edu/astaff/project/moiradev/repository/moira/regtape/student.pc,v 1.6 2009-06-17 15:43:06 zacheiss Exp $");

/* File format is:

0-8	MIT ID
9-38	last name
39-68	first name
69-98	middle name
99	year
100-103	department
104-133	address
134-173	city
174-175	state
176-184	zip code
185-204	phone
205-234	office address
235-254	office phone

*/

#define LOC_ID 0
#define LEN_ID 9
#define LOC_LAST (LOC_ID + LEN_ID)
#define LEN_LAST 30
#define LOC_FIRST (LOC_LAST + LEN_LAST)
#define LEN_FIRST 30
#define LOC_MIDDLE (LOC_FIRST + LEN_FIRST)
#define LEN_MIDDLE 30
#define LOC_YEAR (LOC_MIDDLE + LEN_MIDDLE)
#define LEN_YEAR 1
#define LOC_COURSE (LOC_YEAR + LEN_YEAR)
#define LEN_COURSE 4
#define LOC_ADDRESS (LOC_COURSE + LEN_COURSE)
#define LEN_ADDRESS 30
#define LOC_CITY (LOC_ADDRESS + LEN_ADDRESS)
#define LEN_CITY 40
#define LOC_STATE (LOC_CITY + LEN_CITY)
#define LEN_STATE 2
#define LOC_ZIP (LOC_STATE + LEN_STATE)
#define LEN_ZIP 9
#define LOC_PHONE (LOC_ZIP + LEN_ZIP)
#define LEN_PHONE 20
#define LOC_OADDR (LOC_PHONE + LEN_PHONE)
#define LEN_OADDR 30
#define LOC_OPHONE (LOC_OADDR + LEN_OADDR)
#define LEN_OPHONE 20

EXEC SQL BEGIN DECLARE SECTION;
int who;
char *prog = "stuload";
EXEC SQL END DECLARE SECTION;

char *whoami;

struct entry *get_next_entry(FILE *in);
void process_entry(struct entry *e, int secure);

int main(int argc, char **argv)
{
  FILE *in;
  struct entry *e;
  int i, wait = 0;
  char buf[80], *file = NULL;
  EXEC SQL BEGIN DECLARE SECTION;
  char *db = "moira";
  EXEC SQL END DECLARE SECTION;

  whoami = strrchr(argv[0], '/');
  if (whoami)
    whoami++;
  else
    whoami = argv[0];

  setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
  setvbuf(stderr, NULL, _IOLBF, BUFSIZ);

  for (i = 1; i < argc; i++)
    {
      if (!strcmp(argv[i], "-w"))
	wait++;
      if (file)
	{
	  fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami);
	  exit(1);
	}
      else
	file = argv[i];
    }

  if (!file)
    {
      fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami);
      exit(1);
    }

  in = fopen(file, "r");
  if (!in)
    {
      fprintf(stderr, "Unable to open %s for input\n", file);
      exit(1);
    }

  initialize_sms_error_table();

  EXEC SQL CONNECT :db IDENTIFIED BY :db;
  if (sqlca.sqlcode)
    {
      dbmserr("connecting", sqlca.sqlcode);
      exit(1);
    }

  EXEC SQL SELECT users_id INTO :who FROM users WHERE login = 'root';

  while ((e = get_next_entry(in)))
    {
      process_entry(e, 1);
      EXEC SQL COMMIT WORK;
      if (sqlca.sqlcode)
	{
	  dbmserr("committing work", sqlca.sqlcode);
	  exit(1);
	}
      if (wait)
	{
	  printf("Next");
	  fflush(stdout);
	  fgets(buf, sizeof(buf), stdin);
	}
    }

  exit(0);
}


struct entry *get_next_entry(FILE *in)
{
  static struct entry e;
  static char buf[BUFSIZ], classbuf[10];
  static char namebuf[LEN_FIRST + LEN_MIDDLE + LEN_LAST + 2];
  static char addrbuf[USERS_HOME_ADDR_SIZE], xaddrbuf[USERS_XADDRESS_SIZE];
  static char first[LEN_FIRST + 1], last[LEN_LAST + 1], middle[LEN_MIDDLE + 1];
  static char id[LEN_ID + 1], course[LEN_COURSE + 1];
  static char year[LEN_YEAR + 1], address[LEN_ADDRESS + 1];
  static char city[LEN_CITY + 1];
  static char state[LEN_STATE + 1], phone[LEN_PHONE + 1];
  static char ophone[LEN_OPHONE + 1], title[128];
  static char zip[LEN_ZIP + 1], oaddr[LEN_OADDR + 1];
  static int nyear = 0;
  int ends_jr, ends_iii, ends_iv, ends_sr, ends_ii, ends_v;
  char *p;

  if (nyear == 0)
    {
      struct tm *tm;
      struct timeval tv;

      gettimeofday(&tv, NULL);
      tm = localtime(&tv.tv_sec);
      nyear = tm->tm_year;
      if (tm->tm_mon >= 5)
	nyear++;
    }

  if (!fgets(buf, sizeof(buf), in))
    return NULL;

  strlcpy(id, &buf[LOC_ID], LEN_ID + 1);
  strtrim(id);
  strlcpy(last, &buf[LOC_LAST], LEN_LAST + 1);
  strtrim(last);
  strlcpy(first, &buf[LOC_FIRST], LEN_FIRST + 1);
  strtrim(first);
  strlcpy(middle, &buf[LOC_MIDDLE], LEN_MIDDLE + 1);
  strtrim(middle);
  strlcpy(year, &buf[LOC_YEAR], LEN_YEAR + 1);
  strtrim(year);
  strlcpy(course, &buf[LOC_COURSE], LEN_COURSE + 1);
  strtrim(course);
  strlcpy(address, &buf[LOC_ADDRESS], LEN_ADDRESS + 1);
  strtrim(address);
  strlcpy(city, &buf[LOC_CITY], LEN_CITY + 1);
  strtrim(city);
  strlcpy(state, &buf[LOC_STATE], LEN_STATE + 1);
  strtrim(state);
  strlcpy(zip, &buf[LOC_ZIP], LEN_ZIP + 1);
  strtrim(zip);
  strlcpy(phone, &buf[LOC_PHONE], LEN_PHONE + 1);
  strtrim(phone);
  strlcpy(oaddr, &buf[LOC_OADDR], LEN_OADDR + 1);
  strtrim(oaddr);
  strlcpy(ophone, &buf[LOC_OPHONE], LEN_OPHONE + 1);
  strtrim(ophone);

  e.first = first;
  e.last = last;
  e.middle = middle;
  ends_jr = ends_iii = ends_iv = ends_sr = ends_ii = ends_v = 0;
  LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
		  &ends_ii, &ends_v);
  if (middle[1] == '.' && middle[2] == '\0')
    middle[1] = '\0';
  e.name = namebuf;
  if (*middle)
    sprintf(e.name, "%s %s %s", first, middle, last);
  else
    sprintf(e.name, "%s %s", first, last);

  e.id = id;

  e.xtitle = title;
  if (year[0] == 'G' || year[0] == 'N')
    {
      e.type = "G";
      sprintf(title, "Grad Student");
    }
  else
    {
      e.type = classbuf;
      sprintf(classbuf, "%d", nyear + 4 - atoi(year) + 1900);
      sprintf(title, "Undergrad (class of %s)", classbuf);
    }

  e.dept = course;

  e.oaddr = oaddr;
  fixaddress(e.oaddr);
  e.ophone = e.xphone2 = ophone;
  fixphone(e.ophone);

  e.haddr = addrbuf;
  strlcpy(e.haddr, address, sizeof(addrbuf));
  if (*city)
    {
      strlcat(e.haddr, " ", sizeof(addrbuf));
      strlcat(e.haddr, city, sizeof(addrbuf));
    }
  if (*state)
    {
      strlcat(e.haddr, " ", sizeof(addrbuf));
      strlcat(e.haddr, state, sizeof(addrbuf));
      strlcat(e.haddr, zip, sizeof(addrbuf));
    }
  fixaddress(e.haddr);

  e.hphone = e.xphone1 = phone;
  fixphone(e.hphone);

  e.xaddress = xaddrbuf;
  strlcpy(e.xaddress, address, sizeof(xaddrbuf));
  strlcat(e.xaddress, " |", sizeof(xaddrbuf));
  if (*city)
    {
      strlcat(e.xaddress, city, sizeof(xaddrbuf));
      if (*state)
	{
	  strlcat(e.xaddress, " ", sizeof(xaddrbuf));
	  strlcat(e.xaddress, state, sizeof(xaddrbuf));
	  strlcat(e.xaddress, zip, sizeof(xaddrbuf));
	}
    }
  else
    strlcat(e.xaddress, "MIT INTERDEPARTMENTAL MAIL", sizeof(xaddrbuf));

  return &e;
}
