#include <stdio.h>
#include "mod10-1_lib.h"

int main (int argc, char *argv[])
{

        FILE *infile1, *infile2, *infile3;
        char studentlist[MAX], removelist[MAX], displaylist[MAX];
        int record, found;
        char first[MAX], last[MAX], gender[MAX];
        Structure *temp, *Head = NULL, *P, *C;
        int first_time = 1;

        /* check if files were entered in the command line */
        if (argc < 4) {
                printf("usage: fopen <infile>\n");
                printf("You didn't enter all of the file names.\n");
                printf("Enter the file name with the list of students: ");
                scanf("%s", studentlist);
                printf("Enter the file name with the ID #'s of who should be removed: ");
                scanf("%s", removelist);
                printf("Enter the file name with the ID #'s for people being displayed: ");
                scanf("%s", displaylist);
        } else {
                strcpy(studentlist, argv[1]);
                strcpy(removelist, argv[2]);
                strcpy(displaylist, argv[3]);
        } /* end of if..else */
       
        /* open studentlist file */
        if ((infile1 = fopen(studentlist, "r")) == NULL ) {
                printf("Error opening file %s\n", studentlist);
                exit(1);
        }
       
        /* create the linked list while there are names to be added */
        temp = (Structure *)malloc(sizeof(Structure));
        temp->next = NULL;
        Head = temp;
        while (fscanf(infile1, "%d %s %s %s", &record, first, last, gender) != EOF) {
                if (first_time == 1)
                        first_time = 0;
                else {
                        C = add_node(Head);
                        temp = C;
                }
                temp->ID = record;
                strcpy(temp->firstname, first);
                strcpy(temp->lastname, last);
                strcpy(temp->studentgender, gender);
        } /* end 0f while */
       
        /* open removelist file */
        if ((infile2 = fopen(removelist, "r")) == NULL ) {
                printf("Error opening file %s\n", removelist);
                exit(1);
        }

        /* read id number, find id number, remove node w/ id number */
        while (fscanf(infile2, "%d", &record) != EOF) {
                found = find_id(Head, record, &P, &C);
                if (found == 1)
                        delete_node(&Head, P, C);
                else
                        printf("%d wasn't found for removal.\n", record);
        }
        printf("Removal process was succesful. \n");
       
        /* open displaylist file */
        if ((infile3 = fopen(displaylist, "r")) == NULL ) {
                printf("Error opening file %s\n", displaylist);
                exit(1);
        }

        /* display names of people who ID is found in file */
        while (fscanf(infile3, "%d", &record) != EOF) {
                found = find_id(Head, record, &P, &C);
                if (found == 1)
                        display_name(C);
                else
                        printf("%d wasn't found for display.\n", record);
        }
       
        /* free all exsisting nodes */
        delete_list(&Head);
        printf("The list has been cleared succesfully \n");
       
        /* close file w/ list of students */
        fclose(infile1);

        /* close file w/ list of remove id's */
        fclose(infile2);
       
        /* close file w/ list of ID of students displayed */
        fclose(infile3);

        exit(1);
       
}

