/*
 * ARCre - Create rename scripts so that files with long names can be
 *         easily put into ARChives.
 *
 * Here's how to use it:
 *
 *   ARCre [file(s) to be ARCed...]         : Create the rename scripts.
 *   execute ARCre.bat                      : Rename the files to be ARCed.
 *   ARC a ARChive ARCfile.* execute.me     : Create the ARChive.
 *   execute execute.me                     : Rename the files back.
 *
 *   ARCre takes file names like the ARC program does.  (It will accept
 *   the '*' wildcard.)  Two scripts are produced: "ARCre.bat" and
 *   "execute.me".  The first one will rename the files to "ARCfile.#",
 *   where '#' is 1,2,3,...  The second one will rename the files back
 *   to their original names.
 *
 * Copyright (c) 1987 John R. Hoffman
 *
 * This program is placed in the public domain as long as the above
 * copyright is included.  Sale of this program except for REASONABLE 
 * media costs is prohibited.
 *
 * The credit for the idea for this program goes to John Foust.
 *
 * To create with MANX: cc ARCre.c
 *                      ln ARCre.o -lc
 *
 */

static char *Copyright = "Copyright (c) 1987 John R. Hoffman";

#include <stdio.h>

#define RENAME1  "ARCre.bat"
#define RENAME2  "EXECUTE.ME"
#define BASENAME "ARCfile.%d"

FILE *fopen();
char *scdir();

main(argc, argv)
int argc;
char *argv[];
{
    int i, fcnt;
    char *cp, name[50];
    FILE *fp1, *fp2;

    fp1  = NULL;
    fp2  = NULL;
    fcnt = 1;

    if ( argc <= 1 ) {
        fprintf(stderr, "Usage: ARCre files ...\n");
        exit();
    }

    for ( i = 1; i < argc; i++ ) {

        while ( (cp = scdir(argv[i])) != (char *) NULL ) {

            if ( fp1 == (FILE *) NULL ) {
                if ( (fp1 = fopen(RENAME1, "w")) == (FILE *) NULL ) {
                    fprintf(stderr, "ARCre: Error opening \"%s\".\n", RENAME1);
                    exit();
                }
            }

            if ( fp2 == (FILE *) NULL ) {
                if ( (fp2 = fopen(RENAME2, "w")) == (FILE *) NULL ) {
                    fprintf(stderr, "ARCre: Error opening \"%s\".\n", RENAME2);
                    if ( fp1 != (FILE *) NULL ) {
                        fclose(fp1);
                    }
                    exit();
                }
            }

            sprintf(name, BASENAME, fcnt++);

            fprintf(fp1, "rename \"%s\" %s\n", cp, name);

            fprintf(fp2, "rename %s \"%s\"\n", name, cp);

        }

    }

    if ( fcnt == 1 ) {
        fprintf(stderr, "ARCre: No matching files found.\n");
    	exit();
    }

    if ( fp1 != (FILE *) NULL ) {
        fclose(fp1);
    }

    if ( fp2 != (FILE *) NULL ) {
        fclose(fp2);
    }

    exit();
}
