package com.tns;

import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;

import com.tns.BuildConfig;

import android.R.plurals;
import android.content.Context;
import android.os.Build;
import android.os.Debug;
import android.provider.ContactsContract.Directory;
import android.util.Log;

public class NativeScriptSyncHelper
{
	private static String SYNC_ROOT_SOURCE_DIR = "/data/local/tmp/"; 
	private static final String SYNC_SOURCE_DIR = "/sync/";
	private static final String FULL_SYNC_SOURCE_DIR = "/fullsync/";
	private static final String REMOVED_SYNC_SOURCE_DIR = "/removedsync/";
	
	public static void sync(Context context)
	{
		if (BuildConfig.DEBUG)
		{
			String syncPath = SYNC_ROOT_SOURCE_DIR + context.getPackageName() + SYNC_SOURCE_DIR;
			String fullSyncPath = SYNC_ROOT_SOURCE_DIR + context.getPackageName() + FULL_SYNC_SOURCE_DIR;
			String removedSyncPath = SYNC_ROOT_SOURCE_DIR + context.getPackageName() + REMOVED_SYNC_SOURCE_DIR;
			
			
			if (Platform.IsLogEnabled)
			{
				Log.d(Platform.DEFAULT_LOG_TAG, "syncPath: " + syncPath);
				Log.d(Platform.DEFAULT_LOG_TAG, "fullSyncPath: " + fullSyncPath);
				Log.d(Platform.DEFAULT_LOG_TAG, "removedSyncPath: " + removedSyncPath);
			}
			
			File fullSyncDir = new File(fullSyncPath);
			if (fullSyncDir.exists())
			{
				executeFullSync(context, fullSyncDir);
				return;
			}
			
			File syncDir = new File(syncPath);
			if (syncDir.exists())
			{
				executePartialSync(context, syncDir);
			}
			
			File removedSyncDir = new File(removedSyncPath);
			if (removedSyncDir.exists())
			{
				executeRemovedSync(context, removedSyncDir);
			}
		}
	}

	private static void executeFullSync(Context context, File sourceDir)
	{
		try
		{
			String targetPath = context.getFilesDir().getAbsolutePath();
			
			StringBuilder builder = new StringBuilder();
			builder.append("echo \"Syncing files:\n\"");
			builder.append("du -a \"" + sourceDir.getAbsolutePath() + "\" && "); //list files to sync in logcat
			builder.append("rm -Rf \"" + targetPath + "/app\" && "); //remove app dir
			builder.append("rm -Rf \"" + targetPath + "/tns_modules\" && "); //remove tns_modules dir
			builder.append("rm -Rf \"" + targetPath + "/metadata\" && "); //remove metadata dir
			builder.append("rm -Rf \"" + targetPath + "/internal\" && "); //remove internal dir
			builder.append(String.format("cp -Rf \"%s/*\" \"%s/\" && ", sourceDir.getAbsolutePath(), targetPath)); //copy all sync files
			builder.append("rm -Rf \"" + sourceDir.getAbsolutePath() + "\""); //remove all source dir files
			
			if (Platform.IsLogEnabled)
			{
				Log.d(Platform.DEFAULT_LOG_TAG, "Executing full sync command: " + builder.toString());
			}
			
			Runtime.getRuntime().exec(builder.toString());
		}
		catch (IOException e)
		{
			Log.e(Platform.DEFAULT_LOG_TAG, "Full Sync failed");
			e.printStackTrace();
		}
	}
	
	private static void executePartialSync(Context context, File sourceDir)
	{
		try
		{
			String targetPath = context.getFilesDir().getAbsolutePath();
			
			StringBuilder builder = new StringBuilder();
			builder.append("echo \"Syncing files:\n\"");
			builder.append("du -a \"" + sourceDir.getAbsolutePath() + "\" && "); //list files to sync in logcat
			builder.append(String.format("cp -Rf \"%s/*\" \"%s/\" && ", sourceDir.getAbsolutePath(), targetPath)); //copy all sync files
			builder.append("rm -Rf \"" + sourceDir.getAbsolutePath() + "\""); //remove all source dir files
			
			if (Platform.IsLogEnabled)
			{
				Log.d(Platform.DEFAULT_LOG_TAG, "Executing sync command: " + builder.toString());
			}
			
			Runtime.getRuntime().exec(builder.toString());
		}
		catch (IOException e)
		{
			Log.e(Platform.DEFAULT_LOG_TAG, "Sync failed");
			e.printStackTrace();
		}
	}
	
	private static void executeRemovedSync(final Context context, final File sourceDir)
	{
		final String targetPath = context.getFilesDir().getAbsolutePath();
		final String removedSyncPath = SYNC_ROOT_SOURCE_DIR + context.getPackageName() + REMOVED_SYNC_SOURCE_DIR;

		sourceDir.listFiles(new FileFilter()
		{
			@Override
			public boolean accept(File pathname)
			{
				if (pathname.isFile())
				{
					String filePathToRemove = pathname.getAbsolutePath().replace(removedSyncPath, targetPath);
					if (Platform.IsLogEnabled)
					{
						Log.d(Platform.DEFAULT_LOG_TAG, "Sync removing file: " + filePathToRemove);
					}
					
					File fileToRemove = new File(filePathToRemove);
					fileToRemove.delete();
					pathname.delete();
				}

				return false;
			}
		});
	}
}

@@ -78,261 +78,52 @@
 .io.
-FileReader;%0Aimport java.io.IOException;%0Aimport java.util.Arrays;%0A%0Aimport com.tns.BuildConfig;%0A%0Aimport android.R.plurals;%0Aimport android.content.Context;%0Aimport android.os.Build;%0Aimport android.os.Debug;%0Aimport android.provider.ContactsContract.Directory
+IOException;%0A%0Aimport android.content.Context
 ;%0Aim
@@ -1559,32 +1559,155 @@
 r)%0A%09%7B%0A%09%09try%0A%09%09%7B%0A
+%09%09%09//this removes only the app directory from the device to preserver any existing files in /files directory on the device%0A
 %09%09%09String target
@@ -1746,36 +1746,32 @@
 AbsolutePath();%0A
-%09%09%09%0A
 %09%09%09StringBuilder
@@ -1945,32 +1945,32 @@
  sync in logcat%0A
+
 %09%09%09builder.appen
@@ -2034,275 +2034,8 @@
 dir%0A
-%09%09%09builder.append(%22rm -Rf %5C%22%22 + targetPath + %22/tns_modules%5C%22 && %22); //remove tns_modules dir%0A%09%09%09builder.append(%22rm -Rf %5C%22%22 + targetPath + %22/metadata%5C%22 && %22); //remove metadata dir%0A%09%09%09builder.append(%22rm -Rf %5C%22%22 + targetPath + %22/internal%5C%22 && %22); //remove internal dir%0A
 %09%09%09b

remove non app directories from sync