//
// BuildDistribution.cc
//
// Program to build a zipfile containing a distribution of
// PKG-SNF-SDK-WIN.
//
// (C) Copyright 2010 ARM Research Labs, LLC See
// www.armresearch.com for the copyright terms.
//
// Usage:
//
//   BuildDistribution -d DistPath -t TempPath [-h -v -i]
//
//   where DistPath is the path of the distribution
//   (e.g. SNFMultiSDK_Windows_3.1, and TempPath is the name of the
//   directory to use for temporary storage.  TempPath must be a
//   directory, and must exist.
//
//   The "-h" input causes the program to output help text and exit.
//
//   Specifying "-v" causes the program to be verbose.
//
//   Specifying "-i" causes the program to wait for user confirmation
//   before proceeding.  Specifying "-i" causes the program to be
//   verbose.
//
// The program does the following:
//
//  1) For each directory DIR that appears in DistPath and also
//     in DistPath\..:
//
//     a) Delete the contents of DistPath/DIR, except for .svn.
//
//     b) Copy the contents of DistPath\..\DIR to DistPath\DIR, except
//        for .svn.
//
//  2) Create a temporary directory TempPath/DistName, where DistName
//     is the directory name of DistPath (e.g. if DistName is
//     c:\dir1\dir2\SNFMultiSDK_Windows_3.1, then DistName is
//     SNFMultiSDK_Windows_3.1).
//
//  3) For each directory DIR that appears in DistPath except for
//  .svn:
//
//     a) Copy all files from DistPath to TempPath/DistName, except
//        for .svn.
//
// $Id$
//
/////////////////////////////////////////////////////////////////////////////////////////////

#include <windows.h>
#include <tchar.h>

#include <iostream>
#include <string>

#include "Utility.h"

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

    std::string TempPath = "C:\\TEMP";                                          // Default temporary directory.
    std::string InputDistPath;                                                  // Names of directories and paths.
    bool Verbose = false;
    bool Interactive = false;

    for (int i = 1; i < argc; i++) {                                            // Get command-line input.

        std::string CmdArg;

        CmdArg = argv[i];
        if ("-d" == CmdArg) {

            if (++i == argc) {
                OutputUsage(TempPath);
                return(-1);
            }
            InputDistPath = argv[i];

        } else if ("-t" == CmdArg) {

            if (++i == argc) {
                OutputUsage(TempPath);
                return(-1);
            }
            TempPath = argv[i];

        } else if ("-v" == CmdArg) {

            Verbose = true;

        } else if ("-i" == CmdArg) {

            Interactive = true;
            Verbose = true;

        } else if ("-h" == CmdArg) {

            OutputHelp();
            OutputUsage(TempPath);
            return(0);

        }

    }

    if ( ("" == InputDistPath) || ("" == TempPath) ) {                          // Check input.

        OutputUsage(TempPath);
        return(-1);

    }

    std::string DistPath;                                                       // Names of the directories.
    std::string DistParentPath;
    std::string DistName;
    std::string TempDir;

    try {

        GetDirectoryNames(InputDistPath,
                          DistPath,
                          DistParentPath,
                          DistName);

        TempDir = TempPath + "\\";
        TempDir += DistName;

        if (Verbose) {

            std::cout
              << "\nThe distribution will be built from the files in the source "
              "directory "
              << DistPath << ".\n\n"
              << "The files in the directories that exist in both "
              << DistParentPath << " and the source directory will be copied into the "
              << "distribution.\n\n"
              << "The temporary directory is " << TempDir << ".\n\n";

            if (Interactive) {

                std::cout << "Enter <cr> to continue:  ";

                (void) std::cin.get();

            }

        }

        StringContainer ExcludeName;                                            // Create the list of file names
        ExcludeName.push_back(".svn");                                          // to not copy.
        ExcludeName.push_back(".");
        ExcludeName.push_back("..");
        ExcludeName.push_back(DistName);

        UpdateCommonDirectories(DistPath, DistParentPath, ExcludeName);         // Update files in directories
                                                                                // common to both DistPath and
                                                                                // DistParentPath.

        PrepareTempDir(TempDir);                                                // Delete any existing temporary
                                                                                // directory.

        CopyDirectory(DistPath, TempDir, ExcludeName);                          // Copy files to temporary directory.

        if (Verbose) {

            std::cout << "Distribution created in " << TempDir << ".\n";

        }

    } catch (std::exception &e) {

        OutputErrorMessage(e.what());
        return(-1);

    }

    return(0);
}
