//************************************************
// Project:    System 2.0.0
// Filename:   mycopyattr.js
// Date&Time:  08.03.2002 - initial version
//
// Version:	   1.0
//
// Purpose:    attribute class for file copy
// Author:     M.Klygin
//************************************************
//**Start Encode**
var regRoot = "HKCU\\Software\\Stoum\\MyCopy" ;
var mycopyAttr_Attributes = new Array(  
		"s", "source", 
		"t", "target",
		"m", "filemask",
		"r", "reqursive",
		"a", "action",
		"v", "verbose",
		"o", "overwrite",
		"d", "since",
		"d2","till",
		"reg", "registry",
		"df","dateformat");

var mycopyAttr_ShortAttributes = new Array(  
		"source", 
		"target",
		"filemask",
		"reqursive",
		"action",
		"verbose",
		"overwrite",
		"since",
		"till",
		"registry",
		"dateformat");

//************************************************
// Function:    mycopyAttr_ToString
// Arguments:
//
// Purpose:     convert class to string
// Version:     1.0    
// Author:      M.Klygin
// History:     08.03.2002  initial version
//************************************************
function mycopyAttr_ToString()
{
    var res =   "source="+this.sourceDir+" "+
                "target="+this.targetDir+" "+
                "filemask="+this.filemask+" "+
                "reqursive="+this.reqursive+" "+
                "action="+this.action+" "+
                "logfile="+this.logfile+" "+
                "overwrite="+this.overwrite+" "+
                "since="+this.since+" "+
                "till="+this.till+" "+
                "dateformat="+this.dateformat+" "+
                "registry="+this.registry;
    return res;
}

//************************************************
// Function:     mycopyAttr_setProperty
// Arguments:   
//  arg          command line arguments
//
// Purpose:  
// Version:      1.0    
// Author:       M.Klygin
// History:      10.03.2002  initial version
//************************************************
function mycopyAttr_setProperty(property, val)
{
    switch(property.toLowerCase()) 
    {
        case "s":   
            this.sourceDir = val;
            break;
        case "t":
            this.targetDir = val;
            break;
        case "m":
            this.filemask = val;
            break;
        case "r":
            this.reqursive = true;
            break;
        case "a":
            this.action = val;
            this.move   = this.action.toLowerCase()=="move"
            break;
        case "v":
            this.logfile = val;
            break;
        case "o":
            this.overwrite = true;
            break;
        case "d":
			since = val;
            this.since = parseDate(since, this.dateformat) ;
            break;
        case "d2":
            till = val;
            this.till = parseDate(till, this.dateformat) ;
            break;
        case "df":
            this.dateformat = val;
            break;
        case "reg":
            this.registry = val;
            break;

        case "source":
            this.sourceDir = val;
            break;
        case "target":
            this.targetDir = val;
            break;
        case "filemask":
            this.filemask = val;
            break;
        case "reqursive":
            this.reqursive = true;
            break;
        case "action":
            this.action = val;
            this.move   = this.action.toLowerCase()=="move"
            break;
        case "verbose":
            this.logfile = val;
            break;
        case "overwrite":
            this.overwrite = true;
            break;
        case "since":
            since = val;
            this.since = parseDate(since, this.dateformat) ;
            break;
        case "till":
            till = val;
            this.till = parseDate(till, this.dateformat) ;
            break;
        case "dateformat":
            this.dateformat = val;
            break;
        case "registry":
            this.registry = val;
            break;
    } 
}

//************************************************
// Function:    mycopyAttr_getProperty
// Arguments:   
//  arg         command line arguments
//
// Purpose:  
// Version:     1.0    
// Author:      M.Klygin
// History:     10.03.2002  initial version
//************************************************
function mycopyAttr_getProperty(property)
{
    switch(property.toLowerCase()) 
    {
        case "s":   
            val = this.sourceDir;
            break;
        case "t":
            val = this.targetDir;
            break;
        case "m":
            val = this.filemask;
            break;
        case "r":
            val = this.reqursive;
            break;
        case "a":
            val = this.action;
            break;
        case "v":
            val = this.logfile;
            break;
        case "o":
            val = this.overwrite;
            break;
        case "d":
            val = formatDate(this.since, this.dateformat);
            break;
        case "d2":
            val = formatDate(this.till, this.dateformat);
            break;
        case "df":
            val = this.dateformat;
            break;
        case "reg":
            val = this.registry;
            break;

        case "source":
            val = this.sourceDir;
            break;
        case "target":
            val = this.targetDir;
            break;
        case "filemask":
            val = this.filemask;
            break;
        case "reqursive":
            val = this.reqursive;
            break;
        case "action":
            val = this.action;
            break;
        case "verbose":
            val = this.logfile;
            break;
        case "overwrite":
            val = this.overwrite;
            break;
        case "since":
            val = formatDate(this.since, this.dateformat) ;
            break;
        case "till":
            val = formatDate(this.till, this.dateformat) ;
            break;
        case "dateformat":
            val = this.dateformat;
            break;
        case "registry":
            val = this.registry;
            break;
    } 
	return val;
}

//************************************************
// Function:    mycopyAttr_RegRead
// Arguments:   
//  arg         arguments
//
// Purpose:  
// Version:     1.0    
// Author:      M.Klygin
// History:     10.03.2002  initial version
//************************************************
function mycopyAttr_RegRead(property)
{
    var WshShell = WScript.CreateObject ("WScript.Shell");

    try
    {
        var key = regRoot+"\\"+this.registry+"\\"+property;
        var val = WshShell.RegRead(key);
        this.setProperty(property, val) ;
        return 1 ;
    }
    catch (err)
    {
        return -1 ;
    }
}

