public static function parseHomeDirectory()
    {
        if ($value = getenv('PULI_HOME')) {
            $homeDir = $value;
            $env = 'PULI_HOME';
        } elseif ($value = getenv('HOME')) {
            $homeDir = $value;
            $env = 'HOME';
        } elseif ($value = getenv('APPDATA')) {
            $homeDir = $value;
            $env = 'APPDATA';
        } else {
            throw new InvalidConfigException(sprintf(
                'Either the context variable PULI_HOME or %s must be set for '.
                'Puli to run.',
                defined('PHP_WINDOWS_VERSION_MAJOR') ? 'APPDATA' : 'HOME'
            ));
        }

        $homeDir = strtr($homeDir, array('\\' => '/'));

        if (!file_exists($homeDir)) {
            throw new FileNotFoundException(sprintf(
                'The home path %s defined in the context variable %s '.
                'does not exist.',
                $homeDir,
                $env
            ));
        }

        if (is_file($homeDir)) {
            throw new NoDirectoryException(sprintf(
                'The home path %s defined in the context variable %s '.
                'points to a file. Expected a directory path.',
                $homeDir,
                $env
            ));
        }

        switch ($env) {
            case 'PULI_HOME':
                return $homeDir; // user defined
            case 'HOME':
                return $homeDir.'/.puli'; // Linux/Mac
            default:
                return $homeDir.'/Puli'; // Windows
        }
    }