// Copyright (C) 1997-2004 Alias Systems Corp.
// 
// The information in this file is provided for the exclusive use of the
// licensees of Alias.  Such users have the right to use, modify,
// and incorporate this code into other products for purposes authorized
// by the Alias license agreement, without fee.
// 
// ALIAS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
// INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
// EVENT SHALL ALIAS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
// DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

//
//  Alias Script File
//  MODIFY THIS AT YOUR OWN RISK
//
//  Creation Date:  November 12 2000
//
//  Procedure Name:
//      incrementalSaveProcessPath
//
//  Description:
//      Takes the save file path and returns the information that we'll need 
//		to do the incremental save.
//
//  Input Arguments:
//      path to split
//
//  Return Value:
//      String array where elements are as follows:
//			0 - scene name
//			1 - scene path
//			2 - scene name extension
//			3 - scene prefix
//			4 - version string
//			5 - increment dir name
//
global proc string[] incrementalSaveProcessPath( string $scenePath )
{
	string $result[];

	// Separate the full filename into path and name (eg. "C:/maya/projects/scenes/" and "myScene.mb")
	//
	string $scenePath = `file -q -sceneName`;
	string $sceneName = `match "[^/]+$" $scenePath`;
	$scenePath = `substring $scenePath 1 (size($scenePath) - size($sceneName))`;

	string $sceneExtension = `match "[.][^.]*$" $sceneName`;
	if($sceneExtension == `match "[.][0-9]+" $sceneExtension`) {
		$sceneExtension = "";
	} 

	string $fullScenePrefix = `substring $sceneName 1 (size($sceneName) - size($sceneExtension))`;

	string $currVersionString = `match "[.][0-9]+$" $fullScenePrefix`;
	$currVersionString = `match "[0-9]+" $currVersionString`;

	// Find the extension and "prefix" of the sceneName. We may use the "fullScenePrefix" later.
	// For example, we would want "myScene4.screen2.0001.mb" to give us "myScene4.screen2"
	//
	string $sceneNamePrefix = $fullScenePrefix;

	if($currVersionString != "") {
		$sceneNamePrefix = `substring $sceneNamePrefix 1 
			(size($sceneNamePrefix) - size($currVersionString) - 1)`;
	}

	string $incrementDirName = "incrementalSave/" + $sceneNamePrefix + $sceneExtension + "/";
	string $lastDirectoryName = `match "incrementalSave/[^/]+/$" $scenePath`;

	if($lastDirectoryName == $incrementDirName) {
		// The directory-name for storing older increments is already at the end of the
		// path for the current scene, so we can assume that the user is saving changes to
		// an older increment. We will want to save these changes to the original scene name.
		//
		$scenePath = `substring $scenePath 1 (size($scenePath) - size($lastDirectoryName))`;
	} else {
		// There is the possibility of users naming a scene so that it ends in
		// a number (myOriginalScene.4.ma), just like increments are.
		// If this happens, the previous code will have been fooled into thinking that
		// the number was not part of the scene name. However, if we know that we're
		// not inside the increments directory, then we know that the full original 
		// scene prefix should be used (not the one with the number trimmed off!).
		//
		$sceneNamePrefix = $fullScenePrefix;
		$incrementDirName = "incrementalSave/" + $sceneNamePrefix + $sceneExtension;
	}

	$result[0] = $scenePath;
	$result[1] = $sceneName;
	$result[2] = $sceneExtension;
	$result[3] = $sceneNamePrefix;
	$result[4] = $currVersionString;
	$result[5] = $incrementDirName;

	return $result;
}