/*
 * Copyright (c) 2013 MovingBlocks
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.terasology.launcher.util;

import java.io.File;
import java.io.IOException;

public final class DirectoryUtils {

    public static final String LAUNCHER_APPLICATION_DIR_NAME = "terasologylauncher";
    public static final String GAMES_APPLICATION_DIR_NAME = "terasology";
    public static final String DOWNLOAD_DIR_NAME = "download";

    private static final String PROPERTY_USER_HOME = "user.home";

    private DirectoryUtils() {
    }

    public static void checkDirectory(final File directory) throws IOException {
        if (!directory.exists() && !directory.mkdirs()) {
            throw new IOException("Can not create directory! " + directory);
        }

        if (!directory.isDirectory()) {
            throw new IOException("Directory is not a directory! " + directory);
        }

        if (!directory.canRead() || !directory.canWrite()) {
            throw new IOException("Can not read from or write into directory! " + directory);
        }
    }

    /**
     * Should only be executed once at the start.
     */
    public static File getApplicationDirectory(final OperatingSystem os, final String applicationName) {
        final String userHome = System.getProperty(PROPERTY_USER_HOME, ".");
        File applicationDirectory;

        if (os.isUnix()) {
            applicationDirectory = new File(userHome, '.' + applicationName + '/');
        } else if (os.isWindows()) {
            final String applicationData = System.getenv("APPDATA");
            if (applicationData != null) {
                applicationDirectory = new File(applicationData, "." + applicationName + '/');
            } else {
                applicationDirectory = new File(userHome, '.' + applicationName + '/');
            }
        } else if (os.isMac()) {
            applicationDirectory = new File(userHome, "Library/Application Support/" + applicationName);
        } else {
            applicationDirectory = new File(userHome, applicationName + '/');
        }

        return applicationDirectory;
    }
}

@@ -780,33 +780,33 @@
 ION_DIR_NAME = %22
-t
+T
 erasologylaunche
@@ -798,17 +798,17 @@
 rasology
-l
+L
 auncher%22
@@ -870,17 +870,17 @@
 NAME = %22
-t
+T
 erasolog
@@ -1012,16 +1012,148 @@
 r.home%22;
+%0A    private static final String ENV_APPDATA = %22APPDATA%22;%0A    private static final String MAC_PATH = %22Library/Application Support/%22;
 %0A%0A    pr
@@ -1900,22 +1900,20 @@
   final 
-String
+File
  userHom
@@ -1916,16 +1916,25 @@
 rHome = 
+new File(
 System.g
@@ -1968,16 +1968,17 @@
 ME, %22.%22)
+)
 ;%0A      
@@ -2022,126 +2022,8 @@
  if 
-(os.isUnix()) %7B%0A            applicationDirectory = new File(userHome, '.' + applicationName + '/');%0A        %7D else if 
 (os.
@@ -2062,27 +2062,22 @@
  String 
-application
+envApp
 Data = S
@@ -2089,25 +2089,27 @@
 .getenv(
-%22
+ENV_
 APPDATA
-%22
 );%0A     
@@ -2119,27 +2119,23 @@
     if (
-application
+(envApp
 Data != 
@@ -2139,16 +2139,46 @@
 != null)
+ && (envAppData.length() %3E 0))
  %7B%0A     
@@ -2224,30 +2224,19 @@
 ile(
-applicationData, %22.%22 +
+envAppData,
  app
@@ -2243,33 +2243,34 @@
 licationName + '
-/
+%5C%5C
 ');%0A            
@@ -2339,14 +2339,8 @@
 ome,
- '.' +
  app
@@ -2347,37 +2347,241 @@
 licationName + '
-/
+%5C%5C
 ');%0A
+                // Alternatives :%0A                //   System.getenv(%22HOME%22)%0A                //   System.getenv(%22USERPROFILE%22)%0A                //   System.getenv(%22HOMEDRIVE%22) + System.getenv(%22HOMEPATH%22)%0A
             %7D%0A  
@@ -2606,11 +2606,12 @@
 s.is
-Mac
+Unix
 ()) 
@@ -2670,38 +2670,147 @@
 me, 
-%22Library/Application Support/%22
+'.' + applicationName.toLowerCase() + '/');%0A        %7D else if (os.isMac()) %7B%0A            applicationDirectory = new File(userHome, MAC_PATH
  + a
@@ -2823,16 +2823,22 @@
 tionName
+ + '/'
 );%0A     

Refactor getApplicationDirectory