/* winsetup.c
 *
 * This file is #included into unzip.c.  It contains much of the same
 * code as unzip's main().  15 Oct 92
 */


/* MS Windows Setup  and Take-Down functions bracket calls to 
 * process_zipfile().
 * These functions allocate and free the necessary buffers, set and clear
 * any global variables so that  process_zipfile()  can be called multiple
 * times in the same session of WizUnzip. You'll recognize some of the 
 * code from main() in SetUpToProcessZipFile().
 */
HANDLE hOutBuf;
HANDLE hOutOut;   /* added 04/03/92 for version 1.1 */
HANDLE hInBuf;
HANDLE hZipFN;
HANDLE hFileName;

BOOL FSetUpToProcessZipFile(int ncflag, int ntflag, int nvflag, int nUflag, 
       int nzflag, int ndflag, int noflag, int naflag, int argc,
       LPSTR lpszZipFN, PSTR *PFNAMES)
{
    /* clear all global flags -- need to or not. */

    tflag = vflag=cflag=aflag=U_flag=quietflg=zflag = 0;
    overwrite_all=overwrite_none=0;
    pfnames = &fnames[0];   /* assign default filename vector... */
    pxnames = &fnames[1];   /* ...and default excluded-filename vector */

    cflag = ncflag; overwrite_all = noflag;
    tflag = ntflag; vflag = nvflag; zflag = nzflag; U_flag = nUflag;
    aflag = naflag;
    sflag = 1;

    local_hdr_sig[0] = central_hdr_sig[0] = end_central_sig[0] = '\120';
    local_hdr_sig[1] = central_hdr_sig[1] = end_central_sig[1] = '\0';

    if (!(hZipFN = LocalAlloc(LMEM_MOVEABLE, FILNAMSIZ)))
        return FALSE;

    zipfn = (char *)LocalLock(hZipFN);
    lstrcpy(zipfn, lpszZipFN);
    if (stat(zipfn, &statbuf) || (statbuf.st_mode & S_IFMT) == S_IFDIR)
        strcat(zipfn, ZSUFX);

    if (stat(zipfn, &statbuf)) {  /* try again */
        fprintf(stderr, "error:  can't find zipfile [ %s ]\n", zipfn);
        return TRUE;              /* 9:  file not found */
    } else
        ziplen = statbuf.st_size;

    if (argc != 0) {
        char **pp = PFNAMES-1;
        char *q;

        pfnames = PFNAMES;
        /* convert MSDOS-style directory separators to Unix-style ones */
        while (*pfnames != NULL) {
            for (q = *pfnames;  *q;  ++q)
                if (*q == '\\')
                    *q = '/';
            ++pfnames;
        }
        pfnames = PFNAMES;
        while (*++pp)
            if (!strcmp(*pp, "-x")) {
                if (pp > PFNAMES)
                    **pp = 0;          /* terminate pfnames */
                else
                    pfnames = fnames;  /* defaults */
                pxnames = pp + 1;      /* excluded-names ptr starts after -x */
                break;                 /* skip rest of args */
            }
        process_all_files = FALSE;
    } else
        process_all_files = TRUE;       /* for speed */

/*---------------------------------------------------------------------------
    Okey dokey, we have everything we need to get started.  Let's roll.
  ---------------------------------------------------------------------------*/

    if (hInBuf = LocalAlloc(LMEM_MOVEABLE, INBUFSIZ+4)) {  /* 4 extra: hold[] */
        inbuf = (uch *)LocalLock(hInBuf);
        WinAssert(inbuf);
    }
    if (hOutBuf = LocalAlloc(LMEM_MOVEABLE, OUTBUFSIZ+1)) {  /* extra: ASCIIZ */
        outbuf = (uch *)LocalLock(hOutBuf);
        WinAssert(outbuf);
        if (aflag) {   /* if LF => CR,LF translation */
            if (hOutOut = GlobalAlloc(GMEM_MOVEABLE,OUTBUFSIZ)) {
                outout = (uch _far *)GlobalLock(hOutOut);
                WinAssert(outout);
            }
        } else    /* no translation; just re-use output buffer */
            outout = (uch _far *)outbuf;  /*  point to outbuf */
    }
    if ( hFileName = LocalAlloc(LMEM_MOVEABLE, FILNAMSIZ)) {
        filename = (char *)LocalLock(hFileName);
        WinAssert(filename);
    }

    if ((inbuf == NULL) || (outbuf == NULL) || (outout == NULL) ||
        (zipfn == NULL) || (filename == NULL))
        return FALSE;

    hold = &inbuf[INBUFSIZ];   /* to check for boundary-spanning signatures */

    return TRUE;    /* set up was OK */
}

void TakeDownFromProcessZipFile(void)
{
    if (inbuf) {
        LocalUnlock(hInBuf);
        inbuf = NULL;
    }
    if (hInBuf)
        hInBuf = LocalFree(hInBuf);

    if (outbuf) {
        LocalUnlock(hOutBuf);
        outbuf = NULL;
    }
    if (hOutBuf)
        hOutBuf = LocalFree(hOutBuf);

    if (aflag && outout)    /* if doing LF => CR,LF translation */
        GlobalUnlock(hOutOut);
    outout = NULL;          /* free now, translation or not     */
    if (hOutOut)
        hOutOut = GlobalFree(hOutOut);  /* mark buffer as freed */

    if (zipfn) {
        LocalUnlock(hZipFN);
        zipfn = NULL;
    }
    if (hZipFN)
        hZipFN = LocalFree(hZipFN);

    if (filename) {
        LocalUnlock(hFileName);
        filename = NULL;
    }
    if (hFileName)
        hFileName = LocalFree(hFileName);
}