//************************************************
// Function:     mycopyAttr_RegWrite
// Arguments:   
//
// Purpose:  
// Version:      1.0    
// Author:       M.Klygin
// History:      10.03.2002  initial version
//************************************************
function mycopyAttr_RegWrite(property)
{

    if (property.toLowerCase()=='all')
    {
        var argList = mycopyAttr_ShortAttributes ;
        // read registry properies
        for (var i=0; i < argList.length; i++)
        {
            this.RegWrite(argList[i]) ;
        }
        return 1;
    }

    var WshShell = WScript.CreateObject ("WScript.Shell");

    try
    {
	    var key = regRoot+"\\"+this.registry+"\\"+property;
		var val = this.getProperty(property) ;
        WshShell.RegWrite(key, val);
        return 1 ;
    }
    catch (err)
    {
        return -1 ;
    }
}

//************************************************
// Function:     mycopyAttr_Usage
// Arguments:   
//
// Purpose:  
// Version:      1.0    
// Author:       M.Klygin
// History:     10.03.2002  initial version
//************************************************
function mycopyAttr_Usage()
{
    var Usage = "Usage:\n"+
        "WScript mycopy.wsf [/switches:parameters]\n"+
        "\n"+
        "Switches:\n" + 
        "   s  | source     - source directory\n"+
        "   t  | target     - target directory\n"+
        "   m  | filemask   - file mask, inluding wildcards\n"+
        "   r  | reqursive  - target directory\n"+
        "   a  | action     - action (\"copy\", \"move\")\n"+
        "   v  | verbose    - verbose mode\n"+
        "   o  | overwrite  - overwrite target file\n"+
        "   d  | since      - since date\n"+
        "   d2 | till       - till date\n"+
        "   df | dateformat - date format (\"dd.MM.yyyy HH:mm:ss\") \n" ;

    return Usage;
}


//************************************************
// Function:     mycopyAttr
// Arguments:
//
// Purpose:     create attribute class
// Version:      1.0    
// Author:       M.Klygin
// History:     08.03.2002  initial version
//************************************************
function mycopyAttr1(sourceDir, targetDir, filemask, reqursive, action, since, till)
{
    var WshShell = WScript.CreateObject ("WScript.Shell");
    var currentDir = WshShell.CurrentDirectory ;

    if (arguments.length == 0)
    {
        sourceDir = currentDir;
        targetDir = currentDir;
        filemask  = '*.*';
        reqursive = false;
        action    = "copy";
        since     = new Date(1980,01,01);
        till      = new Date(2050,01,01);
    }
	dateformat= "dd.MM.yyyy HH:mm:ss";

    this.sourceDir = sourceDir;
    this.targetDir = targetDir;
    this.filemask  = filemask;
    this.reqursive = reqursive;
    this.action    = action;
    this.move      = action.toLowerCase()=="move"
    this.dateformat= dateformat;
    this.since     = parseDate(since, this.dateformat);  //since;
    this.till      = parseDate(till, this.dateformat);  //till;
    this.logfile   = "result.txt";
    this.overwrite = false;

    this.toString  = mycopyAttr_ToString;
    this.setProperty=mycopyAttr_setProperty;
    this.getProperty=mycopyAttr_getProperty;
    this.RegRead   = mycopyAttr_RegRead ;
    this.RegWrite  = mycopyAttr_RegWrite ;
    this.Usage     = mycopyAttr_Usage ;
}

//************************************************
// Function:     mycopyAttr
// Arguments:   
//  arg         command line arguments
//
// Purpose:  
// Version:      1.0    
// Author:       M.Klygin
// History:     08.03.2002  initial version
//************************************************
function mycopyAttr(arg)
{
    var argList = mycopyAttr_Attributes ;

    var WshShell = WScript.CreateObject ("WScript.Shell");
    var currentDir = WshShell.CurrentDirectory ;
    
    // default properties
    this.sourceDir = currentDir;
    this.targetDir = currentDir;
    this.filemask  = '*.*';
    this.reqursive = false;
    this.action    = "copy";
    this.move      = this.action.toLowerCase()=="move"
    this.logfile   = "";
    this.overwrite = false;
    this.since     = new Date(1980,01,01);
    this.till      = new Date ;//new Date(2050,01,01);
    this.dateformat= "dd.MM.yyyy HH:mm:ss";
    this.registry  = "application";

    this.toString  = mycopyAttr_ToString;
    this.setProperty=mycopyAttr_setProperty;
    this.getProperty=mycopyAttr_getProperty;
    this.RegRead   = mycopyAttr_RegRead ;
    this.RegWrite  = mycopyAttr_RegWrite ;
    this.Usage     = mycopyAttr_Usage   ;

	
	// read registry properies
	if (arg.Named.Exists("reg"))
	{
		var val = arg.Named("reg");
		this.setProperty("reg", val) ;
	}
    for (var i=0; i < argList.length; i++)
    {
        this.RegRead(argList[i]) ;
    }

    // read argument properies
    for (var i=0; i < argList.length; i++)
    {
        if (arguments.length > 0)
        {
            if (arg.Named.Exists(argList[i]))
            {
                var val = arg.Named(argList[i]);
                
                this.setProperty(argList[i], val) ;
            }
        }
    }
}
