// ==========================================================================
// Author:  Yee Hsu
// Date:    8/5/2003
//
// Desc:    Print BOL (Bill of Lading) onto packing slip of all orders
//          given input files.
// ==========================================================================

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

FILE* tryopen(FILE* fp, const char* fname)
{
    char buf[2];
    char filename[32];

    if (fp == NULL)
    {
        sprintf(filename, "%s.txt", fname);
        if ((fp = fopen(filename, "r")) == NULL)
        {
            fprintf(stderr, "\nError openining file: %s\n", fname);
            fprintf(stderr, "\nHit [Enter] to exit: ");
            fread(buf, sizeof(buf), 1, stdin);
            exit(1);
        }
    }
    return fp;
}

int getbol(int pt, char* date, char* fname)
{
    FILE* fp;
    char buf[128];
    static char datebf[12];
    int blpt = pt;

    fp = tryopen(fopen(fname, "r"), fname);
    while (1)
    {
        fgets(buf, sizeof(buf), fp);
        if (feof(fp)) break;
        if (atoi(buf) == pt) break;
        if (strlen(buf) == 91 && buf[0] == ' ')
        {
            blpt = atoi(buf);
            buf[58] = '\0';
            sprintf(datebf, "%s", &buf[50]);
            sprintf(date, "%s", datebf);
        }
    }
    fclose(fp);
    return blpt;
}

void header()
{
    printf("Application for Shipping Department.\n");
    printf("Creates a 'manifest.txt' and 'pickticket.txt' file\n");
    printf("from a bill of lading report and pick ticket reports.\n");
    printf("Version 2.1 Yee Hsu. 2003.\n\n");
}

int main(int argc, char* argv[])
{
    FILE *pt, *bl, *fp, *pk;
    int ptic, otic, ntic, i, boln, pteof, pcn;
    char *pstore, *po, *pdept, *pwgt, *pctn, *pc;
    char store[16], dept[8], wgt[8], ctn[4];
    char ptname[32], blname[32], date[12];
    char ptbf[1024], blbf[1024], buf[1024];
    const char mfname[] = "manifest.txt";
    const char pkname[] = "pickticket.txt";

    header();
    fprintf(stdout, "Enter a period (.) to exit.\n\n");
    fprintf(stdout, "Enter BL filename: ");
    fscanf(stdin, "%s", blname);
    bl = tryopen(fopen(blname, "r"), blname);
    fp = tryopen(fopen(mfname, "w"), mfname);
    pk = tryopen(fopen(pkname, "w"), pkname);

    while (1)
    {
        pteof = 0;
        fprintf(stdout, "Enter PT filename: ");
        fscanf(stdin, "%s", ptname);
        if (strcmp(ptname, ".") == 0) break;
        pt = tryopen(fopen(ptname, "r"), ptname);
        fseek(pk, -3L, SEEK_END);

        while (1)
        {
            for (i = 0; i < 3; i++)
            {
                fgets(ptbf, sizeof(ptbf), pt);
                if (feof(pt)) {pteof = 1; break;}
                fputs(ptbf, pk);
            }
            if (pteof == 1) break;
            ptic = atoi(ptbf);

            for (i = 0; i < 6; i++)
            {
                fgets(ptbf, sizeof(ptbf), pt);
                fputs(ptbf, pk);
            }
            pstore = strtok(ptbf, " ");
            pstore = strtok(NULL, " ");
            sprintf(store, "%s", pstore);

            fgets(ptbf, sizeof(ptbf), pt);
            fputs(ptbf, pk);
            fgets(ptbf, sizeof(ptbf), pt);
            fputs(ptbf, pk);
            po = &ptbf[68];
            ptbf[80] = '\0';
            pdept = strtok(&ptbf[81], " ");
            sprintf(dept, "%s", pdept);

            fseek(bl, 0L, SEEK_SET);
            while (!feof(bl))
            {
                fgets(blbf, sizeof(ptbf), bl);
                ntic = atoi(blbf);

                if (ntic == ptic && ntic != otic)
                {
                    boln = getbol(ntic, date, blname);
                    otic = ntic;
                    pwgt = strtok(&blbf[28], " ");
                    sprintf(wgt, "%s", pwgt);
                    pctn = strtok(NULL, " ");
                    pctn = strtok(NULL, " ");
                    sprintf(ctn, "%s", pctn);
                    sprintf(buf, "%d    %s    %s    %d    %s    %6s    %s", boln, po, dept, ntic, store, wgt, ctn);
                    fprintf(fp, "%s", buf);    
                    break;
                }
            }
            for (i = 0; i < 39; i++)
            {
                fgets(ptbf, sizeof(ptbf), pt);
                fputs(ptbf, pk);
            }
            fgets(ptbf, sizeof(ptbf), pt);
            pc = strtok(ptbf, " ");
            pc = strtok(NULL, " ");
            if (pc != NULL)
            {
                pcn = atoi(pc);
                fprintf(pk, "            %3d CTNs %6s lbs.                      %3d               %8s  UNITS: %5d  UNITS: %5d\n", atoi(ctn), wgt, boln, date, pcn, pcn);
            }
            else
                fprintf(pk, "\n");
        }
        fclose(pt);
    }
    fclose(bl);
    fclose(fp);
    fclose(pk);
    return 0;
}