/**********************************************************************
 * Bruce Lewis
 * MIT Project Athena
 * Created:  8/1/87
 *
 * $Source: /site/u1/exchange/teacher/RCS/fnhand.c,v $
 * $Author: brlewis $
 * $Header: fnhand.c,v 1.10 88/11/15 15:57:18 brlewis Exp $
 *
 * Copyright 1987 by the Massachusetts Institute of Technology.
 * For copying and distribution information, see the file
 * "mit-copyright.h".
 *
 * fnhand.c module -- functions of the handout menu
 **********************************************************************/

#ifndef lint
static char rcsid_fnhand_c[] = "$Header: fnhand.c,v 1.10 88/11/15 15:57:18 brlewis Exp $";
#endif /* lint */

#include"teacher.h"

/**********************************************************************
 * hlist(argc, argv)
 *   + lists handouts by class and/or topic
 **********************************************************************/

hlist(argc, argv)

     int argc;
     char *argv[];
{
  show_list(argc, argv, HANDOUTS);
}

/**********************************************************************
 * hshow(argc, argv)
 *   + copy handout files to /tmp
 *   + start editor
 **********************************************************************/

hshow(argc, argv)

     int argc;
     char *argv[];
{
  teacher_display(argc, argv, HANDOUTS);
}

/**********************************************************************
 * hgetnote(argc, argv)
 *   + displays note for a handout
 **********************************************************************/

hgetnote(argc, argv)

     int argc;
     char *argv[];
{
  LIST *list;
  char *newtopic;
  int i;

  Debug((stderr, ">>>> Enter hgetnote\n"));
  newline(1);

  if ((list = teacher_list(argc, argv, HANDOUTS)) == NULL)
    return;

  for(i=0; i<how_many_files(list); i++) {

    if (is_note(file_in(list,i))) break;
    gshow(stdout, file_in(list,i));

    /* make handid into a note id */
    newtopic = NewArray(char,
			strlen(f_topic(file_in(list, i))) + NOTELEN + 1);
    (void) strcpy(newtopic, NOTESTR);
    (void) strcat(newtopic, f_topic(file_in(list, i)));
    free(f_topic(file_in(list, i)));  /* hair for plugging memory leak */
    f_topic(file_in(list, i)) = newtopic;

    if (Fx_retrieve(file_in(list, i), "/dev/tty") != SUCCEEDED) {
#ifdef DEBUG
      Fx_error("Fx_retrieve");
#endif DEBUG
      printf("No note for %s handout.\n", newtopic + NOTELEN);
    }
  }

  newline(1);
  Fx_destroy_list(&list);
  Debug((stderr, "<<<< Exit hgetnote\n"));
  return;
}

/**********************************************************************
 * hput(argc, argv)
 *   + copies local file to handout bin
 **********************************************************************/

hput(argc, argv)

     int argc;
     char *argv[];
{
  teacher_put(argc, argv, HANDOUTS);
}

/**********************************************************************
 * hnote(argc, argv)
 *   + adds note to a handout
 **********************************************************************/

hnote(argc, argv)

     int argc;
     char *argv[];
{
  LIST *tlist;
  FILE_ID *id;
  int i;

  Debug((stderr, ">>>> Enter hnote\n"));
  newline(1);

  if ((tlist = teacher_list(argc, argv, HANDOUTS)) == NULL)
    return;

  for (i=0; i<how_many_files(tlist); i++) {
    id = file_in(tlist, i);
    if (is_note(id)) continue;
    gshow(stdout, id);
    make_note(id);
    printf("Type a brief description and press RETURN, Ctrl-D.\n");
    if (Fx_send("/dev/tty", id) != SUCCEEDED)
      Fx_error("Could not add note");
  }

  newline(1);
  Fx_destroy_list(&tlist);
  Debug((stderr, "<<<< Exit hnote\n"));
  return;
}

/**********************************************************************
 * htake(argc, argv)
 *   + copies a handout to a file
 **********************************************************************/

htake(argc, argv)

     int argc;
     char *argv[];
{
  LIST *tlist;
  char destination[GENLEN];
  int i;

  Debug((stderr, ">>>> Enter htake\n"));
  newline(1);

  if ((tlist = teacher_list(argc, argv, HANDOUTS)) == NULL) return;

  for (i=0; i<how_many_files(tlist); i++) {
    gshow(stdout, file_in(tlist, i));
    promptfor("Local filename", destination);
    if (strlen(destination) == 0)
      (void) strcpy(destination, f_topic(file_in(tlist, i)));

    if (Fx_retrieve(file_in(tlist, i), destination) != SUCCEEDED)
      Fx_error("Can't retrieve handout");
    else printf("Taken.\n");
  }

  newline(1);
  Fx_destroy_list(&tlist);
  Debug((stderr, "<<<< Exit htake\n"));
  return;
}
/**********************************************************************
 * hpurge(argc, argv)
 *   + removes handouts
 **********************************************************************/

hpurge(argc, argv)

     int argc;
     char *argv[];
{
  LIST *tlist;
  FILE_ID *id;
  int i;

  Debug((stderr, ">>>> Enter hpurge\n"));
  newline(1);

  if ((tlist = teacher_list(argc, argv, HANDOUTS)) == NULL) return;

  for (i=0; i<how_many_files(tlist); i++) {
    id = file_in(tlist, i);
    if (is_note(id)) continue;
    gshow(stdout, id);
    if (Fx_delete(id) != SUCCEEDED)
      Fx_error("Can't purge handout");
    else printf("Purged.\n");

    /* delete note; ignore error */
    make_note(id);
    (void) Fx_delete(id);
  }

  newline(1);
  Fx_destroy_list(&tlist);
  Debug((stderr, "<<<< Exit hpurge\n"));
  return;
}
